2019-09-17 20:57:53 +02:00
|
|
|
/*
|
|
|
|
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
2021-02-04 03:12:39 +01:00
|
|
|
* or more contributor license agreements. Licensed under the Elastic License
|
|
|
|
* 2.0; you may not use this file except in compliance with the Elastic License
|
|
|
|
* 2.0.
|
2019-09-17 20:57:53 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
import React from 'react';
|
|
|
|
import { render, unmountComponentAtNode } from 'react-dom';
|
|
|
|
import { I18nProvider } from '@kbn/i18n/react';
|
2020-04-15 12:22:37 +02:00
|
|
|
import { CoreSetup, CoreStart } from 'kibana/public';
|
|
|
|
import { ExpressionsSetup, ExpressionsStart } from '../../../../../src/plugins/expressions/public';
|
|
|
|
import { EmbeddableSetup, EmbeddableStart } from '../../../../../src/plugins/embeddable/public';
|
2019-09-17 20:57:53 +02:00
|
|
|
import {
|
2019-12-09 12:31:49 +01:00
|
|
|
DataPublicPluginSetup,
|
|
|
|
DataPublicPluginStart,
|
2020-04-15 12:22:37 +02:00
|
|
|
} from '../../../../../src/plugins/data/public';
|
2019-09-17 20:57:53 +02:00
|
|
|
import {
|
|
|
|
Datasource,
|
|
|
|
Visualization,
|
|
|
|
EditorFrameSetup,
|
|
|
|
EditorFrameInstance,
|
|
|
|
EditorFrameStart,
|
|
|
|
} from '../types';
|
2020-08-21 18:08:25 +02:00
|
|
|
import { Document } from '../persistence/saved_object_store';
|
2019-09-17 20:57:53 +02:00
|
|
|
import { mergeTables } from './merge_tables';
|
2020-09-23 23:40:52 +02:00
|
|
|
import { EmbeddableFactory, LensEmbeddableStartServices } from './embeddable/embeddable_factory';
|
2020-05-15 12:01:27 +02:00
|
|
|
import { UiActionsStart } from '../../../../../src/plugins/ui_actions/public';
|
2020-11-04 11:27:52 +01:00
|
|
|
import { ChartsPluginSetup } from '../../../../../src/plugins/charts/public';
|
2020-09-23 23:40:52 +02:00
|
|
|
import { DashboardStart } from '../../../../../src/plugins/dashboard/public';
|
|
|
|
import { LensAttributeService } from '../lens_attribute_service';
|
2019-09-17 20:57:53 +02:00
|
|
|
|
|
|
|
export interface EditorFrameSetupPlugins {
|
2019-12-09 12:31:49 +01:00
|
|
|
data: DataPublicPluginSetup;
|
2020-04-15 12:22:37 +02:00
|
|
|
embeddable?: EmbeddableSetup;
|
2019-11-19 01:26:13 +01:00
|
|
|
expressions: ExpressionsSetup;
|
2020-11-04 11:27:52 +01:00
|
|
|
charts: ChartsPluginSetup;
|
2019-09-17 20:57:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface EditorFrameStartPlugins {
|
2019-12-09 12:31:49 +01:00
|
|
|
data: DataPublicPluginStart;
|
2020-04-15 12:22:37 +02:00
|
|
|
embeddable?: EmbeddableStart;
|
2020-09-23 23:40:52 +02:00
|
|
|
dashboard?: DashboardStart;
|
2019-11-19 01:26:13 +01:00
|
|
|
expressions: ExpressionsStart;
|
2020-05-15 12:01:27 +02:00
|
|
|
uiActions?: UiActionsStart;
|
2020-11-04 11:27:52 +01:00
|
|
|
charts: ChartsPluginSetup;
|
2019-09-17 20:57:53 +02:00
|
|
|
}
|
|
|
|
|
2020-02-13 13:54:51 +01:00
|
|
|
async function collectAsyncDefinitions<T extends { id: string }>(
|
2020-09-29 19:00:29 +02:00
|
|
|
definitions: Array<T | (() => Promise<T>)>
|
2020-02-13 13:54:51 +01:00
|
|
|
) {
|
2020-09-29 19:00:29 +02:00
|
|
|
const resolvedDefinitions = await Promise.all(
|
|
|
|
definitions.map((definition) => (typeof definition === 'function' ? definition() : definition))
|
|
|
|
);
|
2020-02-13 13:54:51 +01:00
|
|
|
const definitionMap: Record<string, T> = {};
|
2020-05-22 09:08:58 +02:00
|
|
|
resolvedDefinitions.forEach((definition) => {
|
2020-02-13 13:54:51 +01:00
|
|
|
definitionMap[definition.id] = definition;
|
|
|
|
});
|
|
|
|
|
|
|
|
return definitionMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class EditorFrameService {
|
2019-09-17 20:57:53 +02:00
|
|
|
constructor() {}
|
|
|
|
|
2020-09-29 19:00:29 +02:00
|
|
|
private readonly datasources: Array<Datasource | (() => Promise<Datasource>)> = [];
|
|
|
|
private readonly visualizations: Array<Visualization | (() => Promise<Visualization>)> = [];
|
2019-09-17 20:57:53 +02:00
|
|
|
|
2020-08-21 18:08:25 +02:00
|
|
|
/**
|
|
|
|
* This method takes a Lens saved object as returned from the persistence helper,
|
|
|
|
* initializes datsources and visualization and creates the current expression.
|
|
|
|
* This is an asynchronous process and should only be triggered once for a saved object.
|
|
|
|
* @param doc parsed Lens saved object
|
|
|
|
*/
|
2021-02-15 13:00:31 +01:00
|
|
|
private documentToExpression = async (doc: Document) => {
|
2020-08-21 18:08:25 +02:00
|
|
|
const [resolvedDatasources, resolvedVisualizations] = await Promise.all([
|
|
|
|
collectAsyncDefinitions(this.datasources),
|
|
|
|
collectAsyncDefinitions(this.visualizations),
|
|
|
|
]);
|
|
|
|
|
2020-10-02 21:50:25 +02:00
|
|
|
const { persistedStateToExpression } = await import('../async_services');
|
|
|
|
|
2020-08-21 18:08:25 +02:00
|
|
|
return await persistedStateToExpression(resolvedDatasources, resolvedVisualizations, doc);
|
2021-02-15 13:00:31 +01:00
|
|
|
};
|
2020-08-21 18:08:25 +02:00
|
|
|
|
2020-03-16 20:37:42 +01:00
|
|
|
public setup(
|
|
|
|
core: CoreSetup<EditorFrameStartPlugins>,
|
2020-09-23 23:40:52 +02:00
|
|
|
plugins: EditorFrameSetupPlugins,
|
2020-10-19 17:21:25 +02:00
|
|
|
getAttributeService: () => Promise<LensAttributeService>
|
2020-03-16 20:37:42 +01:00
|
|
|
): EditorFrameSetup {
|
2019-09-17 20:57:53 +02:00
|
|
|
plugins.expressions.registerFunction(() => mergeTables);
|
|
|
|
|
2020-09-23 23:40:52 +02:00
|
|
|
const getStartServices = async (): Promise<LensEmbeddableStartServices> => {
|
2020-03-16 20:37:42 +01:00
|
|
|
const [coreStart, deps] = await core.getStartServices();
|
|
|
|
return {
|
2020-10-19 17:21:25 +02:00
|
|
|
attributeService: await getAttributeService(),
|
2020-03-16 20:37:42 +01:00
|
|
|
capabilities: coreStart.application.capabilities,
|
|
|
|
coreHttp: coreStart.http,
|
|
|
|
timefilter: deps.data.query.timefilter.timefilter,
|
|
|
|
expressionRenderer: deps.expressions.ReactExpressionRenderer,
|
2021-02-15 13:00:31 +01:00
|
|
|
documentToExpression: this.documentToExpression,
|
2020-03-16 20:37:42 +01:00
|
|
|
indexPatternService: deps.data.indexPatterns,
|
2020-05-15 12:01:27 +02:00
|
|
|
uiActions: deps.uiActions,
|
2020-03-16 20:37:42 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-04-15 12:22:37 +02:00
|
|
|
if (plugins.embeddable) {
|
|
|
|
plugins.embeddable.registerEmbeddableFactory('lens', new EmbeddableFactory(getStartServices));
|
|
|
|
}
|
2020-03-16 20:37:42 +01:00
|
|
|
|
2019-09-17 20:57:53 +02:00
|
|
|
return {
|
2020-05-22 09:08:58 +02:00
|
|
|
registerDatasource: (datasource) => {
|
2020-02-13 13:54:51 +01:00
|
|
|
this.datasources.push(datasource as Datasource<unknown, unknown>);
|
2019-09-17 20:57:53 +02:00
|
|
|
},
|
2020-05-22 09:08:58 +02:00
|
|
|
registerVisualization: (visualization) => {
|
2020-08-21 18:08:25 +02:00
|
|
|
this.visualizations.push(visualization as Visualization<unknown>);
|
2019-09-17 20:57:53 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public start(core: CoreStart, plugins: EditorFrameStartPlugins): EditorFrameStart {
|
2020-02-13 13:54:51 +01:00
|
|
|
const createInstance = async (): Promise<EditorFrameInstance> => {
|
2019-09-17 20:57:53 +02:00
|
|
|
let domElement: Element;
|
2020-02-13 13:54:51 +01:00
|
|
|
const [resolvedDatasources, resolvedVisualizations] = await Promise.all([
|
|
|
|
collectAsyncDefinitions(this.datasources),
|
|
|
|
collectAsyncDefinitions(this.visualizations),
|
|
|
|
]);
|
|
|
|
|
2021-01-20 16:44:59 +01:00
|
|
|
const unmount = () => {
|
|
|
|
if (domElement) {
|
|
|
|
unmountComponentAtNode(domElement);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-09-17 20:57:53 +02:00
|
|
|
return {
|
2020-09-29 19:00:29 +02:00
|
|
|
mount: async (
|
2020-07-01 10:08:37 +02:00
|
|
|
element,
|
2020-10-06 10:15:41 +02:00
|
|
|
{
|
|
|
|
doc,
|
|
|
|
onError,
|
|
|
|
dateRange,
|
|
|
|
query,
|
|
|
|
filters,
|
|
|
|
savedQuery,
|
|
|
|
onChange,
|
|
|
|
showNoDataPopover,
|
|
|
|
initialContext,
|
2020-12-29 14:18:30 +01:00
|
|
|
searchSessionId,
|
2020-10-06 10:15:41 +02:00
|
|
|
}
|
2020-07-01 10:08:37 +02:00
|
|
|
) => {
|
2021-01-20 16:44:59 +01:00
|
|
|
if (domElement !== element) {
|
|
|
|
unmount();
|
|
|
|
}
|
2019-09-17 20:57:53 +02:00
|
|
|
domElement = element;
|
2020-02-13 13:54:51 +01:00
|
|
|
const firstDatasourceId = Object.keys(resolvedDatasources)[0];
|
|
|
|
const firstVisualizationId = Object.keys(resolvedVisualizations)[0];
|
2019-09-17 20:57:53 +02:00
|
|
|
|
2020-10-02 21:50:25 +02:00
|
|
|
const { EditorFrame, getActiveDatasourceIdFromDoc } = await import('../async_services');
|
2020-09-29 19:00:29 +02:00
|
|
|
|
2020-11-04 11:27:52 +01:00
|
|
|
const palettes = await plugins.charts.palettes.getPalettes();
|
|
|
|
|
2019-09-17 20:57:53 +02:00
|
|
|
render(
|
|
|
|
<I18nProvider>
|
|
|
|
<EditorFrame
|
|
|
|
data-test-subj="lnsEditorFrame"
|
|
|
|
onError={onError}
|
2020-02-13 13:54:51 +01:00
|
|
|
datasourceMap={resolvedDatasources}
|
|
|
|
visualizationMap={resolvedVisualizations}
|
2019-09-17 20:57:53 +02:00
|
|
|
initialDatasourceId={getActiveDatasourceIdFromDoc(doc) || firstDatasourceId || null}
|
|
|
|
initialVisualizationId={
|
|
|
|
(doc && doc.visualizationType) || firstVisualizationId || null
|
|
|
|
}
|
2020-11-03 14:29:56 +01:00
|
|
|
key={doc?.savedObjectId} // ensures rerendering when switching to another visualization inside of lens (eg global search)
|
2019-09-17 20:57:53 +02:00
|
|
|
core={core}
|
2020-05-15 12:01:27 +02:00
|
|
|
plugins={plugins}
|
2020-02-11 19:47:36 +01:00
|
|
|
ExpressionRenderer={plugins.expressions.ReactExpressionRenderer}
|
2020-11-04 11:27:52 +01:00
|
|
|
palettes={palettes}
|
2019-09-17 20:57:53 +02:00
|
|
|
doc={doc}
|
|
|
|
dateRange={dateRange}
|
|
|
|
query={query}
|
2019-10-03 17:24:45 +02:00
|
|
|
filters={filters}
|
|
|
|
savedQuery={savedQuery}
|
2019-09-17 20:57:53 +02:00
|
|
|
onChange={onChange}
|
2020-07-01 10:08:37 +02:00
|
|
|
showNoDataPopover={showNoDataPopover}
|
2020-10-06 10:15:41 +02:00
|
|
|
initialContext={initialContext}
|
2020-12-29 14:18:30 +01:00
|
|
|
searchSessionId={searchSessionId}
|
2019-09-17 20:57:53 +02:00
|
|
|
/>
|
|
|
|
</I18nProvider>,
|
|
|
|
domElement
|
|
|
|
);
|
|
|
|
},
|
2021-01-20 16:44:59 +01:00
|
|
|
unmount,
|
2019-09-17 20:57:53 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
createInstance,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|