Skip to content

Commit

Permalink
[Vega] Shim new platform - cleanup vega_visualization dependencies (#…
Browse files Browse the repository at this point in the history
…53605)

* Cleanup Vega Vis

* fix PR comments

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
alexwizp and elasticmachine committed Dec 23, 2019
1 parent 57b1f19 commit 6ff8931
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
13 changes: 9 additions & 4 deletions src/legacy/core_plugins/vis_type_vega/public/legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<VegaPluginSetupDependencies> = {
const setupPlugins: Readonly<VegaPluginSetupDependencies> = {
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<VegaPluginStartDependencies> = {
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);
34 changes: 22 additions & 12 deletions src/legacy/core_plugins/vis_type_vega/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<DataPublicPlugin['setup']>;
};
}

/** @internal */
export interface VegaPluginSetupDependencies {
expressions: ReturnType<ExpressionsPublicPlugin['setup']>;
visualizations: VisualizationsSetup;
data: ReturnType<DataPublicPlugin['setup']>;
__LEGACY: LegacyDependenciesPlugin;
}

/** @internal */
export interface VegaPluginStartDependencies {
data: ReturnType<DataPublicPlugin['start']>;
}

/** @internal */
export class VegaPlugin implements Plugin<Promise<void>, void> {
initializerContext: PluginInitializerContext;
Expand All @@ -52,10 +57,13 @@ export class VegaPlugin implements Plugin<Promise<void>, void> {

public async setup(
core: CoreSetup,
{ expressions, visualizations, __LEGACY }: VegaPluginSetupDependencies
{ data, expressions, visualizations, __LEGACY }: VegaPluginSetupDependencies
) {
const visualizationDependencies: Readonly<VegaVisualizationDependencies> = {
uiSettings: core.uiSettings,
core,
plugins: {
data,
},
...(await __LEGACY.setup()),
};

Expand All @@ -66,7 +74,9 @@ export class VegaPlugin implements Plugin<Promise<void>, void> {
);
}

public start(core: CoreStart) {
// nothing to do here yet
public start(core: CoreStart, { data }: VegaPluginStartDependencies) {
setNotifications(core.notifications);
setSavedObjects(core.savedObjects);
setData(data);
}
}
33 changes: 33 additions & 0 deletions src/legacy/core_plugins/vis_type_vega/public/services.ts
Original file line number Diff line number Diff line change
@@ -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<DataPublicPluginStart>('Data');

export const [getNotifications, setNotifications] = createGetterSetter<NotificationsStart>(
'Notifications'
);

export const [getSavedObjects, setSavedObjects] = createGetterSetter<SavedObjectsStart>(
'SavedObjects'
);
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -288,15 +288,15 @@ 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);
}
});
}

removeAllFiltersHandler() {
this._queryfilter.removeAll();
this._filterManager.removeAll();
}

/**
Expand Down
30 changes: 16 additions & 14 deletions src/legacy/core_plugins/vis_type_vega/public/vega_visualization.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand All @@ -40,9 +37,11 @@ export const createVegaVisualization = ({ serviceSettings }) =>
* @returns {Promise<string>} 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', {
Expand All @@ -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', {
Expand All @@ -71,8 +70,10 @@ export const createVegaVisualization = ({ serviceSettings }) =>
* @returns {Promise<void>}
*/
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',
})
Expand All @@ -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',
}),
Expand All @@ -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),
};

Expand Down

0 comments on commit 6ff8931

Please sign in to comment.