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

[lens] Fix breakage from app-arch movements #44720

Merged
merged 12 commits into from
Sep 5, 2019
2 changes: 1 addition & 1 deletion packages/kbn-interpreter/src/common/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@

export { Registry } from './lib/registry';

export { fromExpression, toExpression, Ast } from './lib/ast';
export { fromExpression, toExpression, Ast, ExpressionFunctionAST } from './lib/ast';
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { IExpressionLoader, ExpressionLoader } from './lib/loader';
// Accept all options of the runner as props except for the
// dom element which is provided by the component itself
export interface ExpressionRendererProps extends IExpressionLoaderParams {
className: 'string';
className: string;
expression: string | ExpressionAST;
/**
* If an element is specified, but the response of the expression run can't be rendered
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { setInspector, setInterpreter } from './services';
import { execute } from './lib/execute';
import { loader } from './lib/loader';
import { render } from './lib/render';
import { IInterpreter } from './lib/_types';
import { createRenderer } from './expression_renderer';

import { Start as IInspector } from '../../../../../plugins/inspector/public';
Expand All @@ -40,7 +41,9 @@ export class ExpressionsService {
// eslint-disable-next-line
const { getInterpreter } = require('../../../interpreter/public/interpreter');
getInterpreter()
.then(setInterpreter)
.then(({ interpreter }: { interpreter: IInterpreter }) => {
Copy link
Member

Choose a reason for hiding this comment

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

is this needed ? (should it be done in master as well ?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I needed to unwrap in at least one place otherwise our use of ExpressionRenderer would throw an error cannot find interpreter.interpretAst

setInterpreter(interpreter);
})
.catch((e: Error) => {
throw new Error('interpreter is not initialized');
});
Expand Down
4 changes: 2 additions & 2 deletions src/legacy/core_plugins/data/public/expressions/lib/_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ export interface IInterpreterRenderHandlers {
event: (event: event) => void;
}

export interface IInterpreterRenderFunction {
export interface IInterpreterRenderFunction<T = unknown> {
name: string;
displayName: string;
help: string;
validate: () => void;
reuseDomNode: boolean;
render: (domNode: Element, data: unknown, handlers: IInterpreterRenderHandlers) => void;
render: (domNode: Element, data: T, handlers: IInterpreterRenderHandlers) => void | Promise<void>;
}

export interface IInterpreter {
Expand Down
5 changes: 5 additions & 0 deletions x-pack/legacy/plugins/lens/public/app_plugin/app.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { Storage } from 'ui/storage';
import { Document, SavedObjectStore } from '../persistence';
import { mount } from 'enzyme';
import { QueryBar } from '../../../../../../src/legacy/core_plugins/data/public/query';
import { SavedObjectsClientContract } from 'src/core/public';

jest.mock('../../../../../../src/legacy/core_plugins/data/public/query', () => ({
QueryBar: jest.fn(() => null),
Expand All @@ -22,6 +23,7 @@ jest.mock('ui/new_platform');
jest.mock('ui/notify');
jest.mock('ui/chrome');
jest.mock('../persistence');
jest.mock('src/core/public');

const waitForPromises = () => new Promise(resolve => setTimeout(resolve));

Expand All @@ -39,6 +41,7 @@ function makeDefaultArgs(): jest.Mocked<{
docId?: string;
docStorage: SavedObjectStore;
redirectTo: (id?: string) => void;
savedObjectsClient: SavedObjectsClientContract;
}> {
return ({
editorFrame: createMockFrame(),
Expand Down Expand Up @@ -68,13 +71,15 @@ function makeDefaultArgs(): jest.Mocked<{
},
QueryBar: jest.fn(() => <div />),
redirectTo: jest.fn(id => {}),
savedObjectsClient: jest.fn(),
} as unknown) as jest.Mocked<{
editorFrame: EditorFrameInstance;
chrome: Chrome;
store: Storage;
docId?: string;
docStorage: SavedObjectStore;
redirectTo: (id?: string) => void;
savedObjectsClient: SavedObjectsClientContract;
}>;
}

Expand Down
4 changes: 4 additions & 0 deletions x-pack/legacy/plugins/lens/public/app_plugin/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { EuiLink, EuiFlexGroup, EuiFlexItem } from '@elastic/eui';
import { Storage } from 'ui/storage';
import { toastNotifications } from 'ui/notify';
import { Chrome } from 'ui/chrome';
import { SavedObjectsClientContract } from 'src/core/public';
import { Query, QueryBar } from '../../../../../../src/legacy/core_plugins/data/public/query';
import { Document, SavedObjectStore } from '../persistence';
import { EditorFrameInstance } from '../types';
Expand Down Expand Up @@ -55,13 +56,15 @@ export function App({
docId,
docStorage,
redirectTo,
savedObjectsClient,
}: {
editorFrame: EditorFrameInstance;
chrome: Chrome;
store: Storage;
docId?: string;
docStorage: SavedObjectStore;
redirectTo: (id?: string) => void;
savedObjectsClient: SavedObjectsClientContract;
}) {
const uiSettings = chrome.getUiSettingsClient();
const timeDefaults = uiSettings.get('timepicker:timeDefaults');
Expand Down Expand Up @@ -228,6 +231,7 @@ export function App({
state.localQueryBarState.dateRange && state.localQueryBarState.dateRange.to
}
uiSettings={uiSettings}
savedObjectsClient={savedObjectsClient}
/>
</div>

Expand Down
1 change: 1 addition & 0 deletions x-pack/legacy/plugins/lens/public/app_plugin/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export class AppPlugin {
editorFrame={this.instance!}
chrome={chrome}
store={new Storage(localStorage)}
savedObjectsClient={chrome.getSavedObjectsClient()}
docId={routeProps.match.params.id}
docStorage={store}
redirectTo={id => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { i18n } from '@kbn/i18n';
import { EuiBasicTable } from '@elastic/eui';
import { ExpressionFunction, KibanaDatatable } from 'src/legacy/core_plugins/interpreter/types';
import { LensMultiTable } from '../types';
import { RenderFunction } from '../interpreter_types';
import { IInterpreterRenderFunction } from '../../../../../../src/legacy/core_plugins/data/public/expressions/lib/_types';
import { FormatFactory } from '../../../../../../src/legacy/ui/public/visualize/loader/pipeline_helpers/utilities';

export interface DatatableColumns {
Expand Down Expand Up @@ -109,7 +109,7 @@ export const datatableColumns: ExpressionFunction<

export const getDatatableRenderer = (
formatFactory: FormatFactory
): RenderFunction<DatatableProps> => ({
): IInterpreterRenderFunction<DatatableProps> => ({
name: 'lens_datatable_renderer',
displayName: i18n.translate('xpack.lens.datatable.visualizationName', {
defaultMessage: 'Datatable',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,12 @@
import { CoreSetup } from 'src/core/public';
import { getFormat, FormatFactory } from 'ui/visualize/loader/pipeline_helpers/utilities';
import { datatableVisualization } from './visualization';

import {
renderersRegistry,
functionsRegistry,
} from '../../../../../../src/legacy/core_plugins/interpreter/public/registries';
import { InterpreterSetup, RenderFunction } from '../interpreter_types';
import { ExpressionsSetup } from '../../../../../../src/legacy/core_plugins/data/public/expressions';
import { setup as dataSetup } from '../../../../../../src/legacy/core_plugins/data/public/legacy';
import { datatable, datatableColumns, getDatatableRenderer } from './expression';

export interface DatatableVisualizationPluginSetupPlugins {
interpreter: InterpreterSetup;
expressions: ExpressionsSetup;
// TODO this is a simulated NP plugin.
// Once field formatters are actually migrated, the actual shim can be used
fieldFormat: {
Expand All @@ -30,13 +26,11 @@ class DatatableVisualizationPlugin {

setup(
_core: CoreSetup | null,
{ interpreter, fieldFormat }: DatatableVisualizationPluginSetupPlugins
{ expressions, fieldFormat }: DatatableVisualizationPluginSetupPlugins
) {
interpreter.functionsRegistry.register(() => datatableColumns);
interpreter.functionsRegistry.register(() => datatable);
interpreter.renderersRegistry.register(
() => getDatatableRenderer(fieldFormat.formatFactory) as RenderFunction<unknown>
);
expressions.registerFunction(() => datatableColumns);
expressions.registerFunction(() => datatable);
expressions.registerRenderer(() => getDatatableRenderer(fieldFormat.formatFactory));

return datatableVisualization;
}
Expand All @@ -48,10 +42,7 @@ const plugin = new DatatableVisualizationPlugin();

export const datatableVisualizationSetup = () =>
plugin.setup(null, {
interpreter: {
renderersRegistry,
functionsRegistry,
},
expressions: dataSetup.expressions,
fieldFormat: {
formatFactory: getFormat,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { TimeRange } from 'ui/timefilter/time_history';
import { TimeRange } from 'src/plugins/data/public';
import { Query } from 'src/legacy/core_plugins/data/public';
import { Filter } from '@kbn/es-query';
import { Ast, fromExpression, ExpressionFunctionAST } from '@kbn/interpreter/common';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { Embeddable } from './embeddable';
import { TimeRange } from 'ui/timefilter/time_history';
import { TimeRange } from 'src/plugins/data/public';
import { Query, ExpressionRendererProps } from 'src/legacy/core_plugins/data/public';
import { Filter } from '@kbn/es-query';
import { Document } from '../../persistence';
Expand Down Expand Up @@ -104,7 +104,7 @@ describe('embeddable', () => {
expect(expressionRenderer).toHaveBeenCalledTimes(2);
});

it('should pass context in getInitialContext handler', () => {
it('should pass context to embeddable', () => {
const timeRange: TimeRange = { from: 'now-15d', to: 'now' };
const query: Query = { language: 'kquery', query: '' };
const filters: Filter[] = [{ meta: { alias: 'test', negate: false, disabled: false } }];
Expand All @@ -120,7 +120,8 @@ describe('embeddable', () => {
);
embeddable.render(mountpoint);

expect(expressionRenderer.mock.calls[0][0].getInitialContext!()).toEqual({
expect(expressionRenderer.mock.calls[0][0].searchContext).toEqual({
type: 'kibana_context',
timeRange,
query,
filters,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import _ from 'lodash';
import React from 'react';
import { render, unmountComponentAtNode } from 'react-dom';

import { TimeRange } from 'ui/timefilter/time_history';
import { TimeRange } from 'src/plugins/data/public';
import { Query, StaticIndexPattern, ExpressionRenderer } from 'src/legacy/core_plugins/data/public';
import { Filter } from '@kbn/es-query';
import { Subscription } from 'rxjs';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import React, { useState, useEffect } from 'react';
import { I18nProvider } from '@kbn/i18n/react';
import { FormattedMessage } from '@kbn/i18n/react';
import { EuiFlexGroup, EuiFlexItem, EuiText, EuiIcon } from '@elastic/eui';
import { TimeRange } from 'ui/timefilter/time_history';
import { TimeRange } from 'src/plugins/data/public';
import { Query } from 'src/legacy/core_plugins/data/public';
import { Filter } from '@kbn/es-query';
import { ExpressionRenderer } from '../../../../../../../src/legacy/core_plugins/data/public';
Expand Down Expand Up @@ -61,7 +61,7 @@ export function ExpressionWrapper({
onRenderFailure={(e: unknown) => {
setExpressionError(e);
}}
getInitialContext={() => context}
searchContext={{ ...context, type: 'kibana_context' }}
/>
)}
</I18nProvider>
Expand Down
19 changes: 10 additions & 9 deletions x-pack/legacy/plugins/lens/public/editor_frame_plugin/mocks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,25 +93,26 @@ export function createExpressionRendererMock(): jest.Mock<

export function createMockDependencies() {
return ({
data: {
dataSetup: {
indexPatterns: {
indexPatterns: {},
},
expressions: {
registerFunction: jest.fn(),
registerRenderer: jest.fn(),
},
},
dataStart: {
expressions: {
ExpressionRenderer: createExpressionRendererMock(),
run: jest.fn(_ => Promise.resolve({ type: 'render', as: 'test', value: undefined })),
},
indexPatterns: {
indexPatterns: {},
},
},
embeddables: {
registerEmbeddableFactory: jest.fn(),
},
chrome: {
getSavedObjectsClient: () => {},
},
interpreter: {
functionsRegistry: {
register: jest.fn(),
},
},
} as unknown) as MockedDependencies;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ jest.mock('ui/chrome', () => ({

// mock away actual dependencies to prevent all of it being loaded
jest.mock('../../../../../../src/legacy/core_plugins/interpreter/public/registries', () => {});
jest.mock('../../../../../../src/legacy/core_plugins/data/public/legacy', () => {});
jest.mock('../../../../../../src/legacy/core_plugins/data/public/legacy', () => ({
start: {},
setup: {},
}));
jest.mock('./embeddable/embeddable_factory', () => ({ EmbeddableFactory: class Mock {} }));

describe('editor_frame plugin', () => {
Expand Down
Loading