Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

[Vega] Shim new platform - cleanup vega_visualization dependencies #53605

Merged
merged 4 commits into from
Dec 23, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
Comment on lines +27 to +32
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe you could use createKibanaUtilsCore from kibana_utils here.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think I'd make an official best practice out of either option

);
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
29 changes: 15 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,20 @@
* 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.notifications = getNotifications();
this.dataPlugin = getData();
}

/**
Expand All @@ -40,9 +38,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 +52,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 @@ -72,7 +72,7 @@ export const createVegaVisualization = ({ serviceSettings }) =>
*/
async render(visData, visParams, status) {
if (!visData && !this._vegaView) {
toastNotifications.addWarning(
this.notifications.toasts.addWarning(
alexwizp marked this conversation as resolved.
Show resolved Hide resolved
i18n.translate('visTypeVega.visualization.unableToRenderWithoutDataWarningMessage', {
defaultMessage: 'Unable to render without data',
})
Expand All @@ -86,7 +86,7 @@ export const createVegaVisualization = ({ serviceSettings }) =>
if (this._vegaView) {
this._vegaView.onError(error);
} else {
toastNotifications.addError(error, {
this.notifications.toasts.addError(error, {
title: i18n.translate('visTypeVega.visualization.renderErrorTitle', {
defaultMessage: 'Vega error',
}),
Expand All @@ -103,13 +103,14 @@ export const createVegaVisualization = ({ serviceSettings }) =>
this._vegaView = null;
}

const { filterManager } = npStart.plugins.data.query;
const { filterManager: queryfilter } = this.dataPlugin.query;
alexwizp marked this conversation as resolved.
Show resolved Hide resolved
const { timefilter } = this.dataPlugin.query.timefilter;
const vegaViewParams = {
parentEl: this._el,
vegaParser,
serviceSettings,
queryfilter: filterManager,
timefilter: timefilter,
queryfilter,
timefilter,
findIndex: this.findIndex.bind(this),
};

Expand Down