From 4b09eba4806f12cbab4fca835bd97c22bc406d01 Mon Sep 17 00:00:00 2001 From: Chris Davies Date: Thu, 15 Aug 2019 16:35:50 -0400 Subject: [PATCH] [Lens] Remove unnecessary fields and indexing from mappings (#43285) --- x-pack/legacy/plugins/lens/mappings.json | 9 ++-- .../editor_frame/editor_frame.test.tsx | 7 ---- .../editor_frame/editor_frame.tsx | 1 - .../editor_frame/save.test.ts | 2 - .../editor_frame_plugin/editor_frame/save.ts | 3 -- .../editor_frame/state_management.test.ts | 2 - .../editor_frame/state_management.ts | 19 ++++++++- .../embeddable/embeddable.test.tsx | 1 - .../public/editor_frame_plugin/plugin.tsx | 3 +- .../persistence/saved_object_store.test.ts | 42 ++----------------- .../public/persistence/saved_object_store.ts | 14 ++----- 11 files changed, 31 insertions(+), 72 deletions(-) diff --git a/x-pack/legacy/plugins/lens/mappings.json b/x-pack/legacy/plugins/lens/mappings.json index 9136447531be8..8eccf22eb2235 100644 --- a/x-pack/legacy/plugins/lens/mappings.json +++ b/x-pack/legacy/plugins/lens/mappings.json @@ -7,14 +7,13 @@ "visualizationType": { "type": "keyword" }, - "activeDatasourceId": { - "type": "keyword" - }, "state": { - "type": "text" + "enabled": false, + "type": "object" }, "expression": { - "type": "text" + "index": false, + "type": "keyword" } } } diff --git a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/editor_frame.test.tsx b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/editor_frame.test.tsx index 1b83ed4f07e70..3904c2c114543 100644 --- a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/editor_frame.test.tsx +++ b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/editor_frame.test.tsx @@ -154,7 +154,6 @@ describe('editor_frame', () => { initialVisualizationId="testVis" ExpressionRenderer={expressionRendererMock} doc={{ - activeDatasourceId: 'testDatasource', visualizationType: 'testVis', title: '', expression: '', @@ -482,7 +481,6 @@ describe('editor_frame', () => { initialVisualizationId="testVis" ExpressionRenderer={expressionRendererMock} doc={{ - activeDatasourceId: 'testDatasource', visualizationType: 'testVis', title: '', expression: '', @@ -725,7 +723,6 @@ describe('editor_frame', () => { initialVisualizationId="testVis" ExpressionRenderer={expressionRendererMock} doc={{ - activeDatasourceId: 'testDatasource', visualizationType: 'testVis', title: '', expression: '', @@ -779,7 +776,6 @@ describe('editor_frame', () => { initialVisualizationId="testVis" ExpressionRenderer={expressionRendererMock} doc={{ - activeDatasourceId: 'testDatasource', visualizationType: 'testVis', title: '', expression: '', @@ -1379,7 +1375,6 @@ describe('editor_frame', () => { expect(onChange).toHaveBeenNthCalledWith(1, { indexPatternTitles: ['resolved'], doc: { - activeDatasourceId: 'testDatasource', expression: '', id: undefined, state: { @@ -1397,7 +1392,6 @@ describe('editor_frame', () => { expect(onChange).toHaveBeenLastCalledWith({ indexPatternTitles: ['resolved'], doc: { - activeDatasourceId: 'testDatasource', expression: '', id: undefined, state: { @@ -1457,7 +1451,6 @@ describe('editor_frame', () => { expect(onChange).toHaveBeenNthCalledWith(3, { indexPatternTitles: [], doc: { - activeDatasourceId: 'testDatasource', expression: expect.stringContaining('vis "expression"'), id: undefined, state: { diff --git a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/editor_frame.tsx b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/editor_frame.tsx index ff393eabf8ca5..823852eed6133 100644 --- a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/editor_frame.tsx +++ b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/editor_frame.tsx @@ -198,7 +198,6 @@ export function EditorFrame(props: EditorFrameProps) { ), visualization, state, - activeDatasourceId: state.activeDatasourceId!, framePublicAPI, }); diff --git a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/save.test.ts b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/save.test.ts index 86c41868ff3dd..6bfe8f70d93c4 100644 --- a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/save.test.ts +++ b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/save.test.ts @@ -28,7 +28,6 @@ describe('save editor frame state', () => { activeDatasourceId: 'indexpattern', visualization: { activeId: '2', state: {} }, }, - activeDatasourceId: 'indexpattern', framePublicAPI: { addNewLayer: jest.fn(), removeLayers: jest.fn(), @@ -71,7 +70,6 @@ describe('save editor frame state', () => { }); expect(doc).toEqual({ - activeDatasourceId: 'indexpattern', id: undefined, expression: '', state: { diff --git a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/save.ts b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/save.ts index 829955d910e25..6c414d9866033 100644 --- a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/save.ts +++ b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/save.ts @@ -16,7 +16,6 @@ export interface Props { state: EditorFrameState; visualization: Visualization; framePublicAPI: FramePublicAPI; - activeDatasourceId: string; } export function getSavedObjectFormat({ @@ -24,7 +23,6 @@ export function getSavedObjectFormat({ state, visualization, framePublicAPI, - activeDatasourceId, }: Props): Document { const expression = buildExpression({ visualization, @@ -53,7 +51,6 @@ export function getSavedObjectFormat({ type: 'lens', visualizationType: state.visualization.activeId, expression: expression ? toExpression(expression) : '', - activeDatasourceId, state: { datasourceStates, datasourceMetaData: { diff --git a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/state_management.test.ts b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/state_management.test.ts index 22ff323507487..8320a79195442 100644 --- a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/state_management.test.ts +++ b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/state_management.test.ts @@ -45,7 +45,6 @@ describe('editor_frame state management', () => { const initialState = getInitialState({ ...props, doc: { - activeDatasourceId: 'testDatasource', expression: '', state: { datasourceStates: { @@ -370,7 +369,6 @@ describe('editor_frame state management', () => { doc: { id: 'b', expression: '', - activeDatasourceId: 'a', state: { datasourceMetaData: { filterableIndexPatterns: [] }, datasourceStates: { a: { foo: 'c' } }, diff --git a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/state_management.ts b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/state_management.ts index 6f76cc15dafed..60e053bf19133 100644 --- a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/state_management.ts +++ b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/state_management.ts @@ -64,6 +64,21 @@ export type Action = newDatasourceId: string; }; +export function getActiveDatasourceIdFromDoc(doc?: Document) { + if (!doc) { + return null; + } + + const [initialDatasourceId] = Object.keys(doc.state.datasourceStates); + return initialDatasourceId || null; +} + +function getInitialDatasourceId(props: EditorFrameProps) { + return props.initialDatasourceId + ? props.initialDatasourceId + : getActiveDatasourceIdFromDoc(props.doc); +} + export const getInitialState = (props: EditorFrameProps): EditorFrameState => { const datasourceStates: EditorFrameState['datasourceStates'] = {}; @@ -81,7 +96,7 @@ export const getInitialState = (props: EditorFrameProps): EditorFrameState => { return { title: i18n.translate('xpack.lens.chartTitle', { defaultMessage: 'New visualization' }), datasourceStates, - activeDatasourceId: props.initialDatasourceId ? props.initialDatasourceId : null, + activeDatasourceId: getInitialDatasourceId(props), visualization: { state: null, activeId: props.initialVisualizationId, @@ -124,7 +139,7 @@ export const reducer = (state: EditorFrameState, action: Action): EditorFrameSta }), {} ), - activeDatasourceId: action.doc.activeDatasourceId, + activeDatasourceId: getActiveDatasourceIdFromDoc(action.doc), visualization: { ...state.visualization, activeId: action.doc.visualizationType, diff --git a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/embeddable/embeddable.test.tsx b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/embeddable/embeddable.test.tsx index 813c15b360952..2009eb232562b 100644 --- a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/embeddable/embeddable.test.tsx +++ b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/embeddable/embeddable.test.tsx @@ -17,7 +17,6 @@ jest.mock('../../../../../../../src/legacy/ui/public/inspector', () => ({ })); const savedVis: Document = { - activeDatasourceId: '', expression: 'my | expression', state: { visualization: {}, diff --git a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/plugin.tsx b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/plugin.tsx index 08a760671555e..62339a4cc3afc 100644 --- a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/plugin.tsx +++ b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/plugin.tsx @@ -19,6 +19,7 @@ import { Datasource, Visualization, EditorFrameSetup, EditorFrameInstance } from import { EditorFrame } from './editor_frame'; import { mergeTables } from './merge_tables'; import { EmbeddableFactory } from './embeddable/embeddable_factory'; +import { getActiveDatasourceIdFromDoc } from './editor_frame/state_management'; export interface EditorFrameSetupPlugins { data: typeof data; @@ -67,7 +68,7 @@ export class EditorFramePlugin { onError={onError} datasourceMap={this.datasources} visualizationMap={this.visualizations} - initialDatasourceId={(doc && doc.activeDatasourceId) || firstDatasourceId || null} + initialDatasourceId={getActiveDatasourceIdFromDoc(doc) || firstDatasourceId || null} initialVisualizationId={ (doc && doc.visualizationType) || firstVisualizationId || null } diff --git a/x-pack/legacy/plugins/lens/public/persistence/saved_object_store.test.ts b/x-pack/legacy/plugins/lens/public/persistence/saved_object_store.test.ts index 12dff938d9be1..515d008d82586 100644 --- a/x-pack/legacy/plugins/lens/public/persistence/saved_object_store.test.ts +++ b/x-pack/legacy/plugins/lens/public/persistence/saved_object_store.test.ts @@ -27,7 +27,6 @@ describe('LensStore', () => { title: 'Hello', visualizationType: 'bar', expression: '', - activeDatasourceId: 'indexpattern', state: { datasourceMetaData: { filterableIndexPatterns: [], @@ -46,7 +45,6 @@ describe('LensStore', () => { title: 'Hello', visualizationType: 'bar', expression: '', - activeDatasourceId: 'indexpattern', state: { datasourceMetaData: { filterableIndexPatterns: [], @@ -64,10 +62,8 @@ describe('LensStore', () => { expect(client.create).toHaveBeenCalledWith('lens', { title: 'Hello', visualizationType: 'bar', - expression: '', - activeDatasourceId: 'indexpattern', - state: JSON.stringify({ + state: { datasourceMetaData: { filterableIndexPatterns: [] }, datasourceStates: { indexpattern: { type: 'index_pattern', indexPattern: '.kibana_test' }, @@ -75,7 +71,7 @@ describe('LensStore', () => { visualization: { x: 'foo', y: 'baz' }, query: { query: '', language: 'lucene' }, filters: [], - }), + }, }); }); @@ -86,7 +82,6 @@ describe('LensStore', () => { title: 'Even the very wise cannot see all ends.', visualizationType: 'line', expression: '', - activeDatasourceId: 'indexpattern', state: { datasourceMetaData: { filterableIndexPatterns: [] }, datasourceStates: { indexpattern: { type: 'index_pattern', indexPattern: 'lotr' } }, @@ -101,7 +96,6 @@ describe('LensStore', () => { title: 'Even the very wise cannot see all ends.', visualizationType: 'line', expression: '', - activeDatasourceId: 'indexpattern', state: { datasourceMetaData: { filterableIndexPatterns: [] }, datasourceStates: { indexpattern: { type: 'index_pattern', indexPattern: 'lotr' } }, @@ -116,46 +110,18 @@ describe('LensStore', () => { title: 'Even the very wise cannot see all ends.', visualizationType: 'line', expression: '', - activeDatasourceId: 'indexpattern', - state: JSON.stringify({ + state: { datasourceMetaData: { filterableIndexPatterns: [] }, datasourceStates: { indexpattern: { type: 'index_pattern', indexPattern: 'lotr' } }, visualization: { gear: ['staff', 'pointy hat'] }, query: { query: '', language: 'lucene' }, filters: [], - }), + }, }); }); }); describe('load', () => { - test('parses the visState', async () => { - const { client, store } = testStore(); - client.get = jest.fn(async () => ({ - id: 'Paul', - type: 'lens', - attributes: { - title: 'Hope clouds observation.', - visualizationType: 'dune', - state: '{ "datasource": { "giantWorms": true } }', - }, - })); - const doc = await store.load('Paul'); - - expect(doc).toEqual({ - id: 'Paul', - type: 'lens', - title: 'Hope clouds observation.', - visualizationType: 'dune', - state: { - datasource: { giantWorms: true }, - }, - }); - - expect(client.get).toHaveBeenCalledTimes(1); - expect(client.get).toHaveBeenCalledWith('lens', 'Paul'); - }); - test('throws if an error is returned', async () => { const { client, store } = testStore(); client.get = jest.fn(async () => ({ diff --git a/x-pack/legacy/plugins/lens/public/persistence/saved_object_store.ts b/x-pack/legacy/plugins/lens/public/persistence/saved_object_store.ts index 681042ed34ffe..5fa7e3f0aca4a 100644 --- a/x-pack/legacy/plugins/lens/public/persistence/saved_object_store.ts +++ b/x-pack/legacy/plugins/lens/public/persistence/saved_object_store.ts @@ -14,7 +14,6 @@ export interface Document { type?: string; visualizationType: string | null; title: string; - activeDatasourceId: string; expression: string; state: { datasourceMetaData: { @@ -62,18 +61,14 @@ export class SavedObjectIndexStore implements SavedObjectStore { async save(vis: Document) { const { id, type, ...rest } = vis; - const attributes = { - ...rest, - state: JSON.stringify(rest.state), - }; + // TODO: SavedObjectAttributes should support this kind of object, + // remove this workaround when SavedObjectAttributes is updated. + const attributes = (rest as unknown) as SavedObjectAttributes; const result = await (id ? this.client.update(DOC_TYPE, id, attributes) : this.client.create(DOC_TYPE, attributes)); - return { - ...vis, - id: result.id, - }; + return { ...vis, id: result.id }; } async load(id: string): Promise { @@ -87,7 +82,6 @@ export class SavedObjectIndexStore implements SavedObjectStore { ...attributes, id, type, - state: JSON.parse(((attributes as unknown) as { state: string }).state as string), } as Document; } }