Skip to content

Commit

Permalink
[Lens] Gauge expression types improvement. (#144168)
Browse files Browse the repository at this point in the history
* Provided expression type.

* FIxed types.
  • Loading branch information
Kuznietsov authored Oct 28, 2022
1 parent 30fcc1d commit c7769ce
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -570,8 +570,6 @@ describe('gauge', () => {
ticksPosition: ['auto'],
labelMajorMode: ['auto'],
labelMinor: ['Subtitle'],
labelMajor: [],
palette: [],
shape: ['horizontalBullet'],
},
},
Expand Down
60 changes: 22 additions & 38 deletions x-pack/plugins/lens/public/visualizations/gauge/visualization.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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';
Expand Down Expand Up @@ -116,7 +111,7 @@ const toExpression = (
paletteService: PaletteRegistry,
state: GaugeVisualizationState,
datasourceLayers: DatasourceLayers,
attributes?: Partial<Omit<GaugeArguments, keyof GaugeExpressionState | 'ariaLabel'>>,
attributes?: unknown,
datasourceExpressionsByLayers: Record<string, Ast> | undefined = {}
): Ast | null => {
const datasource = datasourceLayers[state.layerId];
Expand All @@ -127,36 +122,25 @@ const toExpression = (
return null;
}

const gaugeFn = buildExpressionFunction<GaugeExpressionFunctionDefinition>('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()],
};
};

Expand Down

0 comments on commit c7769ce

Please sign in to comment.