From 6ff8931d73b16a50aee305fdf1d369053a2d0244 Mon Sep 17 00:00:00 2001 From: Alexey Antonov Date: Mon, 23 Dec 2019 19:01:14 +0300 Subject: [PATCH] [Vega] Shim new platform - cleanup vega_visualization dependencies (#53605) * Cleanup Vega Vis * fix PR comments Co-authored-by: Elastic Machine --- .../public/__tests__/vega_visualization.js | 13 ++++++- .../vis_type_vega/public/legacy.ts | 13 ++++--- .../vis_type_vega/public/plugin.ts | 34 ++++++++++++------- .../vis_type_vega/public/services.ts | 33 ++++++++++++++++++ .../public/vega_request_handler.ts | 6 ++-- .../public/vega_view/vega_base_view.js | 8 ++--- .../public/vega_visualization.js | 30 ++++++++-------- 7 files changed, 99 insertions(+), 38 deletions(-) create mode 100644 src/legacy/core_plugins/vis_type_vega/public/services.ts diff --git a/src/legacy/core_plugins/vis_type_vega/public/__tests__/vega_visualization.js b/src/legacy/core_plugins/vis_type_vega/public/__tests__/vega_visualization.js index d10f5a3bd7b14..0c12b3d928815 100644 --- a/src/legacy/core_plugins/vis_type_vega/public/__tests__/vega_visualization.js +++ b/src/legacy/core_plugins/vis_type_vega/public/__tests__/vega_visualization.js @@ -62,7 +62,18 @@ describe('VegaVisualizations', () => { vegaVisualizationDependencies = { es: $injector.get('es'), serviceSettings: $injector.get('serviceSettings'), - uiSettings: $injector.get('config'), + core: { + uiSettings: $injector.get('config'), + }, + plugins: { + data: { + query: { + timefilter: { + timefilter: {}, + }, + }, + }, + }, }; if (!visRegComplete) { diff --git a/src/legacy/core_plugins/vis_type_vega/public/legacy.ts b/src/legacy/core_plugins/vis_type_vega/public/legacy.ts index d40212bec3b7f..a7928c7d65e81 100644 --- a/src/legacy/core_plugins/vis_type_vega/public/legacy.ts +++ b/src/legacy/core_plugins/vis_type_vega/public/legacy.ts @@ -21,20 +21,25 @@ import { PluginInitializerContext } from 'kibana/public'; import { npSetup, npStart } from 'ui/new_platform'; import { setup as visualizationsSetup } from '../../visualizations/public/np_ready/public/legacy'; -import { VegaPluginSetupDependencies } from './plugin'; +import { VegaPluginSetupDependencies, VegaPluginStartDependencies } from './plugin'; import { LegacyDependenciesPlugin } from './shim'; import { plugin } from '.'; -const plugins: Readonly = { +const setupPlugins: Readonly = { expressions: npSetup.plugins.expressions, visualizations: visualizationsSetup, + data: npSetup.plugins.data, // Temporary solution // It will be removed when all dependent services are migrated to the new platform. __LEGACY: new LegacyDependenciesPlugin(), }; +const startPlugins: Readonly = { + data: npStart.plugins.data, +}; + const pluginInstance = plugin({} as PluginInitializerContext); -export const setup = pluginInstance.setup(npSetup.core, plugins); -export const start = pluginInstance.start(npStart.core); +export const setup = pluginInstance.setup(npSetup.core, setupPlugins); +export const start = pluginInstance.start(npStart.core, startPlugins); diff --git a/src/legacy/core_plugins/vis_type_vega/public/plugin.ts b/src/legacy/core_plugins/vis_type_vega/public/plugin.ts index 5166770d1727b..75444a4a4f8e4 100644 --- a/src/legacy/core_plugins/vis_type_vega/public/plugin.ts +++ b/src/legacy/core_plugins/vis_type_vega/public/plugin.ts @@ -16,32 +16,37 @@ * specific language governing permissions and limitations * under the License. */ -import { - PluginInitializerContext, - CoreSetup, - CoreStart, - Plugin, - IUiSettingsClient, -} from '../../../../core/public'; +import { PluginInitializerContext, CoreSetup, CoreStart, Plugin } from '../../../../core/public'; import { LegacyDependenciesPlugin, LegacyDependenciesPluginSetup } from './shim'; import { Plugin as ExpressionsPublicPlugin } from '../../../../plugins/expressions/public'; +import { Plugin as DataPublicPlugin } from '../../../../plugins/data/public'; import { VisualizationsSetup } from '../../visualizations/public'; +import { setNotifications, setData, setSavedObjects } from './services'; import { createVegaFn } from './vega_fn'; import { createVegaTypeDefinition } from './vega_type'; /** @internal */ export interface VegaVisualizationDependencies extends LegacyDependenciesPluginSetup { - uiSettings: IUiSettingsClient; + core: CoreSetup; + plugins: { + data: ReturnType; + }; } /** @internal */ export interface VegaPluginSetupDependencies { expressions: ReturnType; visualizations: VisualizationsSetup; + data: ReturnType; __LEGACY: LegacyDependenciesPlugin; } +/** @internal */ +export interface VegaPluginStartDependencies { + data: ReturnType; +} + /** @internal */ export class VegaPlugin implements Plugin, void> { initializerContext: PluginInitializerContext; @@ -52,10 +57,13 @@ export class VegaPlugin implements Plugin, void> { public async setup( core: CoreSetup, - { expressions, visualizations, __LEGACY }: VegaPluginSetupDependencies + { data, expressions, visualizations, __LEGACY }: VegaPluginSetupDependencies ) { const visualizationDependencies: Readonly = { - uiSettings: core.uiSettings, + core, + plugins: { + data, + }, ...(await __LEGACY.setup()), }; @@ -66,7 +74,9 @@ export class VegaPlugin implements Plugin, void> { ); } - public start(core: CoreStart) { - // nothing to do here yet + public start(core: CoreStart, { data }: VegaPluginStartDependencies) { + setNotifications(core.notifications); + setSavedObjects(core.savedObjects); + setData(data); } } diff --git a/src/legacy/core_plugins/vis_type_vega/public/services.ts b/src/legacy/core_plugins/vis_type_vega/public/services.ts new file mode 100644 index 0000000000000..94723f1a378d2 --- /dev/null +++ b/src/legacy/core_plugins/vis_type_vega/public/services.ts @@ -0,0 +1,33 @@ +/* + * 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 { SavedObjectsStart } from 'kibana/public'; +import { NotificationsStart } from 'src/core/public'; +import { DataPublicPluginStart } from '../../../../plugins/data/public'; +import { createGetterSetter } from '../../../../plugins/kibana_utils/public'; + +export const [getData, setData] = createGetterSetter('Data'); + +export const [getNotifications, setNotifications] = createGetterSetter( + 'Notifications' +); + +export const [getSavedObjects, setSavedObjects] = createGetterSetter( + 'SavedObjects' +); diff --git a/src/legacy/core_plugins/vis_type_vega/public/vega_request_handler.ts b/src/legacy/core_plugins/vis_type_vega/public/vega_request_handler.ts index 26380bf2b9d94..089cc3215d87d 100644 --- a/src/legacy/core_plugins/vis_type_vega/public/vega_request_handler.ts +++ b/src/legacy/core_plugins/vis_type_vega/public/vega_request_handler.ts @@ -16,8 +16,6 @@ * specific language governing permissions and limitations * under the License. */ - -import { timefilter } from 'ui/timefilter'; import { esFilters, esQuery, TimeRange, Query } from '../../../../plugins/data/public'; // @ts-ignore @@ -39,10 +37,12 @@ interface VegaRequestHandlerParams { export function createVegaRequestHandler({ es, - uiSettings, + plugins, + core: { uiSettings }, serviceSettings, }: VegaVisualizationDependencies) { const searchCache = new SearchCache(es, { max: 10, maxAge: 4 * 1000 }); + const { timefilter } = plugins.data.query.timefilter; const timeCache = new TimeCache(timefilter, 3 * 1000); return ({ timeRange, filters, query, visParams }: VegaRequestHandlerParams) => { diff --git a/src/legacy/core_plugins/vis_type_vega/public/vega_view/vega_base_view.js b/src/legacy/core_plugins/vis_type_vega/public/vega_view/vega_base_view.js index 98e376fdac2d6..9d6adfd11aedd 100644 --- a/src/legacy/core_plugins/vis_type_vega/public/vega_view/vega_base_view.js +++ b/src/legacy/core_plugins/vis_type_vega/public/vega_view/vega_base_view.js @@ -64,7 +64,7 @@ export class VegaBaseView { this._$parentEl = $(opts.parentEl); this._parser = opts.vegaParser; this._serviceSettings = opts.serviceSettings; - this._queryfilter = opts.queryfilter; + this._filterManager = opts.filterManager; this._timefilter = opts.timefilter; this._findIndex = opts.findIndex; this._view = null; @@ -271,7 +271,7 @@ export class VegaBaseView { async addFilterHandler(query, index) { const indexId = await this._findIndex(index); const filter = esFilters.buildQueryFilter(query, indexId); - this._queryfilter.addFilters(filter); + this._filterManager.addFilters(filter); } /** @@ -288,7 +288,7 @@ export class VegaBaseView { // this._queryfilter.removeFilter(filter); $injector.get('$rootScope').$evalAsync(() => { try { - this._queryfilter.removeFilter(filter); + this._filterManager.removeFilter(filter); } catch (err) { this.onError(err); } @@ -296,7 +296,7 @@ export class VegaBaseView { } removeAllFiltersHandler() { - this._queryfilter.removeAll(); + this._filterManager.removeAll(); } /** diff --git a/src/legacy/core_plugins/vis_type_vega/public/vega_visualization.js b/src/legacy/core_plugins/vis_type_vega/public/vega_visualization.js index 1d95978a9dadb..3d48eeaaf3f94 100644 --- a/src/legacy/core_plugins/vis_type_vega/public/vega_visualization.js +++ b/src/legacy/core_plugins/vis_type_vega/public/vega_visualization.js @@ -16,22 +16,19 @@ * specific language governing permissions and limitations * under the License. */ -import chrome from 'ui/chrome'; import { i18n } from '@kbn/i18n'; -import { toastNotifications } from 'ui/notify'; import { VegaView } from './vega_view/vega_view'; import { VegaMapView } from './vega_view/vega_map_view'; -import { timefilter } from 'ui/timefilter'; -import { npStart } from 'ui/new_platform'; - -import { findIndexPatternByTitle } from '../../data/public/index_patterns'; +import { getNotifications, getData, getSavedObjects } from './services'; export const createVegaVisualization = ({ serviceSettings }) => class VegaVisualization { constructor(el, vis) { - this.savedObjectsClient = chrome.getSavedObjectsClient(); this._el = el; this._vis = vis; + + this.savedObjectsClient = getSavedObjects(); + this.dataPlugin = getData(); } /** @@ -40,9 +37,11 @@ export const createVegaVisualization = ({ serviceSettings }) => * @returns {Promise} index id */ async findIndex(index) { + const { indexPatterns } = this.dataPlugin; let idxObj; + if (index) { - idxObj = await findIndexPatternByTitle(this.savedObjectsClient, index); + idxObj = indexPatterns.findByTitle(this.savedObjectsClient, index); if (!idxObj) { throw new Error( i18n.translate('visTypeVega.visualization.indexNotFoundErrorMessage', { @@ -52,7 +51,7 @@ export const createVegaVisualization = ({ serviceSettings }) => ); } } else { - idxObj = await npStart.plugins.data.indexPatterns.getDefault(); + idxObj = await indexPatterns.getDefault(); if (!idxObj) { throw new Error( i18n.translate('visTypeVega.visualization.unableToFindDefaultIndexErrorMessage', { @@ -71,8 +70,10 @@ export const createVegaVisualization = ({ serviceSettings }) => * @returns {Promise} */ async render(visData, visParams, status) { + const { toasts } = getNotifications(); + if (!visData && !this._vegaView) { - toastNotifications.addWarning( + toasts.addWarning( i18n.translate('visTypeVega.visualization.unableToRenderWithoutDataWarningMessage', { defaultMessage: 'Unable to render without data', }) @@ -86,7 +87,7 @@ export const createVegaVisualization = ({ serviceSettings }) => if (this._vegaView) { this._vegaView.onError(error); } else { - toastNotifications.addError(error, { + toasts.addError(error, { title: i18n.translate('visTypeVega.visualization.renderErrorTitle', { defaultMessage: 'Vega error', }), @@ -103,13 +104,14 @@ export const createVegaVisualization = ({ serviceSettings }) => this._vegaView = null; } - const { filterManager } = npStart.plugins.data.query; + const { filterManager } = this.dataPlugin.query; + const { timefilter } = this.dataPlugin.query.timefilter; const vegaViewParams = { parentEl: this._el, vegaParser, serviceSettings, - queryfilter: filterManager, - timefilter: timefilter, + filterManager, + timefilter, findIndex: this.findIndex.bind(this), };