-
Notifications
You must be signed in to change notification settings - Fork 29
/
processor.ts
306 lines (294 loc) · 10.3 KB
/
processor.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import { InsertTime } from './deframer';
import { useTheme2 } from '@grafana/ui';
import {
Field,
FieldType,
reduceField,
textUtil,
DataFrame,
PanelData,
getFieldDisplayName,
formattedValueToString,
getValueFormat,
stringToJsRegex,
InterpolateFunction,
FieldConfigSource,
ScopedVars,
} from '@grafana/data';
import { getTemplateSrv } from '@grafana/runtime';
import { includes as lodashIncludes } from 'lodash';
import { DisplayModes, OperatorOptions, PolystatModel } from '../components/types';
import { GLOBAL_FILL_COLOR_RGBA } from '../components/defaults';
import { GetDecimalsForValue, SortVariableValuesByField } from '../utils';
import { ApplyComposites } from './composite_processor';
import { CompositeItemType } from 'components/composites/types';
import { ApplyOverrides } from './override_processor';
import { OverrideItemType } from '../components/overrides/types';
import { PolystatThreshold } from '../components/thresholds/types';
import { ClickThroughTransformer } from './clickThroughTransformer';
import { GetMappedValue } from './valueMappingsWrapper';
import { GetValueByOperator } from './stats';
/**
* Converts dataframes to internal model
*
* @param {PanelData} data [data description]
* @param {string[]} globalRegexPattern [globalRegexPattern description]
*
* @return {DataFrame[]} [return description]
*/
export function ProcessDataFrames(
compositesEnabled: boolean,
composites: CompositeItemType[],
overrides: OverrideItemType[],
data: PanelData,
replaceVariables: InterpolateFunction,
fieldConfig: FieldConfigSource<any>,
globalClickthrough: string,
globalClickthroughTabEnabled: boolean,
globalClickthroughSanitizedEnabled: boolean,
globalOperator: string,
globalDecimals: number,
globalDisplayMode: string,
globalRegexPattern: string,
globalFillColor: string,
globalThresholds: PolystatThreshold[],
globalUnitFormat: string,
sortByDirection: number,
sortByField: string
): PolystatModel[] {
// check if data contains a field called Time of type time
let processedData = InsertTime(data.series);
let internalData = [] as PolystatModel[];
// just one for now...
processedData.map((item) => {
const models = DataFrameToPolystat(item, globalOperator);
for (const aModel of models) {
internalData.push(aModel);
}
});
internalData = ApplyGlobalRegexPattern(internalData, globalRegexPattern);
// formatting can change colors due to value maps
internalData = ApplyGlobalFormatting(internalData, fieldConfig, globalUnitFormat, globalDecimals, globalFillColor);
// applies overrides and global thresholds (and mappings)
internalData = ApplyOverrides(
overrides,
internalData,
fieldConfig,
globalFillColor,
globalThresholds,
replaceVariables
);
// composites
if (compositesEnabled) {
internalData = ApplyComposites(composites, internalData, replaceVariables);
}
// clickthroughs
internalData = ApplyGlobalClickThrough(
internalData,
globalClickthrough,
globalClickthroughTabEnabled,
globalClickthroughSanitizedEnabled
);
// filter by global display mode
internalData = FilterByGlobalDisplayMode(internalData, globalDisplayMode);
// final step sorting
internalData = SortVariableValuesByField(internalData, sortByField, sortByDirection);
return internalData;
}
/**
* renames according to a global regex pattern
*
* @param {DataFrame[]} series [series description]
* @param {string[]} regexPattern [regexPattern description]
*
* @return {DataFrame[]} [return description]
*/
export const ApplyGlobalRegexPattern = (data: PolystatModel[], regexPattern: string) => {
for (let i = 0; i < data.length; i++) {
if (regexPattern !== '') {
const regexVal = stringToJsRegex(regexPattern);
if (data[i].name && regexVal.test(data[i].name.toString())) {
const temp = regexVal.exec(data[i].name.toString());
if (!temp) {
continue;
}
let extractedTxt = '';
if (temp.length > 1) {
temp.slice(1).forEach((value, i) => {
if (value) {
extractedTxt += extractedTxt.length > 0 ? ' ' + value.toString() : value.toString();
}
});
data[i].displayName = extractedTxt;
}
}
}
}
return data;
};
export const ApplyGlobalClickThrough = (
data: PolystatModel[],
globalClickthrough: string,
globalClickthroughNewTabEnabled: boolean,
globalClickthroughSanitizedEnabled: boolean
) => {
for (let index = 0; index < data.length; index++) {
if (data[index].clickThrough.length === 0) {
data[index].clickThrough = processDefaultClickThrough(index, globalClickthrough, data);
data[index].newTabEnabled = globalClickthroughNewTabEnabled;
data[index].sanitizeURLEnabled = globalClickthroughSanitizedEnabled;
// always provide both versions and overrides and composites can specify which one to use
data[index].sanitizedURL = textUtil.sanitize(data[index].clickThrough);
}
}
return data;
};
const processDefaultClickThrough = (index: number, globalClickthrough: string, data: PolystatModel[]): string => {
let url = globalClickthrough;
// apply both types of transforms, one targeted at the data item index, and secondly the nth variant
url = ClickThroughTransformer.transformSingleMetric(index, url, data);
url = ClickThroughTransformer.transformNthMetric(url, data);
// process template variables inside clickthrough
const templateVars: ScopedVars = {};
url = getTemplateSrv().replace(url, templateVars);
return url;
};
export const ApplyGlobalFormatting = (
data: PolystatModel[],
fieldConfig: FieldConfigSource<any>,
globalUnitFormat: string,
globalDecimals: number,
globalFillColor: string
) => {
const theme = useTheme2();
let realGlobalFillColor = theme.visualization.getColorByName(globalFillColor);
const formatFunc = getValueFormat(globalUnitFormat);
for (let index = 0; index < data.length; index++) {
// Check for mapped value, if nothing set, format value
if (data[index].value !== null) {
const mappedValue = GetMappedValue(fieldConfig.defaults.mappings!, data[index].value);
if (mappedValue && mappedValue.text !== '') {
data[index].valueFormatted = mappedValue.text;
// set color also
if (mappedValue.color) {
let realColor = theme.visualization.getColorByName(mappedValue.color);
data[index].color = realColor;
} else {
data[index].color = realGlobalFillColor;
}
} else {
if (formatFunc) {
const result = GetDecimalsForValue(data[index].value, globalDecimals);
const formatted = formatFunc(data[index].value, result.decimals, result.scaledDecimals);
data[index].valueFormatted = formatted.text;
if (formatted.suffix) {
data[index].valueFormatted += ` ${formatted.suffix}`;
}
if (formatted.prefix) {
data[index].valueFormatted = `{$formatted.prefix} ${data[index].valueFormatted}`;
}
const valueRounded = roundValue(data[index].value, result.decimals);
if (valueRounded !== null) {
data[index].valueRounded = valueRounded;
}
}
data[index].color = realGlobalFillColor;
}
}
}
return data;
};
const FilterByGlobalDisplayMode = (data: any, globalDisplayMode: string) => {
const filteredMetrics: number[] = [];
const compositeMetrics: PolystatModel[] = [];
if (globalDisplayMode !== 'all') {
const dataLen = data.length;
for (let i = 0; i < dataLen; i++) {
const item = data[i];
// keep if composite
if (item.isComposite) {
compositeMetrics.push(item);
}
if (item.thresholdLevel < 1) {
// push the index number
filteredMetrics.push(i);
}
}
// remove filtered metrics, use splice in reverse order
for (let i = data.length; i >= 0; i--) {
if (lodashIncludes(filteredMetrics, i)) {
data.splice(i, 1);
}
}
if (data.length === 0) {
if (compositeMetrics.length > 0) {
// set data to be all of the composites
data = compositeMetrics;
}
}
}
return data;
};
const roundValue = (num: number, decimals: number) => {
if (num === null) {
return null;
}
const n = Math.pow(10, decimals);
const formatted = (n * num).toFixed(decimals);
return Math.round(parseFloat(formatted)) / n;
};
export function DataFrameToPolystat(frame: DataFrame, globalOperator: string): PolystatModel[] {
/*
const shortenValue = (value: string, length: number) => {
if (value.length > length) {
return value.substring(0, length).concat('...');
} else {
return value;
}
};
*/
const valueFields: Field[] = [];
for (const aField of frame.fields) {
if (aField.type === FieldType.number) {
valueFields.push(aField);
}
}
const models: PolystatModel[] = [];
for (const valueField of valueFields) {
const standardCalcs = reduceField({ field: valueField!, reducers: ['bogus'] });
//const x = getDisplayProcessor({ field: valueField, theme: useTheme2() });
const valueFieldName = getFieldDisplayName(valueField!, frame);
const operatorValue = GetValueByOperator(valueFieldName, null, globalOperator, standardCalcs);
let maxDecimals = 4;
if (valueField!.config.decimals !== undefined && valueField!.config.decimals !== null) {
maxDecimals = valueField!.config.decimals;
}
const result = getValueFormat(valueField!.config.unit)(operatorValue, maxDecimals, undefined, undefined);
const valueFormatted = formattedValueToString(result);
const model: PolystatModel = {
displayMode: DisplayModes[0].value,
thresholdLevel: 0,
value: operatorValue,
valueFormatted: valueFormatted,
valueRounded: roundValue(operatorValue, maxDecimals) || operatorValue,
stats: standardCalcs,
name: valueFieldName, // aSeries.name,
displayName: valueFieldName, // aSeries.name,
timestamp: 0,
prefix: '',
suffix: '',
color: GLOBAL_FILL_COLOR_RGBA,
clickThrough: '',
operatorName: OperatorOptions[0].value,
newTabEnabled: true,
sanitizedURL: '',
sanitizeURLEnabled: true,
showName: true,
showValue: true,
isComposite: false,
members: [],
};
models.push(model);
}
return models;
}