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

[7.x] Expressions refactor (#54342) #57366

Merged
merged 3 commits into from
Feb 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion packages/kbn-interpreter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.0",
"license": "Apache-2.0",
"scripts": {
"interpreter:peg": "pegjs common/lib/grammar.peg",
"interpreter:peg": "pegjs src/common/lib/grammar.peg",
"build": "node scripts/build",
"kbn:bootstrap": "node scripts/build --dev",
"kbn:watch": "node scripts/build --dev --watch"
Expand Down
4 changes: 1 addition & 3 deletions packages/kbn-interpreter/src/common/lib/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ export class Registry {
}

register(fn) {
if (typeof fn !== 'function') throw new Error(`Register requires an function`);

const obj = fn();
const obj = typeof fn === 'function' ? fn() : fn;

if (typeof obj !== 'object' || !obj[this._prop]) {
throw new Error(`Registered functions must return an object with a ${this._prop} property`);
Expand Down
1 change: 1 addition & 0 deletions packages/kbn-utility-types/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ type B = UnwrapPromise<A>; // string
- `ShallowPromise<T>` &mdash; Same as `Promise` type, but it flat maps the wrapped type.
- `UnwrapObservable<T>` &mdash; Returns wrapped type of an observable.
- `UnwrapPromise<T>` &mdash; Returns wrapped type of a promise.
- `UnwrapPromiseOrReturn<T>` &mdash; Returns wrapped type of a promise or the type itself, if it isn't a promise.
5 changes: 5 additions & 0 deletions packages/kbn-utility-types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ export type ShallowPromise<T> = T extends Promise<infer U> ? Promise<U> : Promis
*/
export type UnwrapPromise<T extends Promise<any>> = PromiseType<T>;

/**
* Returns wrapped type of a promise, or returns type as is, if it is not a promise.
*/
export type UnwrapPromiseOrReturn<T> = T extends Promise<infer U> ? U : T;

/**
* Minimal interface for an object resembling an `Observable`.
*/
Expand Down
23 changes: 10 additions & 13 deletions src/legacy/core_plugins/data/public/search/expressions/esaggs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { createFormat } from 'ui/visualize/loader/pipeline_helpers/utilities';
import {
KibanaContext,
KibanaDatatable,
ExpressionFunction,
ExpressionFunctionDefinition,
KibanaDatatableColumn,
} from 'src/plugins/expressions/public';
import {
Expand Down Expand Up @@ -66,7 +66,8 @@ export interface RequestHandlerParams {

const name = 'esaggs';

type Context = KibanaContext | null;
type Input = KibanaContext | null;
type Output = Promise<KibanaDatatable>;

interface Arguments {
index: string;
Expand All @@ -76,8 +77,6 @@ interface Arguments {
aggConfigs: string;
}

type Return = Promise<KibanaDatatable>;

const handleCourierRequest = async ({
searchSource,
aggs,
Expand Down Expand Up @@ -221,12 +220,10 @@ const handleCourierRequest = async ({
return (searchSource as any).tabifiedResponse;
};

export const esaggs = (): ExpressionFunction<typeof name, Context, Arguments, Return> => ({
export const esaggs = (): ExpressionFunctionDefinition<typeof name, Input, Arguments, Output> => ({
name,
type: 'kibana_datatable',
context: {
types: ['kibana_context', 'null'],
},
inputTypes: ['kibana_context', 'null'],
help: i18n.translate('data.functions.esaggs.help', {
defaultMessage: 'Run AggConfig aggregation',
}),
Expand Down Expand Up @@ -256,7 +253,7 @@ export const esaggs = (): ExpressionFunction<typeof name, Context, Arguments, Re
help: '',
},
},
async fn(context, args, { inspectorAdapters, abortSignal }) {
async fn(input, args, { inspectorAdapters, abortSignal }) {
const indexPatterns = getIndexPatterns();
const { filterManager } = getQueryService();

Expand All @@ -272,13 +269,13 @@ export const esaggs = (): ExpressionFunction<typeof name, Context, Arguments, Re
const response = await handleCourierRequest({
searchSource,
aggs,
timeRange: get(context, 'timeRange', undefined),
query: get(context, 'query', undefined),
filters: get(context, 'filters', undefined),
timeRange: get(input, 'timeRange', undefined),
query: get(input, 'query', undefined),
filters: get(input, 'filters', undefined),
forceFetch: true,
metricsAtAllLevels: args.metricsAtAllLevels,
partialRows: args.partialRows,
inspectorAdapters,
inspectorAdapters: inspectorAdapters as Adapters,
filterManager,
abortSignal: (abortSignal as unknown) as AbortSignal,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
import { createInputControlVisFn } from './input_control_fn';

// eslint-disable-next-line
import { functionWrapper } from '../../../../plugins/expressions/public/functions/tests/utils';
import { functionWrapper } from '../../../../plugins/expressions/common/expression_functions/specs/tests/utils';

jest.mock('./legacy_imports.ts');

describe('interpreter/functions#input_control_vis', () => {
const fn = functionWrapper(createInputControlVisFn);
const fn = functionWrapper(createInputControlVisFn());
const visConfig = {
controls: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,11 @@
import { i18n } from '@kbn/i18n';

import {
ExpressionFunction,
ExpressionFunctionDefinition,
KibanaDatatable,
Render,
} from '../../../../plugins/expressions/public';

const name = 'input_control_vis';

type Context = KibanaDatatable;

interface Arguments {
visConfig: string;
}
Expand All @@ -40,19 +36,15 @@ interface RenderValue {
visConfig: VisParams;
}

type Return = Promise<Render<RenderValue>>;

export const createInputControlVisFn = (): ExpressionFunction<
typeof name,
Context,
export const createInputControlVisFn = (): ExpressionFunctionDefinition<
'input_control_vis',
KibanaDatatable,
Arguments,
Return
Render<RenderValue>
> => ({
name: 'input_control_vis',
type: 'render',
context: {
types: [],
},
inputTypes: [],
help: i18n.translate('inputControl.function.help', {
defaultMessage: 'Input control visualization',
}),
Expand All @@ -63,7 +55,7 @@ export const createInputControlVisFn = (): ExpressionFunction<
help: '',
},
},
async fn(context, args) {
fn(input, args) {
const params = JSON.parse(args.visConfig);
return {
type: 'render',
Expand Down
20 changes: 0 additions & 20 deletions src/legacy/core_plugins/interpreter/README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,2 @@
Interpreter legacy plugin has been migrated to the New Platform. Use
`expressions` New Platform plugin instead.

In the New Platform:

```ts
class MyPlugin {
setup(core, { expressions }) {
expressions.registerFunction(myFunction);
}
start(core, { expressions }) {
}
}
```

In the Legacy Platform:

```ts
import { npSetup, npStart } from 'ui/new_platform';

npSetup.plugins.expressions.registerFunction(myFunction);
```
7 changes: 2 additions & 5 deletions src/legacy/core_plugins/interpreter/public/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ import 'uiExports/interpreter';
import { register, registryFactory } from '@kbn/interpreter/common';
import { npSetup } from 'ui/new_platform';
import { registries } from './registries';
import {
ExpressionInterpretWithHandlers,
ExpressionExecutor,
} from '../../../../plugins/expressions/public';
import { Executor, ExpressionExecutor } from '../../../../plugins/expressions/public';

// Expose kbnInterpreter.register(specs) and kbnInterpreter.registries() globally so that plugins
// can register without a transpile step.
Expand All @@ -46,7 +43,7 @@ export const getInterpreter = async () => {
};

// TODO: This function will be left behind in the legacy platform.
export const interpretAst: ExpressionInterpretWithHandlers = async (ast, context, handlers) => {
export const interpretAst: Executor['run'] = async (ast, context, handlers) => {
const { interpreter } = await getInterpreter();
return await interpreter.interpretAst(ast, context, handlers);
};
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
*/

// eslint-disable-next-line
import { functionWrapper } from '../../../../plugins/expressions/public/functions/tests/utils';
import { functionWrapper } from '../../../../plugins/expressions/common/expression_functions/specs/tests/utils';
import { createRegionMapFn } from './region_map_fn';

jest.mock('ui/new_platform');

describe('interpreter/functions#regionmap', () => {
const fn = functionWrapper(createRegionMapFn);
const fn = functionWrapper(createRegionMapFn());
const context = {
type: 'kibana_datatable',
rows: [{ 'col-0-1': 0 }],
Expand Down
4 changes: 2 additions & 2 deletions src/legacy/core_plugins/tile_map/public/tilemap_fn.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

// eslint-disable-next-line
import { functionWrapper } from '../../../../plugins/expressions/public/functions/tests/utils';
import { functionWrapper } from '../../../../plugins/expressions/common/expression_functions/specs/tests/utils';
import { createTileMapFn } from './tile_map_fn';

jest.mock('ui/new_platform');
Expand All @@ -41,7 +41,7 @@ jest.mock('ui/vis/map/convert_to_geojson', () => ({
import { convertToGeoJson } from 'ui/vis/map/convert_to_geojson';

describe('interpreter/functions#tilemap', () => {
const fn = functionWrapper(createTileMapFn);
const fn = functionWrapper(createTileMapFn());
const context = {
type: 'kibana_datatable',
rows: [{ 'col-0-1': 0 }],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
*/

// eslint-disable-next-line
import { functionWrapper } from '../../../../plugins/expressions/public/functions/tests/utils';
import { functionWrapper } from '../../../../plugins/expressions/common/expression_functions/specs/tests/utils';
import { createMarkdownVisFn } from './markdown_fn';

describe('interpreter/functions#markdown', () => {
const fn = functionWrapper(createMarkdownVisFn);
const fn = functionWrapper(createMarkdownVisFn());
const args = {
font: { spec: { fontSize: 12 } },
openLinksInNewTab: true,
Expand Down
24 changes: 8 additions & 16 deletions src/legacy/core_plugins/vis_type_markdown/public/markdown_fn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,23 @@
*/

import { i18n } from '@kbn/i18n';
import { ExpressionFunction, Render } from '../../../../plugins/expressions/public';
import { ExpressionFunctionDefinition, Render } from '../../../../plugins/expressions/public';
import { Arguments, MarkdownVisParams } from './types';

const name = 'markdownVis';

type Context = undefined;

interface RenderValue {
visType: 'markdown';
visConfig: MarkdownVisParams;
}

type Return = Promise<Render<RenderValue>>;

export const createMarkdownVisFn = (): ExpressionFunction<
typeof name,
Context,
export const createMarkdownVisFn = (): ExpressionFunctionDefinition<
'markdownVis',
unknown,
Arguments,
Return
Render<RenderValue>
> => ({
name,
name: 'markdownVis',
type: 'render',
context: {
types: [],
},
inputTypes: [],
help: i18n.translate('visTypeMarkdown.function.help', {
defaultMessage: 'Markdown visualization',
}),
Expand Down Expand Up @@ -70,7 +62,7 @@ export const createMarkdownVisFn = (): ExpressionFunction<
}),
},
},
async fn(context, args) {
fn(input, args) {
return {
type: 'render',
as: 'visualization',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,19 @@

import { last, findIndex, isNaN } from 'lodash';
import React, { Component } from 'react';

import { isColorDark } from '@elastic/eui';

import { getFormat } from '../legacy_imports';
import { MetricVisValue } from './metric_vis_value';
import { Input } from '../metric_vis_fn';
import { FieldFormatsContentType, IFieldFormat } from '../../../../../plugins/data/public';
import { Context } from '../metric_vis_fn';
import { KibanaDatatable } from '../../../../../plugins/expressions/public';
import { getHeatmapColors } from '../../../../../plugins/charts/public';
import { VisParams, MetricVisMetric } from '../types';
import { SchemaConfig, Vis } from '../../../visualizations/public';

export interface MetricVisComponentProps {
visParams: VisParams;
visData: Context;
visData: Input;
vis: Vis;
renderComplete: () => void;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@

import { createMetricVisFn } from './metric_vis_fn';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { functionWrapper } from '../../../../plugins/expressions/public/functions/tests/utils';
import { functionWrapper } from '../../../../plugins/expressions/common/expression_functions/specs/tests/utils';

jest.mock('ui/new_platform');

describe('interpreter/functions#metric', () => {
const fn = functionWrapper(createMetricVisFn);
const fn = functionWrapper(createMetricVisFn());
const context = {
type: 'kibana_datatable',
rows: [{ 'col-0-1': 0 }],
Expand Down
Loading