forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemplate_srv.ts
374 lines (308 loc) · 10.5 KB
/
template_srv.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import { escape, isString, property } from 'lodash';
import {
deprecationWarning,
ScopedVars,
TimeRange,
AdHocVariableFilter,
AdHocVariableModel,
TypedVariableModel,
ScopedVar,
} from '@grafana/data';
import {
getDataSourceSrv,
setTemplateSrv,
TemplateSrv as BaseTemplateSrv,
VariableInterpolation,
} from '@grafana/runtime';
import { sceneGraph, VariableCustomFormatterFn } from '@grafana/scenes';
import { VariableFormatID } from '@grafana/schema';
import { variableAdapters } from '../variables/adapters';
import { ALL_VARIABLE_TEXT, ALL_VARIABLE_VALUE } from '../variables/constants';
import { isAdHoc } from '../variables/guard';
import { getFilteredVariables, getVariables, getVariableWithName } from '../variables/state/selectors';
import { variableRegex } from '../variables/utils';
import { formatVariableValue } from './formatVariableValue';
import { macroRegistry } from './macroRegistry';
interface FieldAccessorCache {
[key: string]: (obj: any) => any;
}
/**
* Internal regex replace function
*/
type ReplaceFunction = (fullMatch: string, variableName: string, fieldPath: string, format: string) => string;
export interface TemplateSrvDependencies {
getFilteredVariables: typeof getFilteredVariables;
getVariables: typeof getVariables;
getVariableWithName: typeof getVariableWithName;
}
const runtimeDependencies: TemplateSrvDependencies = {
getFilteredVariables,
getVariables,
getVariableWithName,
};
export class TemplateSrv implements BaseTemplateSrv {
private _variables: any[];
private regex = variableRegex;
private index: any = {};
private grafanaVariables = new Map<string, any>();
private timeRange?: TimeRange | null = null;
private fieldAccessorCache: FieldAccessorCache = {};
constructor(private dependencies: TemplateSrvDependencies = runtimeDependencies) {
this._variables = [];
}
init(variables: any, timeRange?: TimeRange) {
this._variables = variables;
this.timeRange = timeRange;
this.updateIndex();
}
/**
* @deprecated: this instance variable should not be used and will be removed in future releases
*
* Use getVariables function instead
*/
get variables(): any[] {
deprecationWarning('template_srv.ts', 'variables', 'getVariables');
return this.getVariables();
}
getVariables(): TypedVariableModel[] {
return this.dependencies.getVariables();
}
updateIndex() {
const existsOrEmpty = (value: any) => value || value === '';
this.index = this._variables.reduce((acc, currentValue) => {
if (currentValue.current && (currentValue.current.isNone || existsOrEmpty(currentValue.current.value))) {
acc[currentValue.name] = currentValue;
}
return acc;
}, {});
if (this.timeRange) {
const from = this.timeRange.from.valueOf().toString();
const to = this.timeRange.to.valueOf().toString();
this.index = {
...this.index,
['__from']: {
current: { value: from, text: from },
},
['__to']: {
current: { value: to, text: to },
},
};
}
}
updateTimeRange(timeRange: TimeRange) {
this.timeRange = timeRange;
this.updateIndex();
}
variableInitialized(variable: any) {
this.index[variable.name] = variable;
}
getAdhocFilters(datasourceName: string): AdHocVariableFilter[] {
let filters: any = [];
let ds = getDataSourceSrv().getInstanceSettings(datasourceName);
if (!ds) {
return [];
}
for (const variable of this.getAdHocVariables()) {
const variableUid = variable.datasource?.uid;
if (variableUid === ds.uid) {
filters = filters.concat(variable.filters);
} else if (variableUid?.indexOf('$') === 0) {
if (this.replace(variableUid) === datasourceName) {
filters = filters.concat(variable.filters);
}
}
}
return filters;
}
setGrafanaVariable(name: string, value: any) {
this.grafanaVariables.set(name, value);
}
/**
* @deprecated: setGlobalVariable function should not be used and will be removed in future releases
*
* Use addVariable action to add variables to Redux instead
*/
setGlobalVariable(name: string, variable: any) {
deprecationWarning('template_srv.ts', 'setGlobalVariable', '');
this.index = {
...this.index,
[name]: {
current: variable,
},
};
}
getVariableName(expression: string) {
this.regex.lastIndex = 0;
const match = this.regex.exec(expression);
if (!match) {
return null;
}
const variableName = match.slice(1).find((match) => match !== undefined);
return variableName;
}
containsTemplate(target: string | undefined): boolean {
if (!target) {
return false;
}
const name = this.getVariableName(target);
const variable = name && this.getVariableAtIndex(name);
return variable !== null && variable !== undefined;
}
variableExists(expression: string): boolean {
deprecationWarning('template_srv.ts', 'variableExists', 'containsTemplate');
return this.containsTemplate(expression);
}
highlightVariablesAsHtml(str: string) {
if (!str || !isString(str)) {
return str;
}
str = escape(str);
return this._replaceWithVariableRegex(str, undefined, (match, variableName) => {
if (this.getVariableAtIndex(variableName)) {
return '<span class="template-variable">' + match + '</span>';
}
return match;
});
}
getAllValue(variable: any) {
if (variable.allValue) {
return variable.allValue;
}
const values = [];
for (let i = 1; i < variable.options.length; i++) {
values.push(variable.options[i].value);
}
return values;
}
private getFieldAccessor(fieldPath: string) {
const accessor = this.fieldAccessorCache[fieldPath];
if (accessor) {
return accessor;
}
return (this.fieldAccessorCache[fieldPath] = property(fieldPath));
}
private getVariableValue(scopedVar: ScopedVar, fieldPath: string | undefined) {
if (fieldPath) {
return this.getFieldAccessor(fieldPath)(scopedVar.value);
}
return scopedVar.value;
}
private getVariableText(scopedVar: ScopedVar, value: any) {
if (scopedVar.value === value || typeof value !== 'string') {
return scopedVar.text;
}
return value;
}
replace(
target?: string,
scopedVars?: ScopedVars,
format?: string | Function | undefined,
interpolations?: VariableInterpolation[]
): string {
if (scopedVars && scopedVars.__sceneObject) {
return sceneGraph.interpolate(
scopedVars.__sceneObject.value,
target,
scopedVars,
format as string | VariableCustomFormatterFn | undefined
);
}
if (!target) {
return target ?? '';
}
this.regex.lastIndex = 0;
return this._replaceWithVariableRegex(target, format, (match, variableName, fieldPath, fmt) => {
const value = this._evaluateVariableExpression(match, variableName, fieldPath, fmt, scopedVars);
// If we get passed this interpolations map we will also record all the expressions that were replaced
if (interpolations) {
interpolations.push({ match, variableName, fieldPath, format: fmt, value, found: value !== match });
}
return value;
});
}
private _evaluateVariableExpression(
match: string,
variableName: string,
fieldPath: string,
format: string | VariableCustomFormatterFn | undefined,
scopedVars: ScopedVars | undefined
) {
const variable = this.getVariableAtIndex(variableName);
const scopedVar = scopedVars?.[variableName];
if (scopedVar) {
const value = this.getVariableValue(scopedVar, fieldPath);
const text = this.getVariableText(scopedVar, value);
if (value !== null && value !== undefined) {
return formatVariableValue(value, format, variable, text);
}
}
if (!variable) {
if (macroRegistry[variableName]) {
return macroRegistry[variableName](match, fieldPath, scopedVars, format);
}
return match;
}
if (format === VariableFormatID.QueryParam || isAdHoc(variable)) {
const value = variableAdapters.get(variable.type).getValueForUrl(variable);
const text = isAdHoc(variable) ? variable.id : variable.current.text;
return formatVariableValue(value, format, variable, text);
}
const systemValue = this.grafanaVariables.get(variable.current.value);
if (systemValue) {
return formatVariableValue(systemValue, format, variable);
}
let value = variable.current.value;
let text = variable.current.text;
if (this.isAllValue(value)) {
value = this.getAllValue(variable);
text = ALL_VARIABLE_TEXT;
// skip formatting of custom all values unless format set to text or percentencode
if (variable.allValue && format !== VariableFormatID.Text && format !== VariableFormatID.PercentEncode) {
return this.replace(value);
}
}
if (fieldPath) {
const fieldValue = this.getVariableValue({ value, text }, fieldPath);
if (fieldValue !== null && fieldValue !== undefined) {
return formatVariableValue(fieldValue, format, variable, text);
}
}
return formatVariableValue(value, format, variable, text);
}
/**
* Tries to unify the different variable format capture groups into a simpler replacer function
*/
private _replaceWithVariableRegex(text: string, format: string | Function | undefined, replace: ReplaceFunction) {
this.regex.lastIndex = 0;
return (
text?.replace(this.regex, (match, var1, var2, fmt2, var3, fieldPath, fmt3) => {
const variableName = var1 || var2 || var3;
const fmt = fmt2 || fmt3 || format;
return replace(match, variableName, fieldPath, fmt);
}) || ''
);
}
isAllValue(value: any) {
return value === ALL_VARIABLE_VALUE || (Array.isArray(value) && value[0] === ALL_VARIABLE_VALUE);
}
replaceWithText(target: string, scopedVars?: ScopedVars) {
deprecationWarning('template_srv.ts', 'replaceWithText()', 'replace(), and specify the :text format');
return this.replace(target, scopedVars, 'text');
}
private getVariableAtIndex(name: string) {
if (!name) {
return;
}
if (!this.index[name]) {
return this.dependencies.getVariableWithName(name);
}
return this.index[name];
}
private getAdHocVariables(): AdHocVariableModel[] {
return this.dependencies.getFilteredVariables(isAdHoc) as AdHocVariableModel[];
}
}
// Expose the template srv
const srv = new TemplateSrv();
setTemplateSrv(srv);
export const getTemplateSrv = () => srv;