filterManager PersistableStateService implementation (#104897) (#105972)

This commit is contained in:
Peter Pisljar 2021-07-16 19:14:00 +02:00 committed by GitHub
parent 38f6c18e63
commit 5ef9026ae0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 172 additions and 7 deletions

View file

@ -7,7 +7,7 @@
<b>Signature:</b>
```typescript
export declare class FilterManager
export declare class FilterManager implements PersistableStateService
```
## Constructors
@ -16,6 +16,16 @@ export declare class FilterManager
| --- | --- | --- |
| [(constructor)(uiSettings)](./kibana-plugin-plugins-data-public.filtermanager._constructor_.md) | | Constructs a new instance of the <code>FilterManager</code> class |
## Properties
| Property | Modifiers | Type | Description |
| --- | --- | --- | --- |
| [extract](./kibana-plugin-plugins-data-public.filtermanager.extract.md) | | <code>any</code> | |
| [getAllMigrations](./kibana-plugin-plugins-data-public.filtermanager.getallmigrations.md) | | <code>() =&gt; {}</code> | |
| [inject](./kibana-plugin-plugins-data-public.filtermanager.inject.md) | | <code>any</code> | |
| [migrateToLatest](./kibana-plugin-plugins-data-public.filtermanager.migratetolatest.md) | | <code>any</code> | |
| [telemetry](./kibana-plugin-plugins-data-public.filtermanager.telemetry.md) | | <code>(filters: import(&quot;../../../../kibana_utils/common/persistable_state&quot;).SerializableState, collector: unknown) =&gt; {}</code> | |
## Methods
| Method | Modifiers | Description |

View file

@ -0,0 +1,37 @@
/*
* 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 '../es_query/filters';
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: 'index_pattern', name: 'test', id: '123' }]);
expect(updatedFilters[0]).toHaveProperty('meta.index', '123');
});
test('drops index setting if reference is missing', () => {
const updatedFilters = inject(filters, [
{ type: 'index_pattern', 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');
});
});
});

View file

@ -0,0 +1,64 @@
/*
* 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 { SerializableState } from '../../../kibana_utils/common/persistable_state';
import { SavedObjectReference } from '../../../../core/types';
import { Filter } from '../es_query/filters';
export const extract = (filters: Filter[]) => {
const references: SavedObjectReference[] = [];
const updatedFilters = filters.map((filter) => {
if (filter.meta?.index) {
const id = uuid();
references.push({
type: 'index_pattern',
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: SerializableState, collector: unknown) => {
return {};
};
export const migrateToLatest = (filters: Filter[], version: string) => {
return filters;
};
export const getAllMigrations = () => {
return {};
};

View file

@ -85,7 +85,7 @@ import { Required } from '@kbn/utility-types';
import * as Rx from 'rxjs';
import { SavedObject } from 'src/core/server';
import { SavedObject as SavedObject_2 } from 'kibana/server';
import { SavedObjectReference } from 'src/core/types';
import { SavedObjectReference as SavedObjectReference_2 } from 'src/core/types';
import { SavedObjectsClientContract } from 'src/core/public';
import { SavedObjectsFindOptions } from 'kibana/public';
import { SavedObjectsFindResponse } from 'kibana/server';
@ -952,7 +952,7 @@ export type ExpressionValueSearchContext = ExpressionValueBoxed<'kibana_context'
// @public (undocumented)
export const extractSearchSourceReferences: (state: SearchSourceFields) => [SearchSourceFields & {
indexRefName?: string;
}, SavedObjectReference[]];
}, SavedObjectReference_2[]];
// Warning: (ae-missing-release-tag) "FieldFormat" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
//
@ -1085,14 +1085,19 @@ export type Filter = {
query?: any;
};
// Warning: (ae-forgotten-export) The symbol "PersistableStateService" needs to be exported by the entry point index.d.ts
// Warning: (ae-missing-release-tag) "FilterManager" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
//
// @public (undocumented)
export class FilterManager {
export class FilterManager implements PersistableStateService {
constructor(uiSettings: IUiSettingsClient);
// (undocumented)
addFilters(filters: Filter[] | Filter, pinFilterStatus?: boolean): void;
// (undocumented)
extract: any;
// (undocumented)
getAllMigrations: () => {};
// (undocumented)
getAppFilters(): Filter[];
// (undocumented)
getFetches$(): import("rxjs").Observable<void>;
@ -1107,6 +1112,10 @@ export class FilterManager {
// (undocumented)
getUpdates$(): import("rxjs").Observable<void>;
// (undocumented)
inject: any;
// (undocumented)
migrateToLatest: any;
// (undocumented)
removeAll(): void;
// (undocumented)
removeFilter(filter: Filter): void;
@ -1116,6 +1125,8 @@ export class FilterManager {
// (undocumented)
static setFiltersStore(filters: Filter[], store: FilterStateStore, shouldOverrideStore?: boolean): void;
setGlobalFilters(newGlobalFilters: Filter[]): void;
// (undocumented)
telemetry: (filters: import("../../../../kibana_utils/common/persistable_state").SerializableState, collector: unknown) => {};
}
// Warning: (ae-forgotten-export) The symbol "QueryLanguage" needs to be exported by the entry point index.d.ts
@ -1723,7 +1734,7 @@ export interface IndexPatternTypeMeta {
// @public (undocumented)
export const injectSearchSourceReferences: (searchSourceFields: SearchSourceFields & {
indexRefName: string;
}, references: SavedObjectReference[]) => SearchSourceFields;
}, references: SavedObjectReference_2[]) => SearchSourceFields;
// Warning: (ae-missing-release-tag) "InputTimeRange" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
//

View file

@ -25,8 +25,16 @@ import {
COMPARE_ALL_OPTIONS,
UI_SETTINGS,
} from '../../../common';
import { PersistableStateService } from '../../../../kibana_utils/common/persistable_state';
import {
getAllMigrations,
migrateToLatest,
inject,
extract,
telemetry,
} from '../../../common/query/persistable_state';
export class FilterManager {
export class FilterManager implements PersistableStateService {
private filters: Filter[] = [];
private updated$: Subject<void> = new Subject();
private fetch$: Subject<void> = new Subject();
@ -221,4 +229,17 @@ export class FilterManager {
}
});
}
// Filter needs to implement SerializableState
public extract = extract as any;
// Filter needs to implement SerializableState
public inject = inject as any;
public telemetry = telemetry;
// Filter needs to implement SerializableState
public migrateToLatest = migrateToLatest as any;
public getAllMigrations = getAllMigrations;
}

View file

@ -8,10 +8,27 @@
import { CoreSetup, Plugin } from 'kibana/server';
import { querySavedObjectType } from '../saved_objects';
import {
extract,
inject,
telemetry,
migrateToLatest,
getAllMigrations,
} from '../../common/query/persistable_state';
export class QueryService implements Plugin<void> {
public setup(core: CoreSetup) {
core.savedObjects.registerType(querySavedObjectType);
return {
filterManager: {
extract,
inject,
telemetry,
migrateToLatest,
getAllMigrations,
},
};
}
public start() {}

View file

@ -169,7 +169,7 @@ export interface PersistableStateService<P extends SerializableState = Serializa
* @param version Current semver version of the `state`.
* @returns A serializable state object migrated to the latest state.
*/
migrateToLatest?: (state: VersionedState) => VersionedState<P>;
migrateToLatest?: (state: VersionedState) => P;
/**
* returns all registered migrations

View file

@ -8,6 +8,7 @@ exports[`Header rendering renders correctly against snapshot 1`] = `
<Connect(StatefulSearchOrFilterComponent)
filterManager={
FilterManager {
"extract": [Function],
"fetch$": Subject {
"_isScalar": false,
"closed": false,
@ -17,6 +18,10 @@ exports[`Header rendering renders correctly against snapshot 1`] = `
"thrownError": null,
},
"filters": Array [],
"getAllMigrations": [Function],
"inject": [Function],
"migrateToLatest": [Function],
"telemetry": [Function],
"uiSettings": Object {
"get": [MockFunction],
"get$": [MockFunction],