mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 09:48:58 -04:00
[Lens] Metric expression types improvement. (#144239)
* Added metricTrendlineFn. * Added metricVis type safety.
This commit is contained in:
parent
a6ab758e5f
commit
5abc674b06
3 changed files with 69 additions and 80 deletions
|
@ -14,6 +14,7 @@ export type {
|
|||
MetricInput,
|
||||
MetricVisRenderConfig,
|
||||
MetricVisExpressionFunctionDefinition,
|
||||
TrendlineExpressionFunctionDefinition,
|
||||
DimensionsVisParam,
|
||||
MetricVisParam,
|
||||
VisParams,
|
||||
|
|
|
@ -5,12 +5,13 @@
|
|||
* 2.0.
|
||||
*/
|
||||
|
||||
import { LayoutDirection } from '@elastic/charts';
|
||||
import { CustomPaletteParams, CUSTOM_PALETTE, PaletteRegistry } from '@kbn/coloring';
|
||||
import {
|
||||
EXPRESSION_METRIC_NAME,
|
||||
EXPRESSION_METRIC_TRENDLINE_NAME,
|
||||
} from '@kbn/expression-metric-vis-plugin/public';
|
||||
import { buildExpressionFunction } from '@kbn/expressions-plugin/common';
|
||||
import type {
|
||||
TrendlineExpressionFunctionDefinition,
|
||||
MetricVisExpressionFunctionDefinition,
|
||||
} from '@kbn/expression-metric-vis-plugin/common';
|
||||
import { buildExpression, buildExpressionFunction } from '@kbn/expressions-plugin/common';
|
||||
import { Ast } from '@kbn/interpreter';
|
||||
import { CollapseArgs, CollapseFunction } from '../../../common/expressions';
|
||||
import { CollapseExpressionFunction } from '../../../common/expressions/collapse/types';
|
||||
|
@ -33,51 +34,47 @@ const getTrendlineExpression = (
|
|||
state: MetricVisualizationState,
|
||||
datasourceExpressionsByLayers: Record<string, Ast>
|
||||
): Ast | undefined => {
|
||||
if (!state.trendlineLayerId || !state.trendlineMetricAccessor || !state.trendlineTimeAccessor) {
|
||||
const { trendlineLayerId, trendlineMetricAccessor, trendlineTimeAccessor } = state;
|
||||
if (!trendlineLayerId || !trendlineMetricAccessor || !trendlineTimeAccessor) {
|
||||
return;
|
||||
}
|
||||
|
||||
const datasourceExpression = datasourceExpressionsByLayers[state.trendlineLayerId];
|
||||
const datasourceExpression = datasourceExpressionsByLayers[trendlineLayerId];
|
||||
|
||||
return {
|
||||
type: 'expression',
|
||||
chain: [
|
||||
{
|
||||
type: 'function',
|
||||
function: EXPRESSION_METRIC_TRENDLINE_NAME,
|
||||
arguments: {
|
||||
metric: [state.trendlineMetricAccessor],
|
||||
timeField: [state.trendlineTimeAccessor],
|
||||
breakdownBy:
|
||||
state.trendlineBreakdownByAccessor && !state.collapseFn
|
||||
? [state.trendlineBreakdownByAccessor]
|
||||
: [],
|
||||
inspectorTableId: [state.trendlineLayerId],
|
||||
...(datasourceExpression
|
||||
? {
|
||||
table: [
|
||||
{
|
||||
...datasourceExpression,
|
||||
chain: [
|
||||
...datasourceExpression.chain,
|
||||
...(state.collapseFn
|
||||
? [
|
||||
buildExpressionFunction<CollapseExpressionFunction>('lens_collapse', {
|
||||
by: [state.trendlineTimeAccessor],
|
||||
metric: [state.trendlineMetricAccessor],
|
||||
fn: [state.collapseFn],
|
||||
}).toAst(),
|
||||
]
|
||||
: []),
|
||||
],
|
||||
},
|
||||
],
|
||||
}
|
||||
: {}),
|
||||
if (!datasourceExpression) {
|
||||
return;
|
||||
}
|
||||
|
||||
const metricTrendlineFn = buildExpressionFunction<TrendlineExpressionFunctionDefinition>(
|
||||
'metricTrendline',
|
||||
{
|
||||
metric: trendlineMetricAccessor,
|
||||
timeField: trendlineTimeAccessor,
|
||||
breakdownBy:
|
||||
state.trendlineBreakdownByAccessor && !state.collapseFn
|
||||
? state.trendlineBreakdownByAccessor
|
||||
: undefined,
|
||||
inspectorTableId: trendlineLayerId,
|
||||
table: [
|
||||
{
|
||||
...datasourceExpression,
|
||||
chain: [
|
||||
...datasourceExpression.chain,
|
||||
...(state.collapseFn
|
||||
? [
|
||||
buildExpressionFunction<CollapseExpressionFunction>('lens_collapse', {
|
||||
by: [trendlineTimeAccessor],
|
||||
metric: [trendlineMetricAccessor],
|
||||
fn: [state.collapseFn],
|
||||
}).toAst(),
|
||||
]
|
||||
: []),
|
||||
],
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
],
|
||||
}
|
||||
);
|
||||
return buildExpression([metricTrendlineFn]).toAst();
|
||||
};
|
||||
|
||||
export const toExpression = (
|
||||
|
@ -135,38 +132,35 @@ export const toExpression = (
|
|||
|
||||
const trendlineExpression = getTrendlineExpression(state, datasourceExpressionsByLayers);
|
||||
|
||||
const metricFn = buildExpressionFunction<MetricVisExpressionFunctionDefinition>('metricVis', {
|
||||
metric: state.metricAccessor,
|
||||
secondaryMetric: state.secondaryMetricAccessor,
|
||||
secondaryPrefix: state.secondaryPrefix,
|
||||
max: showingBar(state) ? state.maxAccessor : undefined,
|
||||
breakdownBy:
|
||||
state.breakdownByAccessor && !state.collapseFn ? state.breakdownByAccessor : undefined,
|
||||
trendline: trendlineExpression ? [trendlineExpression] : [],
|
||||
subtitle: state.subtitle ?? undefined,
|
||||
progressDirection: state.progressDirection as LayoutDirection,
|
||||
color: state.color || getDefaultColor(state),
|
||||
palette: state.palette?.params
|
||||
? [
|
||||
paletteService
|
||||
.get(CUSTOM_PALETTE)
|
||||
.toExpression(computePaletteParams(state.palette.params as CustomPaletteParams)),
|
||||
]
|
||||
: [],
|
||||
maxCols: state.maxCols ?? DEFAULT_MAX_COLUMNS,
|
||||
minTiles: maxPossibleTiles ?? undefined,
|
||||
inspectorTableId: state.layerId,
|
||||
});
|
||||
|
||||
return {
|
||||
type: 'expression',
|
||||
chain: [
|
||||
...(datasourceExpression?.chain ?? []),
|
||||
...(collapseExpressionFunction ? [collapseExpressionFunction] : []),
|
||||
{
|
||||
type: 'function',
|
||||
function: EXPRESSION_METRIC_NAME,
|
||||
arguments: {
|
||||
metric: state.metricAccessor ? [state.metricAccessor] : [],
|
||||
secondaryMetric: state.secondaryMetricAccessor ? [state.secondaryMetricAccessor] : [],
|
||||
secondaryPrefix:
|
||||
typeof state.secondaryPrefix !== 'undefined' ? [state.secondaryPrefix] : [],
|
||||
max: showingBar(state) ? [state.maxAccessor] : [],
|
||||
breakdownBy:
|
||||
state.breakdownByAccessor && !state.collapseFn ? [state.breakdownByAccessor] : [],
|
||||
trendline: trendlineExpression ? [trendlineExpression] : [],
|
||||
subtitle: state.subtitle ? [state.subtitle] : [],
|
||||
progressDirection: state.progressDirection ? [state.progressDirection] : [],
|
||||
color: [state.color || getDefaultColor(state)],
|
||||
palette: state.palette?.params
|
||||
? [
|
||||
paletteService
|
||||
.get(CUSTOM_PALETTE)
|
||||
.toExpression(computePaletteParams(state.palette.params as CustomPaletteParams)),
|
||||
]
|
||||
: [],
|
||||
maxCols: [state.maxCols ?? DEFAULT_MAX_COLUMNS],
|
||||
minTiles: maxPossibleTiles ? [maxPossibleTiles] : [],
|
||||
inspectorTableId: [state.layerId],
|
||||
},
|
||||
},
|
||||
metricFn.toAst(),
|
||||
],
|
||||
};
|
||||
};
|
||||
|
|
|
@ -300,21 +300,18 @@ describe('metric visualization', () => {
|
|||
"chain": Array [
|
||||
Object {
|
||||
"arguments": Object {
|
||||
"breakdownBy": Array [],
|
||||
"color": Array [
|
||||
"static-color",
|
||||
],
|
||||
"inspectorTableId": Array [
|
||||
"first",
|
||||
],
|
||||
"max": Array [],
|
||||
"maxCols": Array [
|
||||
5,
|
||||
],
|
||||
"metric": Array [
|
||||
"metric-col-id",
|
||||
],
|
||||
"minTiles": Array [],
|
||||
"palette": Array [
|
||||
Object {
|
||||
"chain": Array [
|
||||
|
@ -370,7 +367,6 @@ describe('metric visualization', () => {
|
|||
"inspectorTableId": Array [
|
||||
"first",
|
||||
],
|
||||
"max": Array [],
|
||||
"maxCols": Array [
|
||||
5,
|
||||
],
|
||||
|
@ -473,7 +469,6 @@ describe('metric visualization', () => {
|
|||
"chain": Array [
|
||||
Object {
|
||||
"arguments": Object {
|
||||
"breakdownBy": Array [],
|
||||
"inspectorTableId": Array [
|
||||
"second",
|
||||
],
|
||||
|
@ -520,7 +515,6 @@ describe('metric visualization', () => {
|
|||
"chain": Array [
|
||||
Object {
|
||||
"arguments": Object {
|
||||
"breakdownBy": Array [],
|
||||
"inspectorTableId": Array [
|
||||
"second",
|
||||
],
|
||||
|
@ -608,8 +602,8 @@ describe('metric visualization', () => {
|
|||
"type": "function",
|
||||
}
|
||||
`);
|
||||
expect(ast.chain[1].arguments.minTiles).toHaveLength(0);
|
||||
expect(ast.chain[1].arguments.breakdownBy).toHaveLength(0);
|
||||
expect(ast.chain[1].arguments.minTiles).toBeUndefined();
|
||||
expect(ast.chain[1].arguments.breakdownBy).toBeUndefined();
|
||||
});
|
||||
|
||||
it('always applies max function to static max dimensions', () => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue