From c7769ce1a966782b75690ba7ea0b99e9e93e5cf2 Mon Sep 17 00:00:00 2001 From: Yaroslav Kuznietsov Date: Fri, 28 Oct 2022 18:30:03 +0300 Subject: [PATCH] [Lens] Gauge expression types improvement. (#144168) * Provided expression type. * FIxed types. --- .../public/visualizations/gauge/constants.ts | 2 +- .../gauge/visualization.test.ts | 2 - .../visualizations/gauge/visualization.tsx | 60 +++++++------------ 3 files changed, 23 insertions(+), 41 deletions(-) diff --git a/x-pack/plugins/lens/public/visualizations/gauge/constants.ts b/x-pack/plugins/lens/public/visualizations/gauge/constants.ts index 3c6ab9f8471b8..29ee228848163 100644 --- a/x-pack/plugins/lens/public/visualizations/gauge/constants.ts +++ b/x-pack/plugins/lens/public/visualizations/gauge/constants.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { GaugeState as GaugeStateOriginal } from '@kbn/expression-gauge-plugin/common'; +import type { GaugeState as GaugeStateOriginal } from '@kbn/expression-gauge-plugin/common'; import { LayerType } from '../../../common'; export const LENS_GAUGE_ID = 'lnsGauge'; diff --git a/x-pack/plugins/lens/public/visualizations/gauge/visualization.test.ts b/x-pack/plugins/lens/public/visualizations/gauge/visualization.test.ts index 1ecedc1a63c41..38580d33163ca 100644 --- a/x-pack/plugins/lens/public/visualizations/gauge/visualization.test.ts +++ b/x-pack/plugins/lens/public/visualizations/gauge/visualization.test.ts @@ -570,8 +570,6 @@ describe('gauge', () => { ticksPosition: ['auto'], labelMajorMode: ['auto'], labelMinor: ['Subtitle'], - labelMajor: [], - palette: [], shape: ['horizontalBullet'], }, }, diff --git a/x-pack/plugins/lens/public/visualizations/gauge/visualization.tsx b/x-pack/plugins/lens/public/visualizations/gauge/visualization.tsx index 803e4e30acb59..684703a7e0011 100644 --- a/x-pack/plugins/lens/public/visualizations/gauge/visualization.tsx +++ b/x-pack/plugins/lens/public/visualizations/gauge/visualization.tsx @@ -12,10 +12,10 @@ import { ThemeServiceStart } from '@kbn/core/public'; import { KibanaThemeProvider } from '@kbn/kibana-react-plugin/public'; import { FormattedMessage, I18nProvider } from '@kbn/i18n-react'; import { Ast } from '@kbn/interpreter'; -import { DatatableRow } from '@kbn/expressions-plugin/common'; +import { buildExpressionFunction, DatatableRow } from '@kbn/expressions-plugin/common'; import { PaletteRegistry, CustomPaletteParams, CUSTOM_PALETTE } from '@kbn/coloring'; -import type { GaugeArguments } from '@kbn/expression-gauge-plugin/common'; -import { GaugeShapes, EXPRESSION_GAUGE_NAME } from '@kbn/expression-gauge-plugin/common'; +import type { GaugeExpressionFunctionDefinition } from '@kbn/expression-gauge-plugin/common'; +import { GaugeShapes } from '@kbn/expression-gauge-plugin/common'; import { getGoalValue, getMaxValue, @@ -27,12 +27,7 @@ import { LayerTypes } from '@kbn/expression-xy-plugin/public'; import type { FormBasedPersistedState } from '../../datasources/form_based/types'; import type { DatasourceLayers, OperationMetadata, Suggestion, Visualization } from '../../types'; import { getSuggestions } from './suggestions'; -import { - GROUP_ID, - LENS_GAUGE_ID, - GaugeVisualizationState, - GaugeExpressionState, -} from './constants'; +import { GROUP_ID, LENS_GAUGE_ID, GaugeVisualizationState } from './constants'; import { GaugeToolbar } from './toolbar_component'; import { applyPaletteParams } from '../../shared_components'; import { GaugeDimensionEditor } from './dimension_editor'; @@ -116,7 +111,7 @@ const toExpression = ( paletteService: PaletteRegistry, state: GaugeVisualizationState, datasourceLayers: DatasourceLayers, - attributes?: Partial>, + attributes?: unknown, datasourceExpressionsByLayers: Record | undefined = {} ): Ast | null => { const datasource = datasourceLayers[state.layerId]; @@ -127,36 +122,25 @@ const toExpression = ( return null; } + const gaugeFn = buildExpressionFunction('gauge', { + metric: state.metricAccessor, + min: state.minAccessor, + max: state.maxAccessor, + goal: state.goalAccessor, + shape: state.shape ?? GaugeShapes.HORIZONTAL_BULLET, + colorMode: state?.colorMode ?? 'none', + palette: state.palette?.params + ? paletteService.get(CUSTOM_PALETTE).toExpression(computePaletteParams(state.palette.params)) + : undefined, + ticksPosition: state.ticksPosition ?? 'auto', + labelMinor: state.labelMinor, + labelMajor: state.labelMajor, + labelMajorMode: state.labelMajorMode ?? 'auto', + }); + return { type: 'expression', - chain: [ - ...(datasourceExpression?.chain ?? []), - { - type: 'function', - function: EXPRESSION_GAUGE_NAME, - arguments: { - metric: state.metricAccessor ? [state.metricAccessor] : [], - min: state.minAccessor ? [state.minAccessor] : [], - max: state.maxAccessor ? [state.maxAccessor] : [], - goal: state.goalAccessor ? [state.goalAccessor] : [], - shape: [state.shape ?? GaugeShapes.HORIZONTAL_BULLET], - colorMode: [state?.colorMode ?? 'none'], - palette: state.palette?.params - ? [ - paletteService - .get(CUSTOM_PALETTE) - .toExpression( - computePaletteParams((state.palette?.params || {}) as CustomPaletteParams) - ), - ] - : [], - ticksPosition: state.ticksPosition ? [state.ticksPosition] : ['auto'], - labelMinor: state.labelMinor ? [state.labelMinor] : [], - labelMajor: state.labelMajor ? [state.labelMajor] : [], - labelMajorMode: state.labelMajorMode ? [state.labelMajorMode] : ['auto'], - }, - }, - ], + chain: [...(datasourceExpression?.chain ?? []), gaugeFn.toAst()], }; };