Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Data views] Implement persistable state #132959

Merged
merged 14 commits into from
Jun 7, 2022
2 changes: 2 additions & 0 deletions packages/kbn-es-query/src/es_query/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ export type {
IFieldSubType,
BoolQuery,
DataViewBase,
DataViewBaseSerializable,
DataViewFieldBase,
DataViewFieldBaseSerializable,
IFieldSubTypeMulti,
IFieldSubTypeNested,
} from './types';
9 changes: 7 additions & 2 deletions packages/kbn-es-query/src/es_query/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,23 @@
*/

import * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import { SerializableRecord } from '@kbn/utility-types';

/**
* A field's sub type
* @public
*/
export type IFieldSubType = IFieldSubTypeMultiOptional | IFieldSubTypeNestedOptional;

export interface IFieldSubTypeMultiOptional {
export interface IFieldSubTypeMultiOptional extends SerializableRecord {
multi?: { parent: string };
}

export interface IFieldSubTypeMulti {
multi: { parent: string };
}

export interface IFieldSubTypeNestedOptional {
export interface IFieldSubTypeNestedOptional extends SerializableRecord {
nested?: { path: string };
}

Expand Down Expand Up @@ -53,6 +54,8 @@ export interface DataViewFieldBase {
scripted?: boolean;
}

export type DataViewFieldBaseSerializable = SerializableRecord & DataViewFieldBase;

/**
* A base interface for an index pattern
* @public
Expand All @@ -63,6 +66,8 @@ export interface DataViewBase {
title: string;
}

export type DataViewBaseSerializable = SerializableRecord & DataViewBase;

export interface BoolQuery {
must: estypes.QueryDslQueryContainer[];
must_not: estypes.QueryDslQueryContainer[];
Expand Down
2 changes: 2 additions & 0 deletions packages/kbn-es-query/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
export type {
BoolQuery,
DataViewBase,
DataViewBaseSerializable,
DataViewFieldBase,
DataViewFieldBaseSerializable,
EsQueryConfig,
IFieldSubType,
IFieldSubTypeMulti,
Expand Down
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 { DataViewBaseSerializable } from '@kbn/es-query';
lukasolson marked this conversation as resolved.
Show resolved Hide resolved
import { DataViewPersistableStateService } from './persistable_state';
import { SavedObjectReference } from '@kbn/core/types';
const { inject, extract } = DataViewPersistableStateService;

describe('data view persistable state tests', () => {
test('inject references', () => {
const state: DataViewBaseSerializable = {
id: 'my-id',
title: 'my-title',
fields: [],
};
const references: SavedObjectReference[] = [];

const result = inject(state, references);

expect(result).toBe(state);
});

test('extract references', () => {
const state: DataViewBaseSerializable = {
id: 'my-id',
title: 'my-title',
fields: [],
};

const result = extract(state);

expect(result.state).toBe(state);
expect(result.references).toEqual([]);
});
});
22 changes: 22 additions & 0 deletions src/plugins/data_views/common/data_views/persistable_state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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 { DataViewBaseSerializable } from '@kbn/es-query';
import { PersistableStateService } from '@kbn/kibana-utils-plugin/common';

export const DataViewPersistableStateService: PersistableStateService<DataViewBaseSerializable> = {
inject: (state, references) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As data view spec can contain fields we should call field methods inside each of this.

cc @mattkime @sixstringcode if we don't plan to work on local runtime fields (if local data views can satisfy that requirement for some time) then we don't need to implement persistable state interface for fields.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe from my conversation with @mattkime that this is still up in the air, so I'll remove the implementation for fields from this PR.

return state;
},
extract: (state) => {
return { state, references: [] };
},
getAllMigrations: () => ({}),
migrateToLatest: ({ state }) => state,
telemetry: () => ({}),
};
38 changes: 38 additions & 0 deletions src/plugins/data_views/common/fields/persistable_state.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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 { DataViewFieldBaseSerializable } from '@kbn/es-query';
import { DataViewFieldPersistableStateService } from './persistable_state';
import { SavedObjectReference } from '@kbn/core/types';
const { inject, extract } = DataViewFieldPersistableStateService;

describe('data view field persistable state tests', () => {
test('inject references', () => {
const state: DataViewFieldBaseSerializable = {
name: 'my-name',
type: 'my-type',
};
const references: SavedObjectReference[] = [];

const result = inject(state, references);

expect(result).toBe(state);
});

test('extract references', () => {
const state: DataViewFieldBaseSerializable = {
name: 'my-name',
type: 'my-type',
};

const result = extract(state);

expect(result.state).toBe(state);
expect(result.references).toEqual([]);
});
});
23 changes: 23 additions & 0 deletions src/plugins/data_views/common/fields/persistable_state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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 { DataViewFieldBaseSerializable } from '@kbn/es-query';
import { PersistableStateService } from '@kbn/kibana-utils-plugin/common';

export const DataViewFieldPersistableStateService: PersistableStateService<DataViewFieldBaseSerializable> =
{
inject: (state, references) => {
return state;
},
extract: (state) => {
return { state, references: [] };
},
getAllMigrations: () => ({}),
migrateToLatest: ({ state }) => state,
telemetry: () => ({}),
};
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export interface PersistableStateService<P extends Serializable = Serializable>
* @param version Current semver version of the `state`.
* @returns A serializable state object migrated to the latest state.
*/
migrateToLatest?: (state: VersionedState) => P;
migrateToLatest?: (state: VersionedState<P>) => P;

/**
* returns all registered migrations
Expand Down