-
Notifications
You must be signed in to change notification settings - Fork 916
/
to_expression.ts
151 lines (123 loc) · 4.19 KB
/
to_expression.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
import { SchemaConfig } from '../../../../visualizations/public';
import { MetricVisExpressionFunctionDefinition } from '../../../../vis_type_metric/public';
import { AggConfigs, IAggConfig } from '../../../../data/common';
import { buildExpression, buildExpressionFunction } from '../../../../expressions/public';
import { RenderState } from '../../application/utils/state_management';
import { MetricOptionsDefaults } from './metric_viz_type';
import { getAggExpressionFunctions } from '../common/expression_helpers';
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]);
};
// TODO: Update to the common getShemas from src/plugins/visualizations/public/legacy/build_pipeline.ts
// And move to a common location accessible by all the visualizations
const getVisSchemas = (aggConfigs: AggConfigs): any => {
const createSchemaConfig = (accessor: number, agg: IAggConfig): SchemaConfig => {
const hasSubAgg = [
'derivative',
'moving_avg',
'serial_diff',
'cumulative_sum',
'sum_bucket',
'avg_bucket',
'min_bucket',
'max_bucket',
].includes(agg.type.name);
const formatAgg = hasSubAgg
? agg.params.customMetric || agg.aggConfigs.getRequestAggById(agg.params.metricAgg)
: agg;
const params = {};
const label = agg.makeLabel && agg.makeLabel();
return {
accessor,
format: formatAgg.toSerializedFieldFormat(),
params,
label,
aggType: agg.type.name,
};
};
let cnt = 0;
const schemas: any = {
metric: [],
};
if (!aggConfigs) {
return schemas;
}
const responseAggs = aggConfigs.getResponseAggs();
responseAggs.forEach((agg) => {
const schemaName = agg.schema;
if (!schemaName) {
cnt++;
return;
}
if (!schemas[schemaName]) {
schemas[schemaName] = [];
}
schemas[schemaName]!.push(createSchemaConfig(cnt++, agg));
});
return schemas;
};
export interface MetricRootState extends RenderState {
style: MetricOptionsDefaults;
}
export const toExpression = async ({ style: styleState, visualization }: MetricRootState) => {
const { aggConfigs, expressionFns } = await getAggExpressionFunctions(visualization);
// TODO: Update to use the getVisSchemas function from the Visualizations plugin
// const schemas = getVisSchemas(vis, params);
const {
percentageMode,
useRanges,
colorSchema,
metricColorMode,
colorsRange,
labels,
invertColors,
style,
} = styleState.metric;
const schemas = getVisSchemas(aggConfigs);
// fix formatter for percentage mode
if (percentageMode === true) {
schemas.metric.forEach((metric: SchemaConfig) => {
metric.format = { id: 'percent' };
});
}
// TODO: ExpressionFunctionDefinitions mark all arguments as required even though the function marks most as optional
// Update buildExpressionFunction to correctly handle optional arguments
// @ts-expect-error
const metricVis = buildExpressionFunction<MetricVisExpressionFunctionDefinition>('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: SchemaConfig) => {
metricVis.addArgument('metric', prepareDimension(metric));
});
return buildExpression([...expressionFns, metricVis]).toString();
};