From 8f61db1026407d4eefcad175608b633d9a6cab5f Mon Sep 17 00:00:00 2001 From: Wylie Conlon Date: Tue, 3 Sep 2019 17:51:58 -0400 Subject: [PATCH 1/9] [lens] Move around new platform usages --- .../plugins/lens/public/app_plugin/app.test.tsx | 5 +++++ x-pack/legacy/plugins/lens/public/app_plugin/app.tsx | 4 ++++ .../legacy/plugins/lens/public/app_plugin/plugin.tsx | 1 + .../editor_frame/expression_helpers.ts | 2 +- .../embeddable/embeddable.test.tsx | 2 +- .../editor_frame_plugin/embeddable/embeddable.tsx | 2 +- .../embeddable/expression_wrapper.tsx | 2 +- .../dimension_panel/dimension_panel.test.tsx | 3 ++- .../dimension_panel/dimension_panel.tsx | 3 ++- .../dimension_panel/popover_editor.tsx | 1 + .../public/indexpattern_plugin/indexpattern.test.ts | 2 ++ .../lens/public/indexpattern_plugin/indexpattern.tsx | 8 +++++++- .../indexpattern_suggestions.test.tsx | 2 ++ .../operations/definitions/date_histogram.test.tsx | 8 +++++++- .../operations/definitions/date_histogram.tsx | 6 ++++-- .../operations/definitions/filter_ratio.test.tsx | 6 +++++- .../operations/definitions/filter_ratio.tsx | 12 +++++++++++- .../operations/definitions/index.ts | 3 ++- .../operations/definitions/terms.test.tsx | 9 ++++++++- .../operations/definitions/terms.tsx | 6 ++++-- .../lens/public/indexpattern_plugin/plugin.tsx | 1 + 21 files changed, 72 insertions(+), 16 deletions(-) diff --git a/x-pack/legacy/plugins/lens/public/app_plugin/app.test.tsx b/x-pack/legacy/plugins/lens/public/app_plugin/app.test.tsx index 3b30c040985c4..b735f2e82b87c 100644 --- a/x-pack/legacy/plugins/lens/public/app_plugin/app.test.tsx +++ b/x-pack/legacy/plugins/lens/public/app_plugin/app.test.tsx @@ -14,6 +14,7 @@ import { Storage } from 'ui/storage'; import { Document, SavedObjectStore } from '../persistence'; import { mount } from 'enzyme'; import { QueryBar } from '../../../../../../src/legacy/core_plugins/data/public/query'; +import { SavedObjectsClientContract } from 'src/core/public'; jest.mock('../../../../../../src/legacy/core_plugins/data/public/query', () => ({ QueryBar: jest.fn(() => null), @@ -23,6 +24,7 @@ jest.mock('ui/new_platform'); jest.mock('ui/notify'); jest.mock('ui/chrome'); jest.mock('../persistence'); +jest.mock('src/core/public'); const waitForPromises = () => new Promise(resolve => setTimeout(resolve)); @@ -40,6 +42,7 @@ function makeDefaultArgs(): jest.Mocked<{ docId?: string; docStorage: SavedObjectStore; redirectTo: (id?: string) => void; + savedObjectsClient: SavedObjectsClientContract; }> { return ({ editorFrame: createMockFrame(), @@ -67,6 +70,7 @@ function makeDefaultArgs(): jest.Mocked<{ }, QueryBar: jest.fn(() =>
), redirectTo: jest.fn(id => {}), + savedObjectsClient: jest.fn(), } as unknown) as jest.Mocked<{ editorFrame: EditorFrameInstance; chrome: Chrome; @@ -74,6 +78,7 @@ function makeDefaultArgs(): jest.Mocked<{ docId?: string; docStorage: SavedObjectStore; redirectTo: (id?: string) => void; + savedObjectsClient: SavedObjectsClientContract; }>; } diff --git a/x-pack/legacy/plugins/lens/public/app_plugin/app.tsx b/x-pack/legacy/plugins/lens/public/app_plugin/app.tsx index 2b768e621c17d..48c646eb23191 100644 --- a/x-pack/legacy/plugins/lens/public/app_plugin/app.tsx +++ b/x-pack/legacy/plugins/lens/public/app_plugin/app.tsx @@ -12,6 +12,7 @@ import { EuiLink, EuiFlexGroup, EuiFlexItem } from '@elastic/eui'; import { Storage } from 'ui/storage'; import { toastNotifications } from 'ui/notify'; import { Chrome } from 'ui/chrome'; +import { SavedObjectsClientContract } from 'src/core/public'; import { Query, QueryBar } from '../../../../../../src/legacy/core_plugins/data/public/query'; import { Document, SavedObjectStore } from '../persistence'; import { EditorFrameInstance } from '../types'; @@ -55,6 +56,7 @@ export function App({ docId, docStorage, redirectTo, + savedObjectsClient, }: { editorFrame: EditorFrameInstance; chrome: Chrome; @@ -62,6 +64,7 @@ export function App({ docId?: string; docStorage: SavedObjectStore; redirectTo: (id?: string) => void; + savedObjectsClient: SavedObjectsClientContract; }) { const uiSettings = chrome.getUiSettingsClient(); const timeDefaults = uiSettings.get('timepicker:timeDefaults'); @@ -211,6 +214,7 @@ export function App({ state.localQueryBarState.dateRange && state.localQueryBarState.dateRange.to } uiSettings={uiSettings} + savedObjectsClient={savedObjectsClient} />
diff --git a/x-pack/legacy/plugins/lens/public/app_plugin/plugin.tsx b/x-pack/legacy/plugins/lens/public/app_plugin/plugin.tsx index 07bd55cbd4e93..e6af305f56f7c 100644 --- a/x-pack/legacy/plugins/lens/public/app_plugin/plugin.tsx +++ b/x-pack/legacy/plugins/lens/public/app_plugin/plugin.tsx @@ -49,6 +49,7 @@ export class AppPlugin { editorFrame={this.instance!} chrome={chrome} store={new Storage(localStorage)} + savedObjectsClient={chrome.getSavedObjectsClient()} docId={routeProps.match.params.id} docStorage={store} redirectTo={id => { diff --git a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/expression_helpers.ts b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/expression_helpers.ts index 1b71f28260088..537bf12cb79e8 100644 --- a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/expression_helpers.ts +++ b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/expression_helpers.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { TimeRange } from 'ui/timefilter/time_history'; +import { TimeRange } from 'ui/timefilter'; import { Query } from 'src/legacy/core_plugins/data/public'; import { Filter } from '@kbn/es-query'; import { Ast, fromExpression, ExpressionFunctionAST } from '@kbn/interpreter/common'; 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 2009eb232562b..e9f3af8e63ff1 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 @@ -5,7 +5,7 @@ */ import { Embeddable } from './embeddable'; -import { TimeRange } from 'ui/timefilter/time_history'; +import { TimeRange } from 'ui/timefilter'; import { Query, ExpressionRendererProps } from 'src/legacy/core_plugins/data/public'; import { Filter } from '@kbn/es-query'; import { Document } from '../../persistence'; diff --git a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/embeddable/embeddable.tsx b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/embeddable/embeddable.tsx index f50ee70731642..23301bf68c570 100644 --- a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/embeddable/embeddable.tsx +++ b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/embeddable/embeddable.tsx @@ -8,7 +8,7 @@ import _ from 'lodash'; import React from 'react'; import { render, unmountComponentAtNode } from 'react-dom'; -import { TimeRange } from 'ui/timefilter/time_history'; +import { TimeRange } from 'ui/timefilter'; import { Query, StaticIndexPattern, ExpressionRenderer } from 'src/legacy/core_plugins/data/public'; import { Filter } from '@kbn/es-query'; import { Subscription } from 'rxjs'; diff --git a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/embeddable/expression_wrapper.tsx b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/embeddable/expression_wrapper.tsx index d5ba7bcd39118..024f16ce097d8 100644 --- a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/embeddable/expression_wrapper.tsx +++ b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/embeddable/expression_wrapper.tsx @@ -10,7 +10,7 @@ import React, { useState, useEffect } from 'react'; import { I18nProvider } from '@kbn/i18n/react'; import { FormattedMessage } from '@kbn/i18n/react'; import { EuiFlexGroup, EuiFlexItem, EuiText, EuiIcon } from '@elastic/eui'; -import { TimeRange } from 'ui/timefilter/time_history'; +import { TimeRange } from 'ui/timefilter'; import { Query } from 'src/legacy/core_plugins/data/public'; import { Filter } from '@kbn/es-query'; import { ExpressionRenderer } from '../../../../../../../src/legacy/core_plugins/data/public'; diff --git a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/dimension_panel/dimension_panel.test.tsx b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/dimension_panel/dimension_panel.test.tsx index 1e801467f3f94..2351bd7952a3d 100644 --- a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/dimension_panel/dimension_panel.test.tsx +++ b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/dimension_panel/dimension_panel.test.tsx @@ -14,7 +14,7 @@ import { IndexPatternDimensionPanel, IndexPatternDimensionPanelProps } from './d import { DropHandler, DragContextState } from '../../drag_drop'; import { createMockedDragDropContext } from '../mocks'; import { mountWithIntl as mount, shallowWithIntl as shallow } from 'test_utils/enzyme_helpers'; -import { UiSettingsClientContract } from 'src/core/public'; +import { UiSettingsClientContract, SavedObjectsClientContract } from 'src/core/public'; import { Storage } from 'ui/storage'; jest.mock('ui/new_platform'); @@ -121,6 +121,7 @@ describe('IndexPatternDimensionPanel', () => { filterOperations: () => true, storage: {} as Storage, uiSettings: {} as UiSettingsClientContract, + savedObjectsClient: {} as SavedObjectsClientContract, }; jest.clearAllMocks(); diff --git a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/dimension_panel/dimension_panel.tsx b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/dimension_panel/dimension_panel.tsx index 0076a9f599bb9..9158806138c2e 100644 --- a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/dimension_panel/dimension_panel.tsx +++ b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/dimension_panel/dimension_panel.tsx @@ -9,7 +9,7 @@ import React, { memo, useMemo } from 'react'; import { EuiButtonIcon } from '@elastic/eui'; import { Storage } from 'ui/storage'; import { i18n } from '@kbn/i18n'; -import { UiSettingsClientContract } from 'src/core/public'; +import { UiSettingsClientContract, SavedObjectsClientContract } from 'src/core/public'; import { DatasourceDimensionPanelProps, StateSetter } from '../../types'; import { IndexPatternColumn, @@ -30,6 +30,7 @@ export type IndexPatternDimensionPanelProps = DatasourceDimensionPanelProps & { dragDropContext: DragContextState; uiSettings: UiSettingsClientContract; storage: Storage; + savedObjectsClient: SavedObjectsClientContract; layerId: string; }; diff --git a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/dimension_panel/popover_editor.tsx b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/dimension_panel/popover_editor.tsx index 7eb03152b341f..eaec3ea8b6c5b 100644 --- a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/dimension_panel/popover_editor.tsx +++ b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/dimension_panel/popover_editor.tsx @@ -355,6 +355,7 @@ export function PopoverEditor(props: PopoverEditorProps) { currentColumn={state.layers[layerId].columns[columnId]} storage={props.storage} uiSettings={props.uiSettings} + savedObjectsClient={props.savedObjectsClient} layerId={layerId} /> )} diff --git a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/indexpattern.test.ts b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/indexpattern.test.ts index d69a3827a43c9..0aa21e867fc30 100644 --- a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/indexpattern.test.ts +++ b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/indexpattern.test.ts @@ -9,6 +9,7 @@ import { data as dataMock } from '../../../../../../src/legacy/core_plugins/data import { Storage } from 'ui/storage'; import { functionsRegistry } from '../../../../../../src/legacy/core_plugins/interpreter/public/registries'; import { toastNotifications as notificationsMock } from 'ui/notify'; +import { SavedObjectsClientContract } from 'src/core/public'; import { getIndexPatternDatasource, IndexPatternPersistedState, @@ -140,6 +141,7 @@ describe('IndexPattern Data Source', () => { interpreter: { functionsRegistry }, toastNotifications: notificationsMock, data: dataMock, + savedObjectsClient: {} as SavedObjectsClientContract, }); persistedState = { diff --git a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/indexpattern.tsx b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/indexpattern.tsx index c21f6a68c0416..78174e8d96f63 100644 --- a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/indexpattern.tsx +++ b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/indexpattern.tsx @@ -8,6 +8,7 @@ import _ from 'lodash'; import React from 'react'; import { render } from 'react-dom'; import { I18nProvider } from '@kbn/i18n/react'; +import { SavedObjectsClientContract } from 'src/core/public'; import { Storage } from 'ui/storage'; import { DatasourceDimensionPanelProps, @@ -152,7 +153,11 @@ export function getIndexPatternDatasource({ chrome, toastNotifications, storage, -}: IndexPatternDatasourcePluginPlugins & { storage: Storage }) { + savedObjectsClient, +}: IndexPatternDatasourcePluginPlugins & { + storage: Storage; + savedObjectsClient: SavedObjectsClientContract; +}) { const uiSettings = chrome.getUiSettingsClient(); // Not stateful. State is persisted to the frame const indexPatternDatasource: Datasource = { @@ -267,6 +272,7 @@ export function getIndexPatternDatasource({ setState={setState} uiSettings={uiSettings} storage={storage} + savedObjectsClient={savedObjectsClient} layerId={props.layerId} {...props} /> diff --git a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/indexpattern_suggestions.test.tsx b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/indexpattern_suggestions.test.tsx index ae26633a848ea..1373d7dda4f3e 100644 --- a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/indexpattern_suggestions.test.tsx +++ b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/indexpattern_suggestions.test.tsx @@ -8,6 +8,7 @@ import chromeMock from 'ui/chrome'; import { data as dataMock } from '../../../../../../src/legacy/core_plugins/data/public/setup'; import { functionsRegistry } from '../../../../../../src/legacy/core_plugins/interpreter/public/registries'; import { toastNotifications as notificationsMock } from 'ui/notify'; +import { SavedObjectsClientContract } from 'src/core/public'; import { getIndexPatternDatasource, IndexPatternPersistedState, @@ -140,6 +141,7 @@ describe('IndexPattern Data Source suggestions', () => { interpreter: { functionsRegistry }, toastNotifications: notificationsMock, data: dataMock, + savedObjectsClient: {} as SavedObjectsClientContract, }); persistedState = { diff --git a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/operations/definitions/date_histogram.test.tsx b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/operations/definitions/date_histogram.test.tsx index 4b8b556927052..76f67bcecbf47 100644 --- a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/operations/definitions/date_histogram.test.tsx +++ b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/operations/definitions/date_histogram.test.tsx @@ -10,7 +10,7 @@ import { dateHistogramOperation } from '.'; import { shallow } from 'enzyme'; import { IndexPatternPrivateState } from '../../indexpattern'; import { EuiRange, EuiSwitch } from '@elastic/eui'; -import { UiSettingsClientContract } from 'src/core/public'; +import { UiSettingsClientContract, SavedObjectsClientContract } from 'src/core/public'; import { Storage } from 'ui/storage'; import { createMockedIndexPattern } from '../../mocks'; @@ -323,6 +323,7 @@ describe('date_histogram', () => { currentColumn={state.layers.first.columns.col1 as DateHistogramIndexPatternColumn} storage={{} as Storage} uiSettings={{} as UiSettingsClientContract} + savedObjectsClient={{} as SavedObjectsClientContract} /> ); @@ -340,6 +341,7 @@ describe('date_histogram', () => { layerId="second" storage={{} as Storage} uiSettings={{} as UiSettingsClientContract} + savedObjectsClient={{} as SavedObjectsClientContract} /> ); @@ -356,6 +358,7 @@ describe('date_histogram', () => { layerId="third" storage={{} as Storage} uiSettings={{} as UiSettingsClientContract} + savedObjectsClient={{} as SavedObjectsClientContract} /> ); expect(instance.find(EuiRange).exists()).toBe(false); @@ -373,6 +376,7 @@ describe('date_histogram', () => { currentColumn={state.layers.third.columns.col1 as DateHistogramIndexPatternColumn} storage={{} as Storage} uiSettings={{} as UiSettingsClientContract} + savedObjectsClient={{} as SavedObjectsClientContract} /> ); instance.find(EuiSwitch).prop('onChange')!({ @@ -394,6 +398,7 @@ describe('date_histogram', () => { currentColumn={state.layers.first.columns.col1 as DateHistogramIndexPatternColumn} storage={{} as Storage} uiSettings={{} as UiSettingsClientContract} + savedObjectsClient={{} as SavedObjectsClientContract} /> ); @@ -452,6 +457,7 @@ describe('date_histogram', () => { currentColumn={state.layers.first.columns.col1 as DateHistogramIndexPatternColumn} storage={{} as Storage} uiSettings={{} as UiSettingsClientContract} + savedObjectsClient={{} as SavedObjectsClientContract} /> ); diff --git a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/operations/definitions/date_histogram.tsx b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/operations/definitions/date_histogram.tsx index a90e4e7a3068d..9558a141ad7a0 100644 --- a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/operations/definitions/date_histogram.tsx +++ b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/operations/definitions/date_histogram.tsx @@ -228,14 +228,16 @@ export const dateHistogramOperation: OperationDefinition) => + onChange={( + e: React.ChangeEvent | React.MouseEvent + ) => setState( updateColumnParam({ state, layerId, currentColumn, paramName: 'interval', - value: numericToInterval(Number(e.target.value)), + value: numericToInterval(Number((e.target as HTMLInputElement).value)), }) ) } diff --git a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/operations/definitions/filter_ratio.test.tsx b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/operations/definitions/filter_ratio.test.tsx index 8864e959977a8..e9da64c44fcbd 100644 --- a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/operations/definitions/filter_ratio.test.tsx +++ b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/operations/definitions/filter_ratio.test.tsx @@ -11,7 +11,7 @@ import { FilterRatioIndexPatternColumn } from './filter_ratio'; import { filterRatioOperation } from '.'; import { IndexPatternPrivateState } from '../../indexpattern'; import { Storage } from 'ui/storage'; -import { UiSettingsClientContract } from 'src/core/public'; +import { UiSettingsClientContract, SavedObjectsClientContract } from 'src/core/public'; import { QueryBarInput } from '../../../../../../../../src/legacy/core_plugins/data/public/query'; import { createMockedIndexPattern } from '../../mocks'; @@ -108,6 +108,7 @@ describe('filter_ratio', () => { currentColumn={state.layers.first.columns.col1 as FilterRatioIndexPatternColumn} storage={storageMock} uiSettings={{} as UiSettingsClientContract} + savedObjectsClient={{} as SavedObjectsClientContract} /> ); }).not.toThrow(); @@ -123,6 +124,7 @@ describe('filter_ratio', () => { currentColumn={state.layers.first.columns.col1 as FilterRatioIndexPatternColumn} storage={storageMock} uiSettings={{} as UiSettingsClientContract} + savedObjectsClient={{} as SavedObjectsClientContract} /> ); @@ -141,6 +143,7 @@ describe('filter_ratio', () => { currentColumn={state.layers.first.columns.col1 as FilterRatioIndexPatternColumn} storage={storageMock} uiSettings={{} as UiSettingsClientContract} + savedObjectsClient={{} as SavedObjectsClientContract} /> ); @@ -180,6 +183,7 @@ describe('filter_ratio', () => { currentColumn={state.layers.first.columns.col1 as FilterRatioIndexPatternColumn} storage={storageMock} uiSettings={{} as UiSettingsClientContract} + savedObjectsClient={{} as SavedObjectsClientContract} /> ); diff --git a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/operations/definitions/filter_ratio.tsx b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/operations/definitions/filter_ratio.tsx index 0d50801708b3e..de6e34ec3d941 100644 --- a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/operations/definitions/filter_ratio.tsx +++ b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/operations/definitions/filter_ratio.tsx @@ -77,7 +77,15 @@ export const filterRatioOperation: OperationDefinition { + paramEditor: ({ + state, + setState, + currentColumn, + uiSettings, + storage, + layerId, + savedObjectsClient, + }) => { const [hasDenominator, setDenominator] = useState(false); return ( @@ -94,6 +102,7 @@ export const filterRatioOperation: OperationDefinition { setState( updateColumnParam({ @@ -121,6 +130,7 @@ export const filterRatioOperation: OperationDefinition { setState( updateColumnParam({ diff --git a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/operations/definitions/index.ts b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/operations/definitions/index.ts index 13020605d3606..9f82d0f2a1763 100644 --- a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/operations/definitions/index.ts +++ b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/operations/definitions/index.ts @@ -5,7 +5,7 @@ */ import { Storage } from 'ui/storage'; -import { UiSettingsClientContract } from 'src/core/public'; +import { UiSettingsClientContract, SavedObjectsClientContract } from 'src/core/public'; import { termsOperation } from './terms'; import { minOperation, averageOperation, sumOperation, maxOperation } from './metrics'; import { dateHistogramOperation } from './date_histogram'; @@ -46,6 +46,7 @@ export interface ParamEditorProps { layerId: string; uiSettings: UiSettingsClientContract; storage: Storage; + savedObjectsClient: SavedObjectsClientContract; } interface BaseOperationDefinitionProps { diff --git a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/operations/definitions/terms.test.tsx b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/operations/definitions/terms.test.tsx index 56b15eaaa47db..30b8bfb0ec5bd 100644 --- a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/operations/definitions/terms.test.tsx +++ b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/operations/definitions/terms.test.tsx @@ -8,7 +8,7 @@ import React from 'react'; import { shallow } from 'enzyme'; import { IndexPatternPrivateState } from '../../indexpattern'; import { EuiRange, EuiSelect } from '@elastic/eui'; -import { UiSettingsClientContract } from 'src/core/public'; +import { UiSettingsClientContract, SavedObjectsClientContract } from 'src/core/public'; import { Storage } from 'ui/storage'; import { createMockedIndexPattern } from '../../mocks'; import { TermsIndexPatternColumn } from './terms'; @@ -312,6 +312,7 @@ describe('terms', () => { layerId="first" storage={{} as Storage} uiSettings={{} as UiSettingsClientContract} + savedObjectsClient={{} as SavedObjectsClientContract} /> ); @@ -358,6 +359,7 @@ describe('terms', () => { currentColumn={state.layers.first.columns.col1 as TermsIndexPatternColumn} storage={{} as Storage} uiSettings={{} as UiSettingsClientContract} + savedObjectsClient={{} as SavedObjectsClientContract} /> ); @@ -377,6 +379,7 @@ describe('terms', () => { layerId="first" storage={{} as Storage} uiSettings={{} as UiSettingsClientContract} + savedObjectsClient={{} as SavedObjectsClientContract} /> ); @@ -423,6 +426,7 @@ describe('terms', () => { currentColumn={state.layers.first.columns.col1 as TermsIndexPatternColumn} storage={{} as Storage} uiSettings={{} as UiSettingsClientContract} + savedObjectsClient={{} as SavedObjectsClientContract} /> ); @@ -445,6 +449,7 @@ describe('terms', () => { currentColumn={state.layers.first.columns.col1 as TermsIndexPatternColumn} storage={{} as Storage} uiSettings={{} as UiSettingsClientContract} + savedObjectsClient={{} as SavedObjectsClientContract} /> ); @@ -488,6 +493,7 @@ describe('terms', () => { currentColumn={state.layers.first.columns.col1 as TermsIndexPatternColumn} storage={{} as Storage} uiSettings={{} as UiSettingsClientContract} + savedObjectsClient={{} as SavedObjectsClientContract} /> ); @@ -505,6 +511,7 @@ describe('terms', () => { currentColumn={state.layers.first.columns.col1 as TermsIndexPatternColumn} storage={{} as Storage} uiSettings={{} as UiSettingsClientContract} + savedObjectsClient={{} as SavedObjectsClientContract} /> ); diff --git a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/operations/definitions/terms.tsx b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/operations/definitions/terms.tsx index 6f1a1a3b5471c..52b27f85fb495 100644 --- a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/operations/definitions/terms.tsx +++ b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/operations/definitions/terms.tsx @@ -181,14 +181,16 @@ export const termsOperation: OperationDefinition = { step={1} value={currentColumn.params.size} showInput - onChange={(e: React.ChangeEvent) => + onChange={( + e: React.ChangeEvent | React.MouseEvent + ) => setState( updateColumnParam({ state, layerId, currentColumn, paramName: 'size', - value: Number(e.target.value), + value: Number((e.target as HTMLInputElement).value), }) ) } diff --git a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/plugin.tsx b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/plugin.tsx index 2f1c23b1bf9dc..63e9642636063 100644 --- a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/plugin.tsx +++ b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/plugin.tsx @@ -50,6 +50,7 @@ class IndexPatternDatasourcePlugin { toastNotifications: toast, data, storage: new Storage(localStorage), + savedObjectsClient: chrome.getSavedObjectsClient(), }); } From b0453392264d73b5e1b38a63f5a6c5bacbba4b96 Mon Sep 17 00:00:00 2001 From: Wylie Conlon Date: Wed, 4 Sep 2019 14:32:55 -0400 Subject: [PATCH 2/9] Update interpreter usage --- .../kbn-interpreter/src/common/index.d.ts | 2 +- .../expressions/expression_renderer.tsx | 2 +- .../public/expressions/expression_runner.ts | 84 ------------------- .../data/public/expressions/lib/_types.ts | 4 +- .../expression.tsx | 4 +- .../datatable_visualization_plugin/plugin.tsx | 25 ++---- .../embeddable/embeddable.test.tsx | 2 +- .../embeddable/expression_wrapper.tsx | 2 +- .../lens/public/editor_frame_plugin/mocks.tsx | 10 ++- .../editor_frame_plugin/plugin.test.tsx | 5 +- .../public/editor_frame_plugin/plugin.tsx | 17 ++-- .../plugins/lens/public/interpreter_types.ts | 32 ------- .../public/xy_visualization_plugin/plugin.tsx | 37 ++++---- .../xy_visualization_plugin/xy_expression.tsx | 4 +- 14 files changed, 53 insertions(+), 177 deletions(-) delete mode 100644 src/legacy/core_plugins/data/public/expressions/expression_runner.ts delete mode 100644 x-pack/legacy/plugins/lens/public/interpreter_types.ts diff --git a/packages/kbn-interpreter/src/common/index.d.ts b/packages/kbn-interpreter/src/common/index.d.ts index 7201ccbb35635..bf03795d0a15c 100644 --- a/packages/kbn-interpreter/src/common/index.d.ts +++ b/packages/kbn-interpreter/src/common/index.d.ts @@ -19,4 +19,4 @@ export { Registry } from './lib/registry'; -export { fromExpression, toExpression, Ast } from './lib/ast'; +export { fromExpression, toExpression, Ast, ExpressionFunctionAST } from './lib/ast'; diff --git a/src/legacy/core_plugins/data/public/expressions/expression_renderer.tsx b/src/legacy/core_plugins/data/public/expressions/expression_renderer.tsx index e5358acc1c05c..11921ca9cf269 100644 --- a/src/legacy/core_plugins/data/public/expressions/expression_renderer.tsx +++ b/src/legacy/core_plugins/data/public/expressions/expression_renderer.tsx @@ -26,7 +26,7 @@ import { IExpressionLoader, ExpressionLoader } from './lib/loader'; // Accept all options of the runner as props except for the // dom element which is provided by the component itself export interface ExpressionRendererProps extends IExpressionLoaderParams { - className: 'string'; + className: string; expression: string | ExpressionAST; /** * If an element is specified, but the response of the expression run can't be rendered diff --git a/src/legacy/core_plugins/data/public/expressions/expression_runner.ts b/src/legacy/core_plugins/data/public/expressions/expression_runner.ts deleted file mode 100644 index ac087e2f530f0..0000000000000 --- a/src/legacy/core_plugins/data/public/expressions/expression_runner.ts +++ /dev/null @@ -1,84 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import { Ast, fromExpression } from '@kbn/interpreter/common'; - -import { RequestAdapter, DataAdapter } from 'ui/inspector/adapters'; -import { RenderFunctionsRegistry, Interpreter, Result } from './expressions_service'; - -export interface ExpressionRunnerOptions { - // TODO use the real types here once they are ready - context?: object; - getInitialContext?: () => object; - element?: Element; - /** - * If an element is specified, but the response of the expression run can't be rendered - * because it isn't a valid response or the specified renderer isn't available, - * this callback is called with the given result. - */ - onRenderFailure?: (result: Result) => void; -} - -export type ExpressionRunner = ( - expression: string | Ast, - options: ExpressionRunnerOptions -) => Promise; - -export const createRunFn = ( - renderersRegistry: RenderFunctionsRegistry, - interpreterPromise: Promise -): ExpressionRunner => async ( - expressionOrAst, - { element, context, getInitialContext, onRenderFailure } -) => { - // TODO: make interpreter initialization synchronous to avoid this - const interpreter = await interpreterPromise; - const ast = - typeof expressionOrAst === 'string' ? fromExpression(expressionOrAst) : expressionOrAst; - - const response = await interpreter.interpretAst(ast, context || { type: 'null' }, { - getInitialContext: getInitialContext || (() => ({})), - inspectorAdapters: { - // TODO connect real adapters - requests: new RequestAdapter(), - data: new DataAdapter(), - }, - }); - - if (response.type === 'error') { - throw response; - } - - if (element) { - if (response.type === 'render' && response.as && renderersRegistry.get(response.as) !== null) { - renderersRegistry.get(response.as).render(element, response.value, { - onDestroy: fn => { - // TODO implement - }, - done: () => { - // TODO implement - }, - }); - } else { - throw response; - } - } - - return response; -}; diff --git a/src/legacy/core_plugins/data/public/expressions/lib/_types.ts b/src/legacy/core_plugins/data/public/expressions/lib/_types.ts index b3185fda2c178..e458260432eaa 100644 --- a/src/legacy/core_plugins/data/public/expressions/lib/_types.ts +++ b/src/legacy/core_plugins/data/public/expressions/lib/_types.ts @@ -68,13 +68,13 @@ export interface IInterpreterRenderHandlers { event: (event: event) => void; } -export interface IInterpreterRenderFunction { +export interface IInterpreterRenderFunction { name: string; displayName: string; help: string; validate: () => void; reuseDomNode: boolean; - render: (domNode: Element, data: unknown, handlers: IInterpreterRenderHandlers) => void; + render: (domNode: Element, data: T, handlers: IInterpreterRenderHandlers) => void | Promise; } export interface IInterpreter { diff --git a/x-pack/legacy/plugins/lens/public/datatable_visualization_plugin/expression.tsx b/x-pack/legacy/plugins/lens/public/datatable_visualization_plugin/expression.tsx index 0e53ee59761f5..353bb85dd32e1 100644 --- a/x-pack/legacy/plugins/lens/public/datatable_visualization_plugin/expression.tsx +++ b/x-pack/legacy/plugins/lens/public/datatable_visualization_plugin/expression.tsx @@ -10,7 +10,7 @@ import { i18n } from '@kbn/i18n'; import { EuiBasicTable } from '@elastic/eui'; import { ExpressionFunction, KibanaDatatable } from 'src/legacy/core_plugins/interpreter/types'; import { LensMultiTable } from '../types'; -import { RenderFunction } from '../interpreter_types'; +import { IInterpreterRenderFunction } from '../../../../../../src/legacy/core_plugins/data/public/expressions/lib/_types'; import { FormatFactory } from '../../../../../../src/legacy/ui/public/visualize/loader/pipeline_helpers/utilities'; export interface DatatableColumns { @@ -109,7 +109,7 @@ export const datatableColumns: ExpressionFunction< export const getDatatableRenderer = ( formatFactory: FormatFactory -): RenderFunction => ({ +): IInterpreterRenderFunction => ({ name: 'lens_datatable_renderer', displayName: i18n.translate('xpack.lens.datatable.visualizationName', { defaultMessage: 'Datatable', diff --git a/x-pack/legacy/plugins/lens/public/datatable_visualization_plugin/plugin.tsx b/x-pack/legacy/plugins/lens/public/datatable_visualization_plugin/plugin.tsx index 52f4f99513e7a..9716b6b708b60 100644 --- a/x-pack/legacy/plugins/lens/public/datatable_visualization_plugin/plugin.tsx +++ b/x-pack/legacy/plugins/lens/public/datatable_visualization_plugin/plugin.tsx @@ -8,16 +8,12 @@ import { CoreSetup } from 'src/core/public'; import { getFormat, FormatFactory } from 'ui/visualize/loader/pipeline_helpers/utilities'; import { datatableVisualization } from './visualization'; - -import { - renderersRegistry, - functionsRegistry, -} from '../../../../../../src/legacy/core_plugins/interpreter/public/registries'; -import { InterpreterSetup, RenderFunction } from '../interpreter_types'; +import { ExpressionsSetup } from '../../../../../../src/legacy/core_plugins/data/public/expressions'; +import { setup as dataSetup } from '../../../../../../src/legacy/core_plugins/data/public/legacy'; import { datatable, datatableColumns, getDatatableRenderer } from './expression'; export interface DatatableVisualizationPluginSetupPlugins { - interpreter: InterpreterSetup; + expressions: ExpressionsSetup; // TODO this is a simulated NP plugin. // Once field formatters are actually migrated, the actual shim can be used fieldFormat: { @@ -30,13 +26,11 @@ class DatatableVisualizationPlugin { setup( _core: CoreSetup | null, - { interpreter, fieldFormat }: DatatableVisualizationPluginSetupPlugins + { expressions, fieldFormat }: DatatableVisualizationPluginSetupPlugins ) { - interpreter.functionsRegistry.register(() => datatableColumns); - interpreter.functionsRegistry.register(() => datatable); - interpreter.renderersRegistry.register( - () => getDatatableRenderer(fieldFormat.formatFactory) as RenderFunction - ); + expressions.registerFunction(() => datatableColumns); + expressions.registerFunction(() => datatable); + expressions.registerRenderer(() => getDatatableRenderer(fieldFormat.formatFactory)); return datatableVisualization; } @@ -48,10 +42,7 @@ const plugin = new DatatableVisualizationPlugin(); export const datatableVisualizationSetup = () => plugin.setup(null, { - interpreter: { - renderersRegistry, - functionsRegistry, - }, + expressions: dataSetup.expressions, fieldFormat: { formatFactory: getFormat, }, 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 e9f3af8e63ff1..9136215219782 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 @@ -120,7 +120,7 @@ describe('embeddable', () => { ); embeddable.render(mountpoint); - expect(expressionRenderer.mock.calls[0][0].getInitialContext!()).toEqual({ + expect(expressionRenderer.mock.calls[0][0].context).toEqual({ timeRange, query, filters, diff --git a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/embeddable/expression_wrapper.tsx b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/embeddable/expression_wrapper.tsx index 024f16ce097d8..035de0493136a 100644 --- a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/embeddable/expression_wrapper.tsx +++ b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/embeddable/expression_wrapper.tsx @@ -61,7 +61,7 @@ export function ExpressionWrapper({ onRenderFailure={(e: unknown) => { setExpressionError(e); }} - getInitialContext={() => context} + context={context} /> )} diff --git a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/mocks.tsx b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/mocks.tsx index a1bcc921104bc..e31053fcc0f04 100644 --- a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/mocks.tsx +++ b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/mocks.tsx @@ -93,14 +93,16 @@ export function createExpressionRendererMock(): jest.Mock< export function createMockDependencies() { return ({ - data: { + dataSetup: { + indexPatterns: { + indexPatterns: {}, + }, + }, + dataStart: { expressions: { ExpressionRenderer: createExpressionRendererMock(), run: jest.fn(_ => Promise.resolve({ type: 'render', as: 'test', value: undefined })), }, - indexPatterns: { - indexPatterns: {}, - }, }, embeddables: { registerEmbeddableFactory: jest.fn(), diff --git a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/plugin.test.tsx b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/plugin.test.tsx index 09a22c61d0ccf..9d6241c033665 100644 --- a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/plugin.test.tsx +++ b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/plugin.test.tsx @@ -13,7 +13,10 @@ jest.mock('ui/chrome', () => ({ // mock away actual dependencies to prevent all of it being loaded jest.mock('../../../../../../src/legacy/core_plugins/interpreter/public/registries', () => {}); -jest.mock('../../../../../../src/legacy/core_plugins/data/public/legacy', () => {}); +jest.mock('../../../../../../src/legacy/core_plugins/data/public/legacy', () => ({ + start: {}, + setup: {}, +})); jest.mock('./embeddable/embeddable_factory', () => ({ EmbeddableFactory: class Mock {} })); describe('editor_frame plugin', () => { 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 62339a4cc3afc..1d60b031f421b 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 @@ -12,7 +12,10 @@ import { CoreSetup } from 'src/core/public'; import chrome, { Chrome } from 'ui/chrome'; import { Plugin as EmbeddablePlugin } from '../../../../../../src/legacy/core_plugins/embeddable_api/public/np_ready/public'; import { setup as embeddablePlugin } from '../../../../../../src/legacy/core_plugins/embeddable_api/public/np_ready/public/legacy'; -import { setup as data } from '../../../../../../src/legacy/core_plugins/data/public/legacy'; +import { + setup as dataSetup, + start as dataStart, +} from '../../../../../../src/legacy/core_plugins/data/public/legacy'; import { ExpressionFunction } from '../../../../../../src/legacy/core_plugins/interpreter/public'; import { functionsRegistry } from '../../../../../../src/legacy/core_plugins/interpreter/public/registries'; import { Datasource, Visualization, EditorFrameSetup, EditorFrameInstance } from '../types'; @@ -22,7 +25,8 @@ import { EmbeddableFactory } from './embeddable/embeddable_factory'; import { getActiveDatasourceIdFromDoc } from './editor_frame/state_management'; export interface EditorFrameSetupPlugins { - data: typeof data; + dataSetup: typeof dataSetup; + dataStart: typeof dataStart; chrome: Chrome; embeddables: ReturnType; interpreter: InterpreterSetup; @@ -48,8 +52,8 @@ export class EditorFramePlugin { 'lens', new EmbeddableFactory( plugins.chrome, - plugins.data.expressions.ExpressionRenderer, - plugins.data.indexPatterns.indexPatterns + plugins.dataStart.expressions.ExpressionRenderer, + plugins.dataSetup.indexPatterns.indexPatterns ) ); @@ -72,7 +76,7 @@ export class EditorFramePlugin { initialVisualizationId={ (doc && doc.visualizationType) || firstVisualizationId || null } - ExpressionRenderer={plugins.data.expressions.ExpressionRenderer} + ExpressionRenderer={plugins.dataStart.expressions.ExpressionRenderer} doc={doc} dateRange={dateRange} query={query} @@ -110,7 +114,8 @@ const editorFrame = new EditorFramePlugin(); export const editorFrameSetup = () => editorFrame.setup(null, { - data, + dataSetup, + dataStart, chrome, embeddables: embeddablePlugin, interpreter: { diff --git a/x-pack/legacy/plugins/lens/public/interpreter_types.ts b/x-pack/legacy/plugins/lens/public/interpreter_types.ts deleted file mode 100644 index fe02ab11757cc..0000000000000 --- a/x-pack/legacy/plugins/lens/public/interpreter_types.ts +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ - -import { Registry } from '@kbn/interpreter/target/common'; -import { ExpressionFunction } from '../../../../../src/legacy/core_plugins/interpreter/public'; - -// TODO these are intermediary types because interpreter is not typed yet -// They can get replaced by references to the real interfaces as soon as they -// are available -interface RenderHandlers { - done: () => void; - onDestroy: (fn: () => void) => void; -} -export interface RenderFunction { - name: string; - displayName: string; - help: string; - validate: () => void; - reuseDomNode: boolean; - render: (domNode: Element, data: T, handlers: RenderHandlers) => void; -} - -export interface InterpreterSetup { - renderersRegistry: Registry; - functionsRegistry: Registry< - ExpressionFunction, - ExpressionFunction - >; -} diff --git a/x-pack/legacy/plugins/lens/public/xy_visualization_plugin/plugin.tsx b/x-pack/legacy/plugins/lens/public/xy_visualization_plugin/plugin.tsx index 6692fc17f05ac..0f0e1e8534cc0 100644 --- a/x-pack/legacy/plugins/lens/public/xy_visualization_plugin/plugin.tsx +++ b/x-pack/legacy/plugins/lens/public/xy_visualization_plugin/plugin.tsx @@ -8,19 +8,14 @@ import { CoreSetup, UiSettingsClientContract } from 'src/core/public'; import chrome, { Chrome } from 'ui/chrome'; import moment from 'moment-timezone'; import { getFormat, FormatFactory } from 'ui/visualize/loader/pipeline_helpers/utilities'; - -import { - renderersRegistry, - functionsRegistry, -} from '../../../../../../src/legacy/core_plugins/interpreter/public/registries'; - +import { ExpressionsSetup } from '../../../../../../src/legacy/core_plugins/data/public/expressions'; +import { setup as dataSetup } from '../../../../../../src/legacy/core_plugins/data/public/legacy'; import { xyVisualization } from './xy_visualization'; -import { InterpreterSetup, RenderFunction } from '../interpreter_types'; import { xyChart, getXyChartRenderer } from './xy_expression'; import { legendConfig, xConfig, layerConfig } from './types'; export interface XyVisualizationPluginSetupPlugins { - interpreter: InterpreterSetup; + expressions: ExpressionsSetup; chrome: Chrome; // TODO this is a simulated NP plugin. // Once field formatters are actually migrated, the actual shim can be used @@ -44,22 +39,21 @@ class XyVisualizationPlugin { setup( _core: CoreSetup | null, { - interpreter, + expressions, fieldFormat: { formatFactory }, chrome: { getUiSettingsClient }, }: XyVisualizationPluginSetupPlugins ) { - interpreter.functionsRegistry.register(() => legendConfig); - interpreter.functionsRegistry.register(() => xConfig); - interpreter.functionsRegistry.register(() => layerConfig); - interpreter.functionsRegistry.register(() => xyChart); + expressions.registerFunction(() => legendConfig); + expressions.registerFunction(() => xConfig); + expressions.registerFunction(() => layerConfig); + expressions.registerFunction(() => xyChart); - interpreter.renderersRegistry.register( - () => - getXyChartRenderer({ - formatFactory, - timeZone: getTimeZone(getUiSettingsClient()), - }) as RenderFunction + expressions.registerRenderer(() => + getXyChartRenderer({ + formatFactory, + timeZone: getTimeZone(getUiSettingsClient()), + }) ); return xyVisualization; @@ -72,10 +66,7 @@ const plugin = new XyVisualizationPlugin(); export const xyVisualizationSetup = () => plugin.setup(null, { - interpreter: { - renderersRegistry, - functionsRegistry, - }, + expressions: dataSetup.expressions, fieldFormat: { formatFactory: getFormat, }, diff --git a/x-pack/legacy/plugins/lens/public/xy_visualization_plugin/xy_expression.tsx b/x-pack/legacy/plugins/lens/public/xy_visualization_plugin/xy_expression.tsx index df69f510a8d21..3eeeee618fb88 100644 --- a/x-pack/legacy/plugins/lens/public/xy_visualization_plugin/xy_expression.tsx +++ b/x-pack/legacy/plugins/lens/public/xy_visualization_plugin/xy_expression.tsx @@ -23,9 +23,9 @@ import { EuiFlexGroup, EuiFlexItem, EuiIcon, EuiText, IconType } from '@elastic/ import { FormattedMessage } from '@kbn/i18n/react'; import { i18n } from '@kbn/i18n'; import { FormatFactory } from '../../../../../../src/legacy/ui/public/visualize/loader/pipeline_helpers/utilities'; +import { IInterpreterRenderFunction } from '../../../../../../src/legacy/core_plugins/data/public/expressions/lib/_types'; import { LensMultiTable } from '../types'; import { XYArgs, SeriesType, visualizationTypes } from './types'; -import { RenderFunction } from '../interpreter_types'; export interface XYChartProps { data: LensMultiTable; @@ -93,7 +93,7 @@ export interface XYChartProps { export const getXyChartRenderer = (dependencies: { formatFactory: FormatFactory; timeZone: string; -}): RenderFunction => ({ +}): IInterpreterRenderFunction => ({ name: 'lens_xy_chart_renderer', displayName: 'XY Chart', help: i18n.translate('xpack.lens.xyChart.renderer.help', { From aa945939c0c0ed0ca611254e3f99c19d693f9dbc Mon Sep 17 00:00:00 2001 From: Wylie Conlon Date: Wed, 4 Sep 2019 15:15:53 -0400 Subject: [PATCH 3/9] Fix passing of interpreter and embeddable --- .../interpreter/public/interpreter.js | 5 +- .../embeddable/expression_wrapper.tsx | 2 +- .../lens/public/editor_frame_plugin/mocks.tsx | 5 -- .../public/editor_frame_plugin/plugin.tsx | 16 +------ .../metric_expression.tsx | 4 +- .../metric_visualization_plugin/plugin.tsx | 48 +++---------------- 6 files changed, 14 insertions(+), 66 deletions(-) diff --git a/src/legacy/core_plugins/interpreter/public/interpreter.js b/src/legacy/core_plugins/interpreter/public/interpreter.js index 4e3b3a3ea827d..3b0d90091db96 100644 --- a/src/legacy/core_plugins/interpreter/public/interpreter.js +++ b/src/legacy/core_plugins/interpreter/public/interpreter.js @@ -61,10 +61,11 @@ export const getInterpreter = async () => { _interpreterPromise = new Promise(resolve => _resolve = resolve); initialize(); } - return await _interpreterPromise; + const { interpreter } = await _interpreterPromise; + return interpreter; }; export const interpretAst = async (...params) => { - const { interpreter } = await getInterpreter(); + const interpreter = await getInterpreter(); return await interpreter.interpretAst(...params); }; diff --git a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/embeddable/expression_wrapper.tsx b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/embeddable/expression_wrapper.tsx index 035de0493136a..3a44af6feae26 100644 --- a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/embeddable/expression_wrapper.tsx +++ b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/embeddable/expression_wrapper.tsx @@ -61,7 +61,7 @@ export function ExpressionWrapper({ onRenderFailure={(e: unknown) => { setExpressionError(e); }} - context={context} + searchContext={{ ...context, type: 'kibana_context' }} /> )} diff --git a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/mocks.tsx b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/mocks.tsx index e31053fcc0f04..a6f19b6aabc98 100644 --- a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/mocks.tsx +++ b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/mocks.tsx @@ -110,10 +110,5 @@ export function createMockDependencies() { chrome: { getSavedObjectsClient: () => {}, }, - interpreter: { - functionsRegistry: { - register: jest.fn(), - }, - }, } as unknown) as MockedDependencies; } 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 1d60b031f421b..c7d84cca61e34 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 @@ -7,7 +7,6 @@ import React from 'react'; import { render, unmountComponentAtNode } from 'react-dom'; import { I18nProvider } from '@kbn/i18n/react'; -import { Registry } from '@kbn/interpreter/target/common'; import { CoreSetup } from 'src/core/public'; import chrome, { Chrome } from 'ui/chrome'; import { Plugin as EmbeddablePlugin } from '../../../../../../src/legacy/core_plugins/embeddable_api/public/np_ready/public'; @@ -16,8 +15,6 @@ import { setup as dataSetup, start as dataStart, } from '../../../../../../src/legacy/core_plugins/data/public/legacy'; -import { ExpressionFunction } from '../../../../../../src/legacy/core_plugins/interpreter/public'; -import { functionsRegistry } from '../../../../../../src/legacy/core_plugins/interpreter/public/registries'; import { Datasource, Visualization, EditorFrameSetup, EditorFrameInstance } from '../types'; import { EditorFrame } from './editor_frame'; import { mergeTables } from './merge_tables'; @@ -29,14 +26,6 @@ export interface EditorFrameSetupPlugins { dataStart: typeof dataStart; chrome: Chrome; embeddables: ReturnType; - interpreter: InterpreterSetup; -} - -export interface InterpreterSetup { - functionsRegistry: Registry< - ExpressionFunction, - ExpressionFunction - >; } export class EditorFramePlugin { @@ -46,7 +35,7 @@ export class EditorFramePlugin { private readonly visualizations: Record = {}; public setup(_core: CoreSetup | null, plugins: EditorFrameSetupPlugins): EditorFrameSetup { - plugins.interpreter.functionsRegistry.register(() => mergeTables); + plugins.dataSetup.expressions.registerFunction(() => mergeTables); plugins.embeddables.registerEmbeddableFactory( 'lens', @@ -118,9 +107,6 @@ export const editorFrameSetup = () => dataStart, chrome, embeddables: embeddablePlugin, - interpreter: { - functionsRegistry, - }, }); export const editorFrameStop = () => editorFrame.stop(); diff --git a/x-pack/legacy/plugins/lens/public/metric_visualization_plugin/metric_expression.tsx b/x-pack/legacy/plugins/lens/public/metric_visualization_plugin/metric_expression.tsx index d6c29abf70c4d..0b7f6aeda05a9 100644 --- a/x-pack/legacy/plugins/lens/public/metric_visualization_plugin/metric_expression.tsx +++ b/x-pack/legacy/plugins/lens/public/metric_visualization_plugin/metric_expression.tsx @@ -8,9 +8,9 @@ import React from 'react'; import ReactDOM from 'react-dom'; import { ExpressionFunction } from 'src/legacy/core_plugins/interpreter/types'; import { FormatFactory } from 'ui/visualize/loader/pipeline_helpers/utilities'; +import { IInterpreterRenderFunction } from '../../../../../../src/legacy/core_plugins/data/public/expressions/lib/_types'; import { MetricConfig } from './types'; import { LensMultiTable } from '../types'; -import { RenderFunction } from './plugin'; import { AutoScale } from './auto_scale'; export interface MetricChartProps { @@ -73,7 +73,7 @@ export const metricChart: ExpressionFunction< export const getMetricChartRenderer = ( formatFactory: FormatFactory -): RenderFunction => ({ +): IInterpreterRenderFunction => ({ name: 'lens_metric_chart_renderer', displayName: 'Metric Chart', help: 'Metric Chart Renderer', diff --git a/x-pack/legacy/plugins/lens/public/metric_visualization_plugin/plugin.tsx b/x-pack/legacy/plugins/lens/public/metric_visualization_plugin/plugin.tsx index f8bfd15b49892..fc3fc9462a498 100644 --- a/x-pack/legacy/plugins/lens/public/metric_visualization_plugin/plugin.tsx +++ b/x-pack/legacy/plugins/lens/public/metric_visualization_plugin/plugin.tsx @@ -4,44 +4,15 @@ * you may not use this file except in compliance with the Elastic License. */ -import { Registry } from '@kbn/interpreter/target/common'; import { CoreSetup } from 'src/core/public'; import { FormatFactory, getFormat } from 'ui/visualize/loader/pipeline_helpers/utilities'; import { metricVisualization } from './metric_visualization'; -import { - renderersRegistry, - functionsRegistry, -} from '../../../../../../src/legacy/core_plugins/interpreter/public/registries'; -import { ExpressionFunction } from '../../../../../../src/legacy/core_plugins/interpreter/public'; +import { ExpressionsSetup } from '../../../../../../src/legacy/core_plugins/data/public/expressions'; +import { setup as dataSetup } from '../../../../../../src/legacy/core_plugins/data/public/legacy'; import { metricChart, getMetricChartRenderer } from './metric_expression'; -// TODO these are intermediary types because interpreter is not typed yet -// They can get replaced by references to the real interfaces as soon as they -// are available -interface RenderHandlers { - done: () => void; - onDestroy: (fn: () => void) => void; -} - -export interface RenderFunction { - name: string; - displayName: string; - help: string; - validate: () => void; - reuseDomNode: boolean; - render: (domNode: Element, data: T, handlers: RenderHandlers) => void; -} - -export interface InterpreterSetup { - renderersRegistry: Registry; - functionsRegistry: Registry< - ExpressionFunction, - ExpressionFunction - >; -} - export interface MetricVisualizationPluginSetupPlugins { - interpreter: InterpreterSetup; + expressions: ExpressionsSetup; // TODO this is a simulated NP plugin. // Once field formatters are actually migrated, the actual shim can be used fieldFormat: { @@ -54,13 +25,11 @@ class MetricVisualizationPlugin { setup( _core: CoreSetup | null, - { interpreter, fieldFormat }: MetricVisualizationPluginSetupPlugins + { expressions, fieldFormat }: MetricVisualizationPluginSetupPlugins ) { - interpreter.functionsRegistry.register(() => metricChart); + expressions.registerFunction(() => metricChart); - interpreter.renderersRegistry.register( - () => getMetricChartRenderer(fieldFormat.formatFactory) as RenderFunction - ); + expressions.registerRenderer(() => getMetricChartRenderer(fieldFormat.formatFactory)); return metricVisualization; } @@ -72,10 +41,7 @@ const plugin = new MetricVisualizationPlugin(); export const metricVisualizationSetup = () => plugin.setup(null, { - interpreter: { - renderersRegistry, - functionsRegistry, - }, + expressions: dataSetup.expressions, fieldFormat: { formatFactory: getFormat, }, From f38b7220b5a45c21a1ea9059b5e73cdc36738912 Mon Sep 17 00:00:00 2001 From: Wylie Conlon Date: Wed, 4 Sep 2019 15:22:18 -0400 Subject: [PATCH 4/9] Fix tests --- .../editor_frame_plugin/embeddable/embeddable.test.tsx | 5 +++-- .../legacy/plugins/lens/public/editor_frame_plugin/mocks.tsx | 4 ++++ 2 files changed, 7 insertions(+), 2 deletions(-) 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 9136215219782..74e2f89b170d6 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 @@ -104,7 +104,7 @@ describe('embeddable', () => { expect(expressionRenderer).toHaveBeenCalledTimes(2); }); - it('should pass context in getInitialContext handler', () => { + it('should pass context to embeddable', () => { const timeRange: TimeRange = { from: 'now-15d', to: 'now' }; const query: Query = { language: 'kquery', query: '' }; const filters: Filter[] = [{ meta: { alias: 'test', negate: false, disabled: false } }]; @@ -120,7 +120,8 @@ describe('embeddable', () => { ); embeddable.render(mountpoint); - expect(expressionRenderer.mock.calls[0][0].context).toEqual({ + expect(expressionRenderer.mock.calls[0][0].searchContext).toEqual({ + type: 'kibana_context', timeRange, query, filters, diff --git a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/mocks.tsx b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/mocks.tsx index a6f19b6aabc98..f4339bf760d8b 100644 --- a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/mocks.tsx +++ b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/mocks.tsx @@ -97,6 +97,10 @@ export function createMockDependencies() { indexPatterns: { indexPatterns: {}, }, + expressions: { + registerFunction: jest.fn(), + registerRenderer: jest.fn(), + }, }, dataStart: { expressions: { From 3abb5240bea5aefe6771c8c7297594dcd5159a43 Mon Sep 17 00:00:00 2001 From: Wylie Conlon Date: Wed, 4 Sep 2019 15:53:40 -0400 Subject: [PATCH 5/9] Fix TimeRange imports and vis alias imports --- .../editor_frame_plugin/editor_frame/expression_helpers.ts | 2 +- .../public/editor_frame_plugin/embeddable/embeddable.test.tsx | 2 +- .../lens/public/editor_frame_plugin/embeddable/embeddable.tsx | 2 +- .../editor_frame_plugin/embeddable/expression_wrapper.tsx | 2 +- x-pack/legacy/plugins/lens/public/register_vis_type_alias.ts | 4 ++-- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/expression_helpers.ts b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/expression_helpers.ts index 537bf12cb79e8..da7ddee67453e 100644 --- a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/expression_helpers.ts +++ b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/expression_helpers.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { TimeRange } from 'ui/timefilter'; +import { TimeRange } from 'src/plugins/data/public'; import { Query } from 'src/legacy/core_plugins/data/public'; import { Filter } from '@kbn/es-query'; import { Ast, fromExpression, ExpressionFunctionAST } from '@kbn/interpreter/common'; 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 74e2f89b170d6..375b29bd10d34 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 @@ -5,7 +5,7 @@ */ import { Embeddable } from './embeddable'; -import { TimeRange } from 'ui/timefilter'; +import { TimeRange } from 'src/plugins/data/public'; import { Query, ExpressionRendererProps } from 'src/legacy/core_plugins/data/public'; import { Filter } from '@kbn/es-query'; import { Document } from '../../persistence'; diff --git a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/embeddable/embeddable.tsx b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/embeddable/embeddable.tsx index 23301bf68c570..8827e5a6397c4 100644 --- a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/embeddable/embeddable.tsx +++ b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/embeddable/embeddable.tsx @@ -8,7 +8,7 @@ import _ from 'lodash'; import React from 'react'; import { render, unmountComponentAtNode } from 'react-dom'; -import { TimeRange } from 'ui/timefilter'; +import { TimeRange } from 'src/plugins/data/public'; import { Query, StaticIndexPattern, ExpressionRenderer } from 'src/legacy/core_plugins/data/public'; import { Filter } from '@kbn/es-query'; import { Subscription } from 'rxjs'; diff --git a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/embeddable/expression_wrapper.tsx b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/embeddable/expression_wrapper.tsx index 3a44af6feae26..e5aabf159de63 100644 --- a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/embeddable/expression_wrapper.tsx +++ b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/embeddable/expression_wrapper.tsx @@ -10,7 +10,7 @@ import React, { useState, useEffect } from 'react'; import { I18nProvider } from '@kbn/i18n/react'; import { FormattedMessage } from '@kbn/i18n/react'; import { EuiFlexGroup, EuiFlexItem, EuiText, EuiIcon } from '@elastic/eui'; -import { TimeRange } from 'ui/timefilter'; +import { TimeRange } from 'src/plugins/data/public'; import { Query } from 'src/legacy/core_plugins/data/public'; import { Filter } from '@kbn/es-query'; import { ExpressionRenderer } from '../../../../../../../src/legacy/core_plugins/data/public'; diff --git a/x-pack/legacy/plugins/lens/public/register_vis_type_alias.ts b/x-pack/legacy/plugins/lens/public/register_vis_type_alias.ts index 8f49f6f12ee16..19f313f2d56d6 100644 --- a/x-pack/legacy/plugins/lens/public/register_vis_type_alias.ts +++ b/x-pack/legacy/plugins/lens/public/register_vis_type_alias.ts @@ -5,12 +5,12 @@ */ import { i18n } from '@kbn/i18n'; -import { visualizations } from '../../../../../src/legacy/core_plugins/visualizations/public'; +import { setup } from '../../../../../src/legacy/core_plugins/visualizations/public/np_ready/public/legacy'; import { BASE_APP_URL, getEditPath } from '../common'; const NOT_INTERNATIONALIZED_PRODUCT_NAME = 'Lens Visualizations'; -visualizations.types.visTypeAliasRegistry.add({ +setup.types.visTypeAliasRegistry.add({ aliasUrl: BASE_APP_URL, name: NOT_INTERNATIONALIZED_PRODUCT_NAME, title: i18n.translate('xpack.lens.visTypeAlias.title', { From d0550a281065c53247b1e8389786351eba848ffd Mon Sep 17 00:00:00 2001 From: Wylie Conlon Date: Wed, 4 Sep 2019 16:54:03 -0400 Subject: [PATCH 6/9] Fix tests again --- .../data/public/expressions/expressions_service.ts | 5 ++++- src/legacy/core_plugins/interpreter/public/interpreter.js | 5 ++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/legacy/core_plugins/data/public/expressions/expressions_service.ts b/src/legacy/core_plugins/data/public/expressions/expressions_service.ts index a00512f04c1ac..2aff9cab67e82 100644 --- a/src/legacy/core_plugins/data/public/expressions/expressions_service.ts +++ b/src/legacy/core_plugins/data/public/expressions/expressions_service.ts @@ -24,6 +24,7 @@ import { setInspector, setInterpreter } from './services'; import { execute } from './lib/execute'; import { loader } from './lib/loader'; import { render } from './lib/render'; +import { IInterpreter } from './lib/_types'; import { createRenderer } from './expression_renderer'; import { Start as IInspector } from '../../../../../plugins/inspector/public'; @@ -40,7 +41,9 @@ export class ExpressionsService { // eslint-disable-next-line const { getInterpreter } = require('../../../interpreter/public/interpreter'); getInterpreter() - .then(setInterpreter) + .then(({ interpreter }: { interpreter: IInterpreter }) => { + setInterpreter(interpreter); + }) .catch((e: Error) => { throw new Error('interpreter is not initialized'); }); diff --git a/src/legacy/core_plugins/interpreter/public/interpreter.js b/src/legacy/core_plugins/interpreter/public/interpreter.js index 3b0d90091db96..4e3b3a3ea827d 100644 --- a/src/legacy/core_plugins/interpreter/public/interpreter.js +++ b/src/legacy/core_plugins/interpreter/public/interpreter.js @@ -61,11 +61,10 @@ export const getInterpreter = async () => { _interpreterPromise = new Promise(resolve => _resolve = resolve); initialize(); } - const { interpreter } = await _interpreterPromise; - return interpreter; + return await _interpreterPromise; }; export const interpretAst = async (...params) => { - const interpreter = await getInterpreter(); + const { interpreter } = await getInterpreter(); return await interpreter.interpretAst(...params); }; From f4ecfe798485845b6bce4ad9e78648f460de6e00 Mon Sep 17 00:00:00 2001 From: Joe Reuter Date: Thu, 5 Sep 2019 12:24:57 +0200 Subject: [PATCH 7/9] use correct lifecycle hooks --- .../plugins/lens/public/app_plugin/plugin.tsx | 28 +++++--- .../embeddable/embeddable_factory.ts | 19 ++++-- .../lens/public/editor_frame_plugin/mocks.tsx | 38 +++++++---- .../editor_frame_plugin/plugin.test.tsx | 25 +++++-- .../public/editor_frame_plugin/plugin.tsx | 68 +++++++++++++------ x-pack/legacy/plugins/lens/public/index.ts | 5 +- x-pack/legacy/plugins/lens/public/types.ts | 5 +- 7 files changed, 129 insertions(+), 59 deletions(-) diff --git a/x-pack/legacy/plugins/lens/public/app_plugin/plugin.tsx b/x-pack/legacy/plugins/lens/public/app_plugin/plugin.tsx index e6af305f56f7c..f3580a59ae89d 100644 --- a/x-pack/legacy/plugins/lens/public/app_plugin/plugin.tsx +++ b/x-pack/legacy/plugins/lens/public/app_plugin/plugin.tsx @@ -9,7 +9,7 @@ import { I18nProvider, FormattedMessage } from '@kbn/i18n/react'; import { HashRouter, Switch, Route, RouteComponentProps } from 'react-router-dom'; import chrome from 'ui/chrome'; import { Storage } from 'ui/storage'; -import { editorFrameSetup, editorFrameStop } from '../editor_frame_plugin'; +import { editorFrameSetup, editorFrameStart, editorFrameStop } from '../editor_frame_plugin'; import { indexPatternDatasourceSetup, indexPatternDatasourceStop } from '../indexpattern_plugin'; import { SavedObjectIndexStore } from '../persistence'; import { xyVisualizationSetup, xyVisualizationStop } from '../xy_visualization_plugin'; @@ -23,6 +23,7 @@ import { EditorFrameInstance } from '../types'; export class AppPlugin { private instance: EditorFrameInstance | null = null; + private store: SavedObjectIndexStore | null = null; constructor() {} @@ -33,15 +34,25 @@ export class AppPlugin { const datatableVisualization = datatableVisualizationSetup(); const xyVisualization = xyVisualizationSetup(); const metricVisualization = metricVisualizationSetup(); - const editorFrame = editorFrameSetup(); - const store = new SavedObjectIndexStore(chrome!.getSavedObjectsClient()); + const editorFrameSetupInterface = editorFrameSetup(); + this.store = new SavedObjectIndexStore(chrome!.getSavedObjectsClient()); - editorFrame.registerDatasource('indexpattern', indexPattern); - editorFrame.registerVisualization(xyVisualization); - editorFrame.registerVisualization(datatableVisualization); - editorFrame.registerVisualization(metricVisualization); + editorFrameSetupInterface.registerDatasource('indexpattern', indexPattern); + editorFrameSetupInterface.registerVisualization(xyVisualization); + editorFrameSetupInterface.registerVisualization(datatableVisualization); + editorFrameSetupInterface.registerVisualization(metricVisualization); + } + + start() { + if (this.store === null) { + throw new Error('Start lifecycle called before setup lifecycle'); + } + + const store = this.store; + + const editorFrameStartInterface = editorFrameStart(); - this.instance = editorFrame.createInstance({}); + this.instance = editorFrameStartInterface.createInstance({}); const renderEditor = (routeProps: RouteComponentProps<{ id?: string }>) => { return ( @@ -97,4 +108,5 @@ export class AppPlugin { const app = new AppPlugin(); export const appSetup = () => app.setup(); +export const appStart = () => app.start(); export const appStop = () => app.stop(); diff --git a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/embeddable/embeddable_factory.ts b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/embeddable/embeddable_factory.ts index edd219b64701b..5d58e80f3cfc6 100644 --- a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/embeddable/embeddable_factory.ts +++ b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/embeddable/embeddable_factory.ts @@ -29,13 +29,9 @@ export class EmbeddableFactory extends AbstractEmbeddableFactory { private chrome: Chrome; private indexPatternService: IndexPatterns; - private expressionRenderer: ExpressionRenderer; + private expressionRenderer: ExpressionRenderer | null = null; - constructor( - chrome: Chrome, - expressionRenderer: ExpressionRenderer, - indexPatternService: IndexPatterns - ) { + constructor(chrome: Chrome, indexPatternService: IndexPatterns) { super({ savedObjectMetaData: { name: i18n.translate('xpack.lens.lensSavedObjectLabel', { @@ -47,7 +43,10 @@ export class EmbeddableFactory extends AbstractEmbeddableFactory { }); this.chrome = chrome; this.indexPatternService = indexPatternService; - this.expressionRenderer = expressionRenderer; + } + + public setExpressionRenderer(renderer: ExpressionRenderer) { + this.expressionRenderer = renderer; } public isEditable() { @@ -69,6 +68,12 @@ export class EmbeddableFactory extends AbstractEmbeddableFactory { input: Partial & { id: string }, parent?: IContainer ) { + if (this.expressionRenderer === null) { + throw new Error( + 'Cannot initialize embeddables before expression renderer is provided. Make sure the `start` lifecycle is completed.' + ); + } + const store = new SavedObjectIndexStore(this.chrome.getSavedObjectsClient()); const savedVis = await store.load(savedObjectId); diff --git a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/mocks.tsx b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/mocks.tsx index f4339bf760d8b..b057101e65153 100644 --- a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/mocks.tsx +++ b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/mocks.tsx @@ -6,11 +6,15 @@ import React from 'react'; import { ExpressionRendererProps } from 'src/legacy/core_plugins/data/public'; -import { setup as data } from '../../../../../../src/legacy/core_plugins/data/public/legacy'; +import { + setup as dataSetup, + start as dataStart, +} from '../../../../../../src/legacy/core_plugins/data/public/legacy'; import { DatasourcePublicAPI, FramePublicAPI, Visualization, Datasource } from '../types'; -import { EditorFrameSetupPlugins } from './plugin'; +import { EditorFrameSetupPlugins, EditorFrameStartPlugins } from './plugin'; -type DataSetup = typeof data; +type DataSetup = typeof dataSetup; +type DataStart = typeof dataStart; export function createMockVisualization(): jest.Mocked { return { @@ -80,10 +84,14 @@ export function createMockFramePublicAPI(): FrameMock { type Omit = Pick>; -export type MockedDependencies = Omit & { +export type MockedSetupDependencies = Omit & { data: Omit & { expressions: jest.Mocked }; }; +export type MockedStartDependencies = Omit & { + data: Omit & { expressions: jest.Mocked }; +}; + export function createExpressionRendererMock(): jest.Mock< React.ReactElement, [ExpressionRendererProps] @@ -91,9 +99,9 @@ export function createExpressionRendererMock(): jest.Mock< return jest.fn(_ => ); } -export function createMockDependencies() { +export function createMockSetupDependencies() { return ({ - dataSetup: { + data: { indexPatterns: { indexPatterns: {}, }, @@ -102,17 +110,21 @@ export function createMockDependencies() { registerRenderer: jest.fn(), }, }, - dataStart: { - expressions: { - ExpressionRenderer: createExpressionRendererMock(), - run: jest.fn(_ => Promise.resolve({ type: 'render', as: 'test', value: undefined })), - }, - }, embeddables: { registerEmbeddableFactory: jest.fn(), }, chrome: { getSavedObjectsClient: () => {}, }, - } as unknown) as MockedDependencies; + } as unknown) as MockedSetupDependencies; +} + +export function createMockStartDependencies() { + return ({ + data: { + expressions: { + ExpressionRenderer: jest.fn(() => null), + }, + }, + } as unknown) as MockedStartDependencies; } diff --git a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/plugin.test.tsx b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/plugin.test.tsx index 9d6241c033665..c864404600d76 100644 --- a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/plugin.test.tsx +++ b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/plugin.test.tsx @@ -5,7 +5,12 @@ */ import { EditorFramePlugin } from './plugin'; -import { createMockDependencies, MockedDependencies } from './mocks'; +import { + MockedSetupDependencies, + MockedStartDependencies, + createMockSetupDependencies, + createMockStartDependencies, +} from './mocks'; jest.mock('ui/chrome', () => ({ getSavedObjectsClient: jest.fn(), @@ -17,17 +22,23 @@ jest.mock('../../../../../../src/legacy/core_plugins/data/public/legacy', () => start: {}, setup: {}, })); -jest.mock('./embeddable/embeddable_factory', () => ({ EmbeddableFactory: class Mock {} })); +jest.mock('./embeddable/embeddable_factory', () => ({ + EmbeddableFactory: class Mock { + setExpressionRenderer() {} + }, +})); describe('editor_frame plugin', () => { let pluginInstance: EditorFramePlugin; let mountpoint: Element; - let pluginDependencies: MockedDependencies; + let pluginSetupDependencies: MockedSetupDependencies; + let pluginStartDependencies: MockedStartDependencies; beforeEach(() => { pluginInstance = new EditorFramePlugin(); mountpoint = document.createElement('div'); - pluginDependencies = createMockDependencies(); + pluginSetupDependencies = createMockSetupDependencies(); + pluginStartDependencies = createMockStartDependencies(); }); afterEach(() => { @@ -36,7 +47,8 @@ describe('editor_frame plugin', () => { it('should create an editor frame instance which mounts and unmounts', () => { expect(() => { - const publicAPI = pluginInstance.setup(null, pluginDependencies); + pluginInstance.setup(null, pluginSetupDependencies); + const publicAPI = pluginInstance.start(null, pluginStartDependencies); const instance = publicAPI.createInstance({}); instance.mount(mountpoint, { onError: jest.fn(), @@ -49,7 +61,8 @@ describe('editor_frame plugin', () => { }); it('should not have child nodes after unmount', () => { - const publicAPI = pluginInstance.setup(null, pluginDependencies); + pluginInstance.setup(null, pluginSetupDependencies); + const publicAPI = pluginInstance.start(null, pluginStartDependencies); const instance = publicAPI.createInstance({}); instance.mount(mountpoint, { onError: jest.fn(), 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 c7d84cca61e34..323f5e59c8489 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 @@ -7,7 +7,7 @@ import React from 'react'; import { render, unmountComponentAtNode } from 'react-dom'; import { I18nProvider } from '@kbn/i18n/react'; -import { CoreSetup } from 'src/core/public'; +import { CoreSetup, CoreStart } from 'src/core/public'; import chrome, { Chrome } from 'ui/chrome'; import { Plugin as EmbeddablePlugin } from '../../../../../../src/legacy/core_plugins/embeddable_api/public/np_ready/public'; import { setup as embeddablePlugin } from '../../../../../../src/legacy/core_plugins/embeddable_api/public/np_ready/public/legacy'; @@ -15,37 +15,63 @@ import { setup as dataSetup, start as dataStart, } from '../../../../../../src/legacy/core_plugins/data/public/legacy'; -import { Datasource, Visualization, EditorFrameSetup, EditorFrameInstance } from '../types'; +import { + Datasource, + Visualization, + EditorFrameSetup, + EditorFrameInstance, + EditorFrameStart, +} from '../types'; 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 { - dataSetup: typeof dataSetup; - dataStart: typeof dataStart; + data: typeof dataSetup; chrome: Chrome; embeddables: ReturnType; } +export interface EditorFrameStartPlugins { + data: typeof dataStart; +} + export class EditorFramePlugin { constructor() {} private readonly datasources: Record = {}; private readonly visualizations: Record = {}; + private embeddableFactory: EmbeddableFactory | null = null; + public setup(_core: CoreSetup | null, plugins: EditorFrameSetupPlugins): EditorFrameSetup { - plugins.dataSetup.expressions.registerFunction(() => mergeTables); - - plugins.embeddables.registerEmbeddableFactory( - 'lens', - new EmbeddableFactory( - plugins.chrome, - plugins.dataStart.expressions.ExpressionRenderer, - plugins.dataSetup.indexPatterns.indexPatterns - ) + plugins.data.expressions.registerFunction(() => mergeTables); + + this.embeddableFactory = new EmbeddableFactory( + plugins.chrome, + plugins.data.indexPatterns.indexPatterns ); + plugins.embeddables.registerEmbeddableFactory('lens', this.embeddableFactory); + + return { + registerDatasource: (name, datasource) => { + this.datasources[name] = datasource as Datasource; + }, + registerVisualization: visualization => { + this.visualizations[visualization.id] = visualization as Visualization; + }, + }; + } + + public start(_core: CoreStart | null, plugins: EditorFrameStartPlugins): EditorFrameStart { + if (this.embeddableFactory === null) { + throw new Error('Start lifecycle called before setup lifecycle'); + } + + this.embeddableFactory.setExpressionRenderer(plugins.data.expressions.ExpressionRenderer); + const createInstance = (): EditorFrameInstance => { let domElement: Element; return { @@ -65,7 +91,7 @@ export class EditorFramePlugin { initialVisualizationId={ (doc && doc.visualizationType) || firstVisualizationId || null } - ExpressionRenderer={plugins.dataStart.expressions.ExpressionRenderer} + ExpressionRenderer={plugins.data.expressions.ExpressionRenderer} doc={doc} dateRange={dateRange} query={query} @@ -85,12 +111,6 @@ export class EditorFramePlugin { return { createInstance, - registerDatasource: (name, datasource) => { - this.datasources[name] = datasource as Datasource; - }, - registerVisualization: visualization => { - this.visualizations[visualization.id] = visualization as Visualization; - }, }; } @@ -103,10 +123,14 @@ const editorFrame = new EditorFramePlugin(); export const editorFrameSetup = () => editorFrame.setup(null, { - dataSetup, - dataStart, + data: dataSetup, chrome, embeddables: embeddablePlugin, }); +export const editorFrameStart = () => + editorFrame.start(null, { + data: dataStart, + }); + export const editorFrameStop = () => editorFrame.stop(); diff --git a/x-pack/legacy/plugins/lens/public/index.ts b/x-pack/legacy/plugins/lens/public/index.ts index 8e232e878e91a..48a37fd5d8656 100644 --- a/x-pack/legacy/plugins/lens/public/index.ts +++ b/x-pack/legacy/plugins/lens/public/index.ts @@ -20,7 +20,7 @@ import 'uiExports/savedObjectTypes'; import { render, unmountComponentAtNode } from 'react-dom'; import { IScope } from 'angular'; import chrome from 'ui/chrome'; -import { appSetup, appStop } from './app_plugin'; +import { appStart, appSetup, appStop } from './app_plugin'; import { PLUGIN_ID } from '../common'; // TODO: Convert this to the "new platform" way of doing UI @@ -31,7 +31,8 @@ function Root($scope: IScope, $element: JQLite) { appStop(); }); - return render(appSetup(), el); + appSetup(); + return render(appStart(), el); } chrome.setRootController(PLUGIN_ID, Root); diff --git a/x-pack/legacy/plugins/lens/public/types.ts b/x-pack/legacy/plugins/lens/public/types.ts index 14fa018fa6e5d..c047ad4fbfe7c 100644 --- a/x-pack/legacy/plugins/lens/public/types.ts +++ b/x-pack/legacy/plugins/lens/public/types.ts @@ -33,12 +33,15 @@ export interface EditorFrameInstance { } export interface EditorFrameSetup { - createInstance: (options: EditorFrameOptions) => EditorFrameInstance; // generic type on the API functions to pull the "unknown vs. specific type" error into the implementation registerDatasource: (name: string, datasource: Datasource) => void; registerVisualization: (visualization: Visualization) => void; } +export interface EditorFrameStart { + createInstance: (options: EditorFrameOptions) => EditorFrameInstance; +} + // Hints the default nesting to the data source. 0 is the highest priority export type DimensionPriority = 0 | 1 | 2; From 8f892450f32191eca2ad747835e46158313eeb80 Mon Sep 17 00:00:00 2001 From: Joe Reuter Date: Thu, 5 Sep 2019 17:47:01 +0200 Subject: [PATCH 8/9] move embeddable registration into start to avoid passing things around --- src/legacy/core_plugins/data/public/plugin.ts | 12 +++++-- .../embeddable/embeddable_factory.ts | 19 ++++------- .../lens/public/editor_frame_plugin/mocks.tsx | 12 +++---- .../editor_frame_plugin/plugin.test.tsx | 4 +-- .../public/editor_frame_plugin/plugin.tsx | 32 ++++++++----------- 5 files changed, 37 insertions(+), 42 deletions(-) diff --git a/src/legacy/core_plugins/data/public/plugin.ts b/src/legacy/core_plugins/data/public/plugin.ts index b62c8106bc168..130c4181822e3 100644 --- a/src/legacy/core_plugins/data/public/plugin.ts +++ b/src/legacy/core_plugins/data/public/plugin.ts @@ -79,6 +79,8 @@ export class DataPlugin implements Plugin & { id: string }, parent?: IContainer ) { - if (this.expressionRenderer === null) { - throw new Error( - 'Cannot initialize embeddables before expression renderer is provided. Make sure the `start` lifecycle is completed.' - ); - } - const store = new SavedObjectIndexStore(this.chrome.getSavedObjectsClient()); const savedVis = await store.load(savedObjectId); diff --git a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/mocks.tsx b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/mocks.tsx index b057101e65153..582aa42051aca 100644 --- a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/mocks.tsx +++ b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/mocks.tsx @@ -102,17 +102,11 @@ export function createExpressionRendererMock(): jest.Mock< export function createMockSetupDependencies() { return ({ data: { - indexPatterns: { - indexPatterns: {}, - }, expressions: { registerFunction: jest.fn(), registerRenderer: jest.fn(), }, }, - embeddables: { - registerEmbeddableFactory: jest.fn(), - }, chrome: { getSavedObjectsClient: () => {}, }, @@ -125,6 +119,12 @@ export function createMockStartDependencies() { expressions: { ExpressionRenderer: jest.fn(() => null), }, + indexPatterns: { + indexPatterns: {}, + }, + }, + embeddables: { + registerEmbeddableFactory: jest.fn(), }, } as unknown) as MockedStartDependencies; } diff --git a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/plugin.test.tsx b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/plugin.test.tsx index c864404600d76..aa37b654f4511 100644 --- a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/plugin.test.tsx +++ b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/plugin.test.tsx @@ -23,9 +23,7 @@ jest.mock('../../../../../../src/legacy/core_plugins/data/public/legacy', () => setup: {}, })); jest.mock('./embeddable/embeddable_factory', () => ({ - EmbeddableFactory: class Mock { - setExpressionRenderer() {} - }, + EmbeddableFactory: class Mock {}, })); describe('editor_frame plugin', () => { 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 323f5e59c8489..2b83f50924e8a 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 @@ -10,7 +10,7 @@ import { I18nProvider } from '@kbn/i18n/react'; import { CoreSetup, CoreStart } from 'src/core/public'; import chrome, { Chrome } from 'ui/chrome'; import { Plugin as EmbeddablePlugin } from '../../../../../../src/legacy/core_plugins/embeddable_api/public/np_ready/public'; -import { setup as embeddablePlugin } from '../../../../../../src/legacy/core_plugins/embeddable_api/public/np_ready/public/legacy'; +import { start as embeddablePlugin } from '../../../../../../src/legacy/core_plugins/embeddable_api/public/np_ready/public/legacy'; import { setup as dataSetup, start as dataStart, @@ -29,12 +29,12 @@ import { getActiveDatasourceIdFromDoc } from './editor_frame/state_management'; export interface EditorFrameSetupPlugins { data: typeof dataSetup; - chrome: Chrome; - embeddables: ReturnType; } export interface EditorFrameStartPlugins { data: typeof dataStart; + embeddables: ReturnType; + chrome: Chrome; } export class EditorFramePlugin { @@ -43,18 +43,9 @@ export class EditorFramePlugin { private readonly datasources: Record = {}; private readonly visualizations: Record = {}; - private embeddableFactory: EmbeddableFactory | null = null; - public setup(_core: CoreSetup | null, plugins: EditorFrameSetupPlugins): EditorFrameSetup { plugins.data.expressions.registerFunction(() => mergeTables); - this.embeddableFactory = new EmbeddableFactory( - plugins.chrome, - plugins.data.indexPatterns.indexPatterns - ); - - plugins.embeddables.registerEmbeddableFactory('lens', this.embeddableFactory); - return { registerDatasource: (name, datasource) => { this.datasources[name] = datasource as Datasource; @@ -66,11 +57,14 @@ export class EditorFramePlugin { } public start(_core: CoreStart | null, plugins: EditorFrameStartPlugins): EditorFrameStart { - if (this.embeddableFactory === null) { - throw new Error('Start lifecycle called before setup lifecycle'); - } - - this.embeddableFactory.setExpressionRenderer(plugins.data.expressions.ExpressionRenderer); + plugins.embeddables.registerEmbeddableFactory( + 'lens', + new EmbeddableFactory( + plugins.chrome, + plugins.data.expressions.ExpressionRenderer, + plugins.data.indexPatterns.indexPatterns + ) + ); const createInstance = (): EditorFrameInstance => { let domElement: Element; @@ -124,13 +118,13 @@ const editorFrame = new EditorFramePlugin(); export const editorFrameSetup = () => editorFrame.setup(null, { data: dataSetup, - chrome, - embeddables: embeddablePlugin, }); export const editorFrameStart = () => editorFrame.start(null, { data: dataStart, + chrome, + embeddables: embeddablePlugin, }); export const editorFrameStop = () => editorFrame.stop(); From ab3ca1da50534ff9026dd0ade0c809abeff06e04 Mon Sep 17 00:00:00 2001 From: Joe Reuter Date: Thu, 5 Sep 2019 18:09:05 +0200 Subject: [PATCH 9/9] fix destructuring --- src/legacy/core_plugins/data/public/plugin.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/legacy/core_plugins/data/public/plugin.ts b/src/legacy/core_plugins/data/public/plugin.ts index 130c4181822e3..ddbc663525f3b 100644 --- a/src/legacy/core_plugins/data/public/plugin.ts +++ b/src/legacy/core_plugins/data/public/plugin.ts @@ -107,7 +107,7 @@ export class DataPlugin implements Plugin