From b22e91adc0cd5366ea2aac38f6377f60a4c16e68 Mon Sep 17 00:00:00 2001 From: ppisljar Date: Mon, 17 Aug 2020 04:41:38 -0700 Subject: [PATCH] adding toAST function --- .../common/ast/build_function.test.ts | 17 +++ .../expressions/common/ast/build_function.ts | 12 +- src/plugins/vis_type_metric/kibana.json | 2 +- .../public/__snapshots__/to_ast.test.ts.snap | 73 +++++++++++ .../vis_type_metric/public/metric_vis_fn.ts | 6 +- .../vis_type_metric/public/metric_vis_type.ts | 3 +- .../vis_type_metric/public/to_ast.test.ts | 54 ++++++++ src/plugins/vis_type_metric/public/to_ast.ts | 103 ++++++++++++++++ src/plugins/visualizations/public/index.ts | 1 + .../__snapshots__/build_pipeline.test.ts.snap | 8 +- .../public/legacy/build_pipeline.test.ts | 39 +----- .../public/legacy/build_pipeline.ts | 115 ++++++------------ src/plugins/visualizations/public/types.ts | 14 ++- src/plugins/visualizations/public/vis.ts | 4 +- .../public/vis_types/base_vis_type.ts | 5 +- 15 files changed, 319 insertions(+), 137 deletions(-) create mode 100644 src/plugins/vis_type_metric/public/__snapshots__/to_ast.test.ts.snap create mode 100644 src/plugins/vis_type_metric/public/to_ast.test.ts create mode 100644 src/plugins/vis_type_metric/public/to_ast.ts diff --git a/src/plugins/expressions/common/ast/build_function.test.ts b/src/plugins/expressions/common/ast/build_function.test.ts index a2b54f31f6f8f..1f914d6f471e6 100644 --- a/src/plugins/expressions/common/ast/build_function.test.ts +++ b/src/plugins/expressions/common/ast/build_function.test.ts @@ -79,6 +79,11 @@ describe('buildExpressionFunction()', () => { `); }); + test('ignores any args in initial state which value is undefined', () => { + const fn = buildExpressionFunction('hello', { world: undefined }); + expect(fn.arguments).not.toHaveProperty('world'); + }); + test('returns all expected properties', () => { const fn = buildExpressionFunction('hello', { world: [true] }); expect(Object.keys(fn)).toMatchInlineSnapshot(` @@ -264,6 +269,18 @@ describe('buildExpressionFunction()', () => { `); }); + test('does not add new argument if the value is undefined', () => { + const fn = buildExpressionFunction('hello', { world: [true] }); + fn.addArgument('foo', undefined); + expect(fn.toAst().arguments).toMatchInlineSnapshot(` + Object { + "world": Array [ + true, + ], + } + `); + }); + test('mutates a function already associated with an expression', () => { const fn = buildExpressionFunction('hello', { world: [true] }); const exp = buildExpression([fn]); diff --git a/src/plugins/expressions/common/ast/build_function.ts b/src/plugins/expressions/common/ast/build_function.ts index 5a1bd615d6450..6cd16b2bc1354 100644 --- a/src/plugins/expressions/common/ast/build_function.ts +++ b/src/plugins/expressions/common/ast/build_function.ts @@ -183,8 +183,10 @@ export function buildExpressionFunction< acc[key] = value.map((v) => { return isExpressionAst(v) ? buildExpression(v) : v; }); - } else { + } else if (value !== undefined) { acc[key] = isExpressionAst(value) ? [buildExpression(value)] : [value]; + } else { + delete acc[key]; } return acc; }, initialArgs as FunctionBuilderArguments); @@ -195,10 +197,12 @@ export function buildExpressionFunction< arguments: args, addArgument(key, value) { - if (!args.hasOwnProperty(key)) { - args[key] = []; + if (value !== undefined) { + if (!args.hasOwnProperty(key)) { + args[key] = []; + } + args[key].push(value); } - args[key].push(value); return this; }, diff --git a/src/plugins/vis_type_metric/kibana.json b/src/plugins/vis_type_metric/kibana.json index b2ebc91471e9d..26ca09e22f26e 100644 --- a/src/plugins/vis_type_metric/kibana.json +++ b/src/plugins/vis_type_metric/kibana.json @@ -4,6 +4,6 @@ "kibanaVersion": "kibana", "server": true, "ui": true, - "requiredPlugins": ["data", "visualizations", "charts","expressions"], + "requiredPlugins": ["data", "visualizations", "charts", "expressions"], "requiredBundles": ["kibanaUtils", "kibanaReact"] } diff --git a/src/plugins/vis_type_metric/public/__snapshots__/to_ast.test.ts.snap b/src/plugins/vis_type_metric/public/__snapshots__/to_ast.test.ts.snap new file mode 100644 index 0000000000000..117c188c6cce2 --- /dev/null +++ b/src/plugins/vis_type_metric/public/__snapshots__/to_ast.test.ts.snap @@ -0,0 +1,73 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`metric vis toExpressionAst function with percentage mode should have percentage format 1`] = ` +Object { + "chain": Array [ + Object { + "arguments": Object { + "aggConfigs": Array [ + "[]", + ], + "includeFormatHints": Array [ + false, + ], + "index": Array [ + "123", + ], + "metricsAtAllLevels": Array [ + false, + ], + "partialRows": Array [ + false, + ], + }, + "function": "esaggs", + "type": "function", + }, + Object { + "arguments": Object { + "percentageMode": Array [ + true, + ], + }, + "function": "metricVis", + "type": "function", + }, + ], + "type": "expression", +} +`; + +exports[`metric vis toExpressionAst function without params 1`] = ` +Object { + "chain": Array [ + Object { + "arguments": Object { + "aggConfigs": Array [ + "[]", + ], + "includeFormatHints": Array [ + false, + ], + "index": Array [ + "123", + ], + "metricsAtAllLevels": Array [ + false, + ], + "partialRows": Array [ + false, + ], + }, + "function": "esaggs", + "type": "function", + }, + Object { + "arguments": Object {}, + "function": "metricVis", + "type": "function", + }, + ], + "type": "expression", +} +`; diff --git a/src/plugins/vis_type_metric/public/metric_vis_fn.ts b/src/plugins/vis_type_metric/public/metric_vis_fn.ts index 628abfa8c061c..b58be63581724 100644 --- a/src/plugins/vis_type_metric/public/metric_vis_fn.ts +++ b/src/plugins/vis_type_metric/public/metric_vis_fn.ts @@ -53,12 +53,14 @@ interface RenderValue { params: any; } -export const createMetricVisFn = (): ExpressionFunctionDefinition< +export type MetricVisExpressionFunctionDefinition = ExpressionFunctionDefinition< 'metricVis', Input, Arguments, Render -> => ({ +>; + +export const createMetricVisFn = (): MetricVisExpressionFunctionDefinition => ({ name: 'metricVis', type: 'render', inputTypes: ['kibana_datatable'], diff --git a/src/plugins/vis_type_metric/public/metric_vis_type.ts b/src/plugins/vis_type_metric/public/metric_vis_type.ts index 0bca7d4085152..6b4d6e151693f 100644 --- a/src/plugins/vis_type_metric/public/metric_vis_type.ts +++ b/src/plugins/vis_type_metric/public/metric_vis_type.ts @@ -18,11 +18,11 @@ */ import { i18n } from '@kbn/i18n'; - import { MetricVisOptions } from './components/metric_vis_options'; import { ColorSchemas, colorSchemas, ColorModes } from '../../charts/public'; import { AggGroupNames } from '../../data/public'; import { Schemas } from '../../vis_default_editor/public'; +import { toExpressionAst } from './to_ast'; export const createMetricVisTypeDefinition = () => ({ name: 'metric', @@ -31,6 +31,7 @@ export const createMetricVisTypeDefinition = () => ({ description: i18n.translate('visTypeMetric.metricDescription', { defaultMessage: 'Display a calculation as a single number', }), + toExpressionAst, visConfig: { defaults: { addTooltip: true, diff --git a/src/plugins/vis_type_metric/public/to_ast.test.ts b/src/plugins/vis_type_metric/public/to_ast.test.ts new file mode 100644 index 0000000000000..e741e64c0422b --- /dev/null +++ b/src/plugins/vis_type_metric/public/to_ast.test.ts @@ -0,0 +1,54 @@ +/* + * 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 { toExpressionAst } from './to_ast'; +import { Vis } from '../../visualizations/public'; + +describe('metric vis toExpressionAst function', () => { + let vis: Vis; + + beforeEach(() => { + vis = { + isHierarchical: () => false, + type: {}, + params: { + percentageMode: false, + }, + data: { + indexPattern: { id: '123' } as any, + aggs: { + getResponseAggs: () => [], + aggs: [], + } as any, + }, + } as any; + }); + + it('without params', () => { + vis.params = { metric: {} }; + const actual = toExpressionAst(vis, {}); + expect(actual).toMatchSnapshot(); + }); + + it('with percentage mode should have percentage format', () => { + vis.params = { metric: { percentageMode: true } }; + const actual = toExpressionAst(vis, {}); + expect(actual).toMatchSnapshot(); + }); +}); diff --git a/src/plugins/vis_type_metric/public/to_ast.ts b/src/plugins/vis_type_metric/public/to_ast.ts new file mode 100644 index 0000000000000..353485d48e6d6 --- /dev/null +++ b/src/plugins/vis_type_metric/public/to_ast.ts @@ -0,0 +1,103 @@ +/* + * 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 { get } from 'lodash'; +import { getVisSchemas, SchemaConfig, Vis } from '../../visualizations/public'; +import { buildExpression, buildExpressionFunction } from '../../expressions/public'; +import { MetricVisExpressionFunctionDefinition } from './metric_vis_fn'; +import { EsaggsExpressionFunctionDefinition } from '../../data/common/search/expressions'; + +const prepareDimension = (params: SchemaConfig) => { + const visdimension = buildExpressionFunction('visdimension', { accessor: params.accessor }); + + if (params.format) { + visdimension.addArgument('format', params.format.id); + visdimension.addArgument('formatParams', JSON.stringify(params.format.params)); + } + + return buildExpression([visdimension]); +}; + +export const toExpressionAst = (vis: Vis, params: any) => { + // soon this becomes: const esaggs = vis.data.aggs!.toExpressionAst(); + const esaggs = buildExpressionFunction('esaggs', { + index: vis.data.indexPattern!.id!, + metricsAtAllLevels: vis.isHierarchical(), + partialRows: vis.type.requiresPartialRows || vis.params.showPartialRows || false, + aggConfigs: JSON.stringify(vis.data.aggs!.aggs), + includeFormatHints: false, + }); + + const schemas = getVisSchemas(vis, params); + + const { + percentageMode, + useRanges, + colorSchema, + metricColorMode, + colorsRange, + labels, + invertColors, + style, + } = vis.params.metric; + + // fix formatter for percentage mode + if (get(vis.params, 'metric.percentageMode') === true) { + schemas.metric.forEach((metric: SchemaConfig) => { + metric.format = { id: 'percent' }; + }); + } + + // @ts-ignore + const metricVis = buildExpressionFunction('metricVis', { + percentageMode, + colorSchema, + colorMode: metricColorMode, + useRanges, + invertColors, + showLabels: labels && labels.show, + }); + + if (style) { + metricVis.addArgument('bgFill', style.bgFill); + metricVis.addArgument('font', buildExpression(`font size=${style.fontSize}`)); + metricVis.addArgument('subText', style.subText); + } + + if (colorsRange) { + colorsRange.forEach((range: any) => { + metricVis.addArgument( + 'colorRange', + buildExpression(`range from=${range.from} to=${range.to}`) + ); + }); + } + + if (schemas.group) { + metricVis.addArgument('bucket', prepareDimension(schemas.group[0])); + } + + schemas.metric.forEach((metric) => { + metricVis.addArgument('metric', prepareDimension(metric)); + }); + + const ast = buildExpression([esaggs, metricVis]); + + return ast.toAst(); +}; diff --git a/src/plugins/visualizations/public/index.ts b/src/plugins/visualizations/public/index.ts index c9e2ba60621e0..17c292a1b183b 100644 --- a/src/plugins/visualizations/public/index.ts +++ b/src/plugins/visualizations/public/index.ts @@ -32,6 +32,7 @@ export { Vis } from './vis'; export { TypesService } from './vis_types/types_service'; export { VISUALIZE_EMBEDDABLE_TYPE, VIS_EVENT_TO_TRIGGER } from './embeddable'; export { VisualizationContainer, VisualizationNoResults } from './components'; +export { getSchemas as getVisSchemas } from './legacy/build_pipeline'; /** @public types */ export { VisualizationsSetup, VisualizationsStart }; diff --git a/src/plugins/visualizations/public/legacy/__snapshots__/build_pipeline.test.ts.snap b/src/plugins/visualizations/public/legacy/__snapshots__/build_pipeline.test.ts.snap index 90c1d4472d5ea..df29c078d23e4 100644 --- a/src/plugins/visualizations/public/legacy/__snapshots__/build_pipeline.test.ts.snap +++ b/src/plugins/visualizations/public/legacy/__snapshots__/build_pipeline.test.ts.snap @@ -1,17 +1,11 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`visualize loader pipeline helpers: build pipeline buildPipeline calls toExpression on vis_type if it exists 1`] = `"kibana | kibana_context | testing custom expressions"`; +exports[`visualize loader pipeline helpers: build pipeline buildPipeline calls toExpression on vis_type if it exists 1`] = `"kibana | kibana_context | test"`; exports[`visualize loader pipeline helpers: build pipeline buildPipelineVisFunction handles input_control_vis function 1`] = `"input_control_vis visConfig='{\\"some\\":\\"nested\\",\\"data\\":{\\"here\\":true}}' "`; exports[`visualize loader pipeline helpers: build pipeline buildPipelineVisFunction handles markdown function 1`] = `"markdownvis '## hello _markdown_' font={font size=12} openLinksInNewTab=true "`; -exports[`visualize loader pipeline helpers: build pipeline buildPipelineVisFunction handles metric function with buckets 1`] = `"metricvis metric={visdimension 0 } metric={visdimension 1 } "`; - -exports[`visualize loader pipeline helpers: build pipeline buildPipelineVisFunction handles metric function with percentage mode should have percentage format 1`] = `"metricvis percentageMode=true metric={visdimension 0 format='percent' } "`; - -exports[`visualize loader pipeline helpers: build pipeline buildPipelineVisFunction handles metric function without buckets 1`] = `"metricvis metric={visdimension 0 } metric={visdimension 1 } "`; - exports[`visualize loader pipeline helpers: build pipeline buildPipelineVisFunction handles metrics/tsvb function 1`] = `"tsvb params='{\\"foo\\":\\"bar\\"}' uiState='{}' "`; exports[`visualize loader pipeline helpers: build pipeline buildPipelineVisFunction handles pie function 1`] = `"kibana_pie visConfig='{\\"dimensions\\":{\\"metric\\":{\\"accessor\\":0,\\"label\\":\\"\\",\\"format\\":{},\\"params\\":{},\\"aggType\\":\\"\\"},\\"buckets\\":[1,2]}}' "`; diff --git a/src/plugins/visualizations/public/legacy/build_pipeline.test.ts b/src/plugins/visualizations/public/legacy/build_pipeline.test.ts index 9ecd321963e8a..50fa5ac64e2a1 100644 --- a/src/plugins/visualizations/public/legacy/build_pipeline.test.ts +++ b/src/plugins/visualizations/public/legacy/build_pipeline.test.ts @@ -29,6 +29,7 @@ import { import { Vis } from '..'; import { dataPluginMock } from '../../../../plugins/data/public/mocks'; import { IndexPattern, IAggConfigs } from '../../../../plugins/data/public'; +import { parseExpression } from '../../../expressions/common'; describe('visualize loader pipeline helpers: build pipeline', () => { describe('prepareJson', () => { @@ -217,42 +218,6 @@ describe('visualize loader pipeline helpers: build pipeline', () => { }); }); - describe('handles metric function', () => { - it('without buckets', () => { - const params = { metric: {} }; - const schemas = { - ...schemasDef, - metric: [ - { ...schemaConfig, accessor: 0 }, - { ...schemaConfig, accessor: 1 }, - ], - }; - const actual = buildPipelineVisFunction.metric(params, schemas, uiState); - expect(actual).toMatchSnapshot(); - }); - - it('with buckets', () => { - const params = { metric: {} }; - const schemas = { - ...schemasDef, - metric: [ - { ...schemaConfig, accessor: 0 }, - { ...schemaConfig, accessor: 1 }, - ], - group: [{ accessor: 2 }], - }; - const actual = buildPipelineVisFunction.metric(params, schemas, uiState); - expect(actual).toMatchSnapshot(); - }); - - it('with percentage mode should have percentage format', () => { - const params = { metric: { percentageMode: true } }; - const schemas = { ...schemasDef }; - const actual = buildPipelineVisFunction.metric(params, schemas, uiState); - expect(actual).toMatchSnapshot(); - }); - }); - describe('handles tagcloud function', () => { it('without buckets', () => { const actual = buildPipelineVisFunction.tagcloud({}, schemasDef, uiState); @@ -331,7 +296,7 @@ describe('visualize loader pipeline helpers: build pipeline', () => { }, // @ts-ignore type: { - toExpression: () => 'testing custom expressions', + toExpressionAst: () => parseExpression('test'), }, } as unknown) as Vis; const expression = await buildPipeline(vis, { diff --git a/src/plugins/visualizations/public/legacy/build_pipeline.ts b/src/plugins/visualizations/public/legacy/build_pipeline.ts index d52e2fcc13bff..ebd240c79287a 100644 --- a/src/plugins/visualizations/public/legacy/build_pipeline.ts +++ b/src/plugins/visualizations/public/legacy/build_pipeline.ts @@ -19,7 +19,7 @@ import { get } from 'lodash'; import moment from 'moment'; -import { SerializedFieldFormat } from '../../../../plugins/expressions/public'; +import { formatExpression, SerializedFieldFormat } from '../../../../plugins/expressions/public'; import { IAggConfig, search, TimefilterContract } from '../../../../plugins/data/public'; import { Vis, VisParams } from '../types'; const { isDateHistogramBucketAggConfig } = search.aggs; @@ -80,7 +80,7 @@ const vislibCharts: string[] = [ 'line', ]; -const getSchemas = ( +export const getSchemas = ( vis: Vis, opts: { timeRange?: any; @@ -287,52 +287,6 @@ export const buildPipelineVisFunction: BuildPipelineVisFunction = { }; return `kibana_table ${prepareJson('visConfig', visConfig)}`; }, - metric: (params, schemas) => { - const { - percentageMode, - useRanges, - colorSchema, - metricColorMode, - colorsRange, - labels, - invertColors, - style, - } = params.metric; - const { metrics, bucket } = buildVisConfig.metric(schemas).dimensions; - - // fix formatter for percentage mode - if (get(params, 'metric.percentageMode') === true) { - metrics.forEach((metric: SchemaConfig) => { - metric.format = { id: 'percent' }; - }); - } - - let expr = `metricvis `; - expr += prepareValue('percentageMode', percentageMode); - expr += prepareValue('colorSchema', colorSchema); - expr += prepareValue('colorMode', metricColorMode); - expr += prepareValue('useRanges', useRanges); - expr += prepareValue('invertColors', invertColors); - expr += prepareValue('showLabels', labels && labels.show); - if (style) { - expr += prepareValue('bgFill', style.bgFill); - expr += prepareValue('font', `{font size=${style.fontSize}}`, true); - expr += prepareValue('subText', style.subText); - expr += prepareDimension('bucket', bucket); - } - - if (colorsRange) { - colorsRange.forEach((range: any) => { - expr += prepareValue('colorRange', `{range from=${range.from} to=${range.to}}`, true); - }); - } - - metrics.forEach((metric: SchemaConfig) => { - expr += prepareDimension('metric', metric); - }); - - return expr; - }, tagcloud: (params, schemas) => { const { scale, orientation, minFontSize, maxFontSize, showLabel } = params; const { metric, bucket } = buildVisConfig.tagcloud(schemas); @@ -390,14 +344,6 @@ const buildVisConfig: BuildVisConfigFunction = { } return visConfig; }, - metric: (schemas) => { - const visConfig = { dimensions: {} } as any; - visConfig.dimensions.metrics = schemas.metric; - if (schemas.group) { - visConfig.dimensions.bucket = schemas.group[0]; - } - return visConfig; - }, tagcloud: (schemas) => { const visConfig = {} as any; visConfig.metric = schemas.metric[0]; @@ -507,39 +453,46 @@ export const buildPipeline = async ( } pipeline += '| '; - // request handler - if (vis.type.requestHandler === 'courier') { - pipeline += `esaggs + if (vis.type.toExpressionAst) { + const visAst = await vis.type.toExpressionAst(vis, params); + pipeline += formatExpression(visAst); + } else { + // request handler + if (vis.type.requestHandler === 'courier') { + pipeline += `esaggs ${prepareString('index', indexPattern!.id)} metricsAtAllLevels=${vis.isHierarchical()} partialRows=${vis.type.requiresPartialRows || vis.params.showPartialRows || false} ${prepareJson('aggConfigs', vis.data.aggs!.aggs)} | `; - } + } - const schemas = getSchemas(vis, { - timeRange: params.timeRange, - timefilter: params.timefilter, - }); - if (buildPipelineVisFunction[vis.type.name]) { - pipeline += buildPipelineVisFunction[vis.type.name]({ title, ...vis.params }, schemas, uiState); - } else if (vislibCharts.includes(vis.type.name)) { - const visConfig = { ...vis.params }; - visConfig.dimensions = await buildVislibDimensions(vis, params); - - pipeline += `vislib type='${vis.type.name}' ${prepareJson('visConfig', visConfig)}`; - } else if (vis.type.toExpression) { - pipeline += await vis.type.toExpression(vis, params); - } else { - const visConfig = { ...vis.params }; - visConfig.dimensions = schemas; - pipeline += `visualization type='${vis.type.name}' + const schemas = getSchemas(vis, { + timeRange: params.timeRange, + timefilter: params.timefilter, + }); + if (buildPipelineVisFunction[vis.type.name]) { + pipeline += buildPipelineVisFunction[vis.type.name]( + { title, ...vis.params }, + schemas, + uiState + ); + } else if (vislibCharts.includes(vis.type.name)) { + const visConfig = { ...vis.params }; + visConfig.dimensions = await buildVislibDimensions(vis, params); + + pipeline += `vislib type='${vis.type.name}' ${prepareJson('visConfig', visConfig)}`; + } else { + const visConfig = { ...vis.params }; + visConfig.dimensions = schemas; + pipeline += `visualization type='${vis.type.name}' ${prepareJson('visConfig', visConfig)} metricsAtAllLevels=${vis.isHierarchical()} partialRows=${vis.type.requiresPartialRows || vis.params.showPartialRows || false} `; - if (indexPattern) { - pipeline += `${prepareString('index', indexPattern.id)} `; - if (vis.data.aggs) { - pipeline += `${prepareJson('aggConfigs', vis.data.aggs!.aggs)}`; + if (indexPattern) { + pipeline += `${prepareString('index', indexPattern.id)} `; + if (vis.data.aggs) { + pipeline += `${prepareJson('aggConfigs', vis.data.aggs!.aggs)}`; + } } } } diff --git a/src/plugins/visualizations/public/types.ts b/src/plugins/visualizations/public/types.ts index daf275297fb82..f47ffbbe921a2 100644 --- a/src/plugins/visualizations/public/types.ts +++ b/src/plugins/visualizations/public/types.ts @@ -18,7 +18,11 @@ */ import { SavedObject } from '../../../plugins/saved_objects/public'; -import { AggConfigOptions, SearchSourceFields } from '../../../plugins/data/public'; +import { + AggConfigOptions, + SearchSourceFields, + TimefilterContract, +} from '../../../plugins/data/public'; import { SerializedVis, Vis, VisParams } from './vis'; export { Vis, SerializedVis, VisParams }; @@ -60,3 +64,11 @@ export interface VisResponseValue { visConfig: object; params?: object; } + +export interface VisToExpressionAstParams { + timefilter: TimefilterContract; + timeRange?: any; + abortSignal?: AbortSignal; +} + +export type VisToExpressionAst = (vis: Vis, params: VisToExpressionAstParams) => string; diff --git a/src/plugins/visualizations/public/vis.ts b/src/plugins/visualizations/public/vis.ts index e8ae48cdce145..b4fc9df1c6ecc 100644 --- a/src/plugins/visualizations/public/vis.ts +++ b/src/plugins/visualizations/public/vis.ts @@ -202,8 +202,8 @@ export class Vis { }; } - toAST() { - return this.type.toAST(this.params); + toExpressionAst() { + return this.type.toExpressionAst(this.params); } // deprecated diff --git a/src/plugins/visualizations/public/vis_types/base_vis_type.ts b/src/plugins/visualizations/public/vis_types/base_vis_type.ts index 09a8ae1683038..fa0bbfc5e250a 100644 --- a/src/plugins/visualizations/public/vis_types/base_vis_type.ts +++ b/src/plugins/visualizations/public/vis_types/base_vis_type.ts @@ -18,7 +18,7 @@ */ import _ from 'lodash'; -import { VisualizationControllerConstructor } from '../types'; +import { VisToExpressionAst, VisualizationControllerConstructor } from '../types'; import { TriggerContextMapping } from '../../../ui_actions/public'; import { Adapters } from '../../../inspector/public'; @@ -42,6 +42,7 @@ export interface BaseVisTypeOptions { setup?: unknown; useCustomNoDataScreen?: boolean; inspectorAdapters?: Adapters | (() => Adapters); + toExpressionAst?: VisToExpressionAst; } export class BaseVisType { @@ -66,6 +67,7 @@ export class BaseVisType { setup?: unknown; useCustomNoDataScreen: boolean; inspectorAdapters?: Adapters | (() => Adapters); + toExpressionAst?: VisToExpressionAst; constructor(opts: BaseVisTypeOptions) { if (!opts.icon && !opts.image) { @@ -102,6 +104,7 @@ export class BaseVisType { this.hierarchicalData = opts.hierarchicalData || false; this.useCustomNoDataScreen = opts.useCustomNoDataScreen || false; this.inspectorAdapters = opts.inspectorAdapters; + this.toExpressionAst = opts.toExpressionAst; } public get schemas() {