Skip to content

Commit

Permalink
[TSVB] Shim new platform (elastic#39169)
Browse files Browse the repository at this point in the history
* Shim server side to new platform

* Shim public to new platfrom

* Break by services

* Add dependencies to the TSVB plugin

* Change folder structure for the shim

* Pass services as a second argument of setup() and small fixes

* Add start() to the Plugin

* Get rid of the Private

* Pass the core to setup()

* Get rid of NP folder

* Set config to timezoneProvider()

* Take an external dependency from EditorController

* Take an extra dependency out from Request Handler

* Rename metricsPlugin to Plugin

* Fix reviews

* Add types to .setup()

* Change types of TSVB

* Divide the plugin, its setup config and and entry point

* Get rid of @ts-ignore

* Add a server type to the CustomCoreSetup interface

* Revert kbn_vis_type settings

* Restructure public assets

* Move setup.js inner to the legacy.ts

* clean up

* fix PR commnets
  • Loading branch information
gospodarsky authored and alexwizp committed Aug 5, 2019
1 parent 51c48b7 commit 8d00d26
Show file tree
Hide file tree
Showing 11 changed files with 330 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,39 @@
*/

import { resolve } from 'path';
import { Legacy } from 'kibana';
import { PluginInitializerContext } from 'src/core/server';
import { CoreSetup } from 'src/core/server';

import { fieldsRoutes } from './server/routes/fields';
import { visDataRoutes } from './server/routes/vis';
import { SearchStrategiesRegister } from './server/lib/search_strategies/search_strategies_register';
import { plugin } from './server/';
import { CustomCoreSetup } from './server/plugin';

export default function(kibana) {
return new kibana.Plugin({
require: ['kibana', 'elasticsearch'],
import { LegacyPluginApi, LegacyPluginInitializer } from '../../../../src/legacy/types';

const metricsPluginInitializer: LegacyPluginInitializer = ({ Plugin }: LegacyPluginApi) =>
new Plugin({
id: 'metrics',
require: ['kibana', 'elasticsearch', 'visualizations', 'interpreter', 'data'],
publicDir: resolve(__dirname, 'public'),
uiExports: {
visTypes: ['plugins/metrics/kbn_vis_types'],
interpreter: ['plugins/metrics/tsvb_fn'],
styleSheetPaths: resolve(__dirname, 'public/index.scss'),
hacks: [resolve(__dirname, 'public/legacy')],
injectDefaultVars: server => ({}),
},
init: (server: Legacy.Server) => {
const initializerContext = {} as PluginInitializerContext;
const core = { http: { server } } as CoreSetup & CustomCoreSetup;

config(Joi) {
plugin(initializerContext).setup(core);
},
config(Joi: any) {
return Joi.object({
enabled: Joi.boolean().default(true),
chartResolution: Joi.number().default(150),
minimumBucketSize: Joi.number().default(10),
}).default();
},
} as Legacy.PluginSpecOptions);

init(server) {
fieldsRoutes(server);
visDataRoutes(server);

SearchStrategiesRegister.init(server);
},
});
}
// eslint-disable-next-line import/no-default-export
export default metricsPluginInitializer;
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@
import React from 'react';
import { render, unmountComponentAtNode } from 'react-dom';
import { I18nContext } from 'ui/i18n';
import chrome from 'ui/chrome';
import { fetchIndexPatternFields } from '../lib/fetch_fields';
import { fetchIndexPatternFields } from './lib/fetch_fields';

function ReactEditorControllerProvider(config) {
class ReactEditorController {
export function createEditorController(config, savedObjectsClient) {
return class {
constructor(el, savedObj) {
this.el = el;

Expand All @@ -36,7 +35,6 @@ function ReactEditorControllerProvider(config) {
}

fetchDefaultIndexPattern = async () => {
const savedObjectsClient = chrome.getSavedObjectsClient();
const indexPattern = await savedObjectsClient.get(
'index-pattern',
config.get('defaultIndex')
Expand Down Expand Up @@ -85,12 +83,5 @@ function ReactEditorControllerProvider(config) {
destroy() {
unmountComponentAtNode(this.el);
}
}

return {
name: 'react_editor',
handler: ReactEditorController,
};
}

export { ReactEditorControllerProvider };
25 changes: 25 additions & 0 deletions src/legacy/core_plugins/metrics/public/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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 { PluginInitializerContext } from '../../../../core/public';
import { MetricsPlugin as Plugin } from './plugin';

export function plugin(initializerContext: PluginInitializerContext) {
return new Plugin(initializerContext);
}

This file was deleted.

35 changes: 35 additions & 0 deletions src/legacy/core_plugins/metrics/public/legacy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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 { PluginInitializerContext } from 'kibana/public';
import { npSetup, npStart } from 'ui/new_platform';

import { visualizations } from '../../visualizations/public';
import { MetricsPluginSetupDependencies } from './plugin';
import { plugin } from '.';

const plugins: Readonly<MetricsPluginSetupDependencies> = {
visualizations,
data: npSetup.plugins.data,
};

const pluginInstance = plugin({} as PluginInitializerContext);

export const setup = pluginInstance.setup(npSetup.core, plugins);
export const start = pluginInstance.start(npStart.core);
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,37 @@
* under the License.
*/

import { functionsRegistry } from 'plugins/interpreter/registries';
import { get } from 'lodash';
import { i18n } from '@kbn/i18n';
import { MetricsRequestHandlerProvider } from './kbn_vis_types/request_handler';
import { PersistedState } from 'ui/persisted_state';

import chrome from 'ui/chrome';

export const tsvb = () => ({
name: 'tsvb',
import { ExpressionFunction, KibanaContext, Render } from '../../interpreter/types';

// @ts-ignore
import { createMetricsRequestHandler } from './request_handler';

const name = 'tsvb';
type Context = KibanaContext | null;

interface Arguments {
params: string;
uiState: string;
}

type VisParams = Required<Arguments>;

interface RenderValue {
visType: 'metrics';
visData: Context;
visConfig: VisParams;
uiState: any;
}

type Return = Promise<Render<RenderValue>>;

export const createMetricsFn = (): ExpressionFunction<typeof name, Context, Arguments, Return> => ({
name,
type: 'render',
context: {
types: ['kibana_context', 'null'],
Expand All @@ -38,17 +59,17 @@ export const tsvb = () => ({
params: {
types: ['string'],
default: '"{}"',
help: '',
},
uiState: {
types: ['string'],
default: '"{}"',
help: '',
},
},
async fn(context, args) {
const $injector = await chrome.dangerouslyGetActiveInjector();
const Private = $injector.get('Private');
const metricsRequestHandler = Private(MetricsRequestHandlerProvider).handler;

async fn(context: Context, args: Arguments) {
const uiSettings = chrome.getUiSettingsClient();
const metricsRequestHandler = createMetricsRequestHandler(uiSettings);
const params = JSON.parse(args.params);
const uiStateParams = JSON.parse(args.uiState);
const uiState = new PersistedState(uiStateParams);
Expand All @@ -58,7 +79,7 @@ export const tsvb = () => ({
query: get(context, 'query', null),
filters: get(context, 'filters', null),
visParams: params,
uiState: uiState,
uiState,
});

response.visType = 'metrics';
Expand All @@ -67,13 +88,11 @@ export const tsvb = () => ({
type: 'render',
as: 'visualization',
value: {
uiState,
visType: 'metrics',
visConfig: params,
uiState: uiState,
visData: response,
},
};
},
});

functionsRegistry.register(tsvb);
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,27 @@
* under the License.
*/

import { MetricsRequestHandlerProvider } from './request_handler';
import chrome from 'ui/chrome';
import { i18n } from '@kbn/i18n';
import { ReactEditorControllerProvider } from './editor_controller';
import { VisFactoryProvider } from 'ui/vis/vis_factory';
// @ts-ignore
import { defaultFeedbackMessage } from 'ui/vis/default_feedback_message';

import { PANEL_TYPES } from '../../common/panel_types';
import { visFactory } from '../../visualizations/public';

// register the provider with the visTypes registry so that other know it exists
import { VisTypesRegistryProvider } from 'ui/registry/vis_types';
VisTypesRegistryProvider.register(MetricsVisProvider);
// @ts-ignore
import { createMetricsRequestHandler } from './request_handler';
// @ts-ignore
import { createEditorController } from './editor_controller';
// @ts-ignore
import { PANEL_TYPES } from '../common/panel_types';

export function MetricsVisProvider(Private) {
const VisFactory = Private(VisFactoryProvider);
const ReactEditorController = Private(ReactEditorControllerProvider).handler;
const metricsRequestHandler = Private(MetricsRequestHandlerProvider).handler;
export const createMetricsTypeDefinition = () => {
const uiSettings = chrome.getUiSettingsClient();
const savedObjectsClient = chrome.getSavedObjectsClient();
const EditorController = createEditorController(uiSettings, savedObjectsClient);
const metricsRequestHandler = createMetricsRequestHandler(uiSettings);

return VisFactory.createReactVisualization({
return visFactory.createReactVisualization({
name: 'metrics',
title: i18n.translate('tsvb.kbnVisTypes.metricsTitle', { defaultMessage: 'TSVB' }),
description: i18n.translate('tsvb.kbnVisTypes.metricsDescription', {
Expand Down Expand Up @@ -76,11 +79,11 @@ export function MetricsVisProvider(Private) {
show_legend: 1,
show_grid: 1,
},
component: require('../components/vis_editor').VisEditor,
component: require('./components/vis_editor').VisEditor,
},
editor: ReactEditorController,
editor: EditorController,
editorConfig: {
component: require('../components/vis_editor').VisEditor,
component: require('./components/vis_editor').VisEditor,
},
options: {
showQueryBar: false,
Expand All @@ -90,4 +93,4 @@ export function MetricsVisProvider(Private) {
requestHandler: metricsRequestHandler,
responseHandler: 'none',
});
}
};
Loading

0 comments on commit 8d00d26

Please sign in to comment.