forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
334 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/plugins/data/common/query/filters/persistable_state.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { extract, inject } from './persistable_state'; | ||
import { Filter } from '@kbn/es-query'; | ||
import { DATA_VIEW_SAVED_OBJECT_TYPE } from '@kbn/data-views-plugin/common'; | ||
|
||
describe('filter manager persistable state tests', () => { | ||
const filters: Filter[] = [ | ||
{ meta: { alias: 'test', disabled: false, negate: false, index: 'test' } }, | ||
]; | ||
describe('reference injection', () => { | ||
test('correctly inserts reference to filter', () => { | ||
const updatedFilters = inject(filters, [ | ||
{ type: DATA_VIEW_SAVED_OBJECT_TYPE, name: 'test', id: '123' }, | ||
]); | ||
expect(updatedFilters[0]).toHaveProperty('meta.index', '123'); | ||
}); | ||
|
||
test('drops index setting if reference is missing', () => { | ||
const updatedFilters = inject(filters, [ | ||
{ type: DATA_VIEW_SAVED_OBJECT_TYPE, name: 'test123', id: '123' }, | ||
]); | ||
expect(updatedFilters[0]).toHaveProperty('meta.index', undefined); | ||
}); | ||
}); | ||
|
||
describe('reference extraction', () => { | ||
test('correctly extracts references', () => { | ||
const { state, references } = extract(filters); | ||
expect(state[0]).toHaveProperty('meta.index'); | ||
expect(references[0]).toHaveProperty('id', 'test'); | ||
}); | ||
}); | ||
}); |
65 changes: 65 additions & 0 deletions
65
src/plugins/data/common/query/filters/persistable_state.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import uuid from 'uuid'; | ||
import { Filter } from '@kbn/es-query'; | ||
import { SavedObjectReference } from '@kbn/core/types'; | ||
import { MigrateFunctionsObject, VersionedState } from '@kbn/kibana-utils-plugin/common'; | ||
import { DATA_VIEW_SAVED_OBJECT_TYPE } from '@kbn/data-views-plugin/common'; | ||
|
||
export const extract = (filters: Filter[]) => { | ||
const references: SavedObjectReference[] = []; | ||
const updatedFilters = filters.map((filter) => { | ||
if (filter.meta?.index) { | ||
const id = uuid(); | ||
references.push({ | ||
type: DATA_VIEW_SAVED_OBJECT_TYPE, | ||
name: id, | ||
id: filter.meta.index, | ||
}); | ||
|
||
return { | ||
...filter, | ||
meta: { | ||
...filter.meta, | ||
index: id, | ||
}, | ||
}; | ||
} | ||
return filter; | ||
}); | ||
return { state: updatedFilters, references }; | ||
}; | ||
|
||
export const inject = (filters: Filter[], references: SavedObjectReference[]) => { | ||
return filters.map((filter) => { | ||
if (!filter.meta.index) { | ||
return filter; | ||
} | ||
const reference = references.find((ref) => ref.name === filter.meta.index); | ||
return { | ||
...filter, | ||
meta: { | ||
...filter.meta, | ||
index: reference && reference.id, | ||
}, | ||
}; | ||
}); | ||
}; | ||
|
||
export const telemetry = (filters: Filter[], collector: unknown) => { | ||
return {}; | ||
}; | ||
|
||
export const migrateToLatest = (filters: VersionedState<Filter[]>) => { | ||
return filters.state; | ||
}; | ||
|
||
export const getAllMigrations = (): MigrateFunctionsObject => { | ||
return {}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ | |
export * from './timefilter'; | ||
export * from './types'; | ||
export * from './is_query'; | ||
export * from './query_state'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import type { Filter } from '@kbn/es-query'; | ||
import type { TimeRange, RefreshInterval } from './timefilter/types'; | ||
import type { Query } from './types'; | ||
|
||
/** | ||
* All query state service state | ||
* | ||
* @remark | ||
* `type` instead of `interface` to make it compatible with PersistableState utils | ||
* | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions | ||
export type QueryState = { | ||
time?: TimeRange; | ||
refreshInterval?: RefreshInterval; | ||
filters?: Filter[]; | ||
query?: Query; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.