2017-12-20 05:33:33 -06:00
|
|
|
import kbn from 'app/core/utils/kbn';
|
|
|
|
import _ from 'lodash';
|
2017-10-22 00:03:26 -05:00
|
|
|
|
|
|
|
function luceneEscape(value) {
|
2017-12-21 01:39:31 -06:00
|
|
|
return value.replace(/([\!\*\+\-\=<>\s\&\|\(\)\[\]\{\}\^\~\?\:\\/"])/g, '\\$1');
|
2017-10-22 00:03:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export class TemplateSrv {
|
|
|
|
variables: any[];
|
|
|
|
|
2018-02-23 08:51:19 -06:00
|
|
|
/*
|
|
|
|
* This regex matches 3 types of variable reference with an optional format specifier
|
|
|
|
* \$(\w+) $var1
|
|
|
|
* \[\[([\s\S]+?)(?::(\w+))?\]\] [[var2]] or [[var2:fmt2]]
|
|
|
|
* \${(\w+)(?::(\w+))?} ${var3} or ${var3:fmt3}
|
|
|
|
*/
|
2018-02-22 17:39:54 -06:00
|
|
|
private regex = /\$(\w+)|\[\[([\s\S]+?)(?::(\w+))?\]\]|\${(\w+)(?::(\w+))?}/g;
|
2017-10-22 00:03:26 -05:00
|
|
|
private index = {};
|
|
|
|
private grafanaVariables = {};
|
|
|
|
private builtIns = {};
|
|
|
|
|
|
|
|
constructor() {
|
2017-12-20 05:33:33 -06:00
|
|
|
this.builtIns['__interval'] = { text: '1s', value: '1s' };
|
|
|
|
this.builtIns['__interval_ms'] = { text: '100', value: '100' };
|
2017-10-22 00:03:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
init(variables) {
|
|
|
|
this.variables = variables;
|
|
|
|
this.updateTemplateData();
|
|
|
|
}
|
|
|
|
|
|
|
|
updateTemplateData() {
|
|
|
|
this.index = {};
|
|
|
|
|
|
|
|
for (var i = 0; i < this.variables.length; i++) {
|
2018-08-26 14:52:57 -05:00
|
|
|
const variable = this.variables[i];
|
2017-10-22 00:03:26 -05:00
|
|
|
|
2017-12-21 01:39:31 -06:00
|
|
|
if (!variable.current || (!variable.current.isNone && !variable.current.value)) {
|
2017-10-22 00:03:26 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.index[variable.name] = variable;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
variableInitialized(variable) {
|
|
|
|
this.index[variable.name] = variable;
|
|
|
|
}
|
|
|
|
|
|
|
|
getAdhocFilters(datasourceName) {
|
|
|
|
var filters = [];
|
|
|
|
|
|
|
|
for (var i = 0; i < this.variables.length; i++) {
|
2018-08-26 14:52:57 -05:00
|
|
|
const variable = this.variables[i];
|
2017-12-20 05:33:33 -06:00
|
|
|
if (variable.type !== 'adhoc') {
|
2017-10-22 00:03:26 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (variable.datasource === datasourceName) {
|
|
|
|
filters = filters.concat(variable.filters);
|
|
|
|
}
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
if (variable.datasource.indexOf('$') === 0) {
|
2017-10-22 00:03:26 -05:00
|
|
|
if (this.replace(variable.datasource) === datasourceName) {
|
|
|
|
filters = filters.concat(variable.filters);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return filters;
|
|
|
|
}
|
|
|
|
|
|
|
|
luceneFormat(value) {
|
2017-12-20 05:33:33 -06:00
|
|
|
if (typeof value === 'string') {
|
2017-10-22 00:03:26 -05:00
|
|
|
return luceneEscape(value);
|
|
|
|
}
|
2018-05-08 07:04:20 -05:00
|
|
|
if (value instanceof Array && value.length === 0) {
|
2018-05-18 04:10:10 -05:00
|
|
|
return '__empty__';
|
2018-05-08 07:04:20 -05:00
|
|
|
}
|
2018-08-26 14:52:57 -05:00
|
|
|
const quotedValues = _.map(value, function(val) {
|
2017-12-19 09:06:54 -06:00
|
|
|
return '"' + luceneEscape(val) + '"';
|
2017-10-22 00:03:26 -05:00
|
|
|
});
|
2017-12-20 05:33:33 -06:00
|
|
|
return '(' + quotedValues.join(' OR ') + ')';
|
2017-10-22 00:03:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
formatValue(value, format, variable) {
|
|
|
|
// for some scopedVars there is no variable
|
|
|
|
variable = variable || {};
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
if (typeof format === 'function') {
|
2017-10-22 00:03:26 -05:00
|
|
|
return format(value, variable, this.formatValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (format) {
|
2017-12-20 05:33:33 -06:00
|
|
|
case 'regex': {
|
|
|
|
if (typeof value === 'string') {
|
2017-10-22 00:03:26 -05:00
|
|
|
return kbn.regexEscape(value);
|
|
|
|
}
|
|
|
|
|
2018-08-26 14:52:57 -05:00
|
|
|
const escapedValues = _.map(value, kbn.regexEscape);
|
2018-02-15 11:44:03 -06:00
|
|
|
if (escapedValues.length === 1) {
|
|
|
|
return escapedValues[0];
|
|
|
|
}
|
2017-12-20 05:33:33 -06:00
|
|
|
return '(' + escapedValues.join('|') + ')';
|
2017-10-22 00:03:26 -05:00
|
|
|
}
|
2017-12-20 05:33:33 -06:00
|
|
|
case 'lucene': {
|
2017-10-22 00:03:26 -05:00
|
|
|
return this.luceneFormat(value);
|
|
|
|
}
|
2017-12-20 05:33:33 -06:00
|
|
|
case 'pipe': {
|
|
|
|
if (typeof value === 'string') {
|
2017-10-22 00:03:26 -05:00
|
|
|
return value;
|
|
|
|
}
|
2017-12-20 05:33:33 -06:00
|
|
|
return value.join('|');
|
2017-10-22 00:03:26 -05:00
|
|
|
}
|
2017-12-20 05:33:33 -06:00
|
|
|
case 'distributed': {
|
|
|
|
if (typeof value === 'string') {
|
2017-10-22 00:03:26 -05:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
return this.distributeVariable(value, variable.name);
|
|
|
|
}
|
2018-03-07 06:35:54 -06:00
|
|
|
case 'csv': {
|
|
|
|
if (_.isArray(value)) {
|
|
|
|
return value.join(',');
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
2017-12-19 09:06:54 -06:00
|
|
|
default: {
|
2017-10-22 00:03:26 -05:00
|
|
|
if (_.isArray(value)) {
|
2017-12-20 05:33:33 -06:00
|
|
|
return '{' + value.join(',') + '}';
|
2017-10-22 00:03:26 -05:00
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setGrafanaVariable(name, value) {
|
|
|
|
this.grafanaVariables[name] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
getVariableName(expression) {
|
|
|
|
this.regex.lastIndex = 0;
|
2018-08-26 14:52:57 -05:00
|
|
|
const match = this.regex.exec(expression);
|
2017-10-22 00:03:26 -05:00
|
|
|
if (!match) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return match[1] || match[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
variableExists(expression) {
|
2018-08-26 14:52:57 -05:00
|
|
|
const name = this.getVariableName(expression);
|
2017-12-19 09:06:54 -06:00
|
|
|
return name && this.index[name] !== void 0;
|
2017-10-22 00:03:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
highlightVariablesAsHtml(str) {
|
2017-12-19 09:06:54 -06:00
|
|
|
if (!str || !_.isString(str)) {
|
|
|
|
return str;
|
|
|
|
}
|
2017-10-22 00:03:26 -05:00
|
|
|
|
|
|
|
str = _.escape(str);
|
|
|
|
this.regex.lastIndex = 0;
|
2018-02-23 08:51:19 -06:00
|
|
|
return str.replace(this.regex, (match, var1, var2, fmt2, var3) => {
|
|
|
|
if (this.index[var1 || var2 || var3] || this.builtIns[var1 || var2 || var3]) {
|
2017-12-20 05:33:33 -06:00
|
|
|
return '<span class="template-variable">' + match + '</span>';
|
2017-10-22 00:03:26 -05:00
|
|
|
}
|
|
|
|
return match;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getAllValue(variable) {
|
|
|
|
if (variable.allValue) {
|
|
|
|
return variable.allValue;
|
|
|
|
}
|
2018-08-26 14:52:57 -05:00
|
|
|
const values = [];
|
2017-10-22 00:03:26 -05:00
|
|
|
for (var i = 1; i < variable.options.length; i++) {
|
|
|
|
values.push(variable.options[i].value);
|
|
|
|
}
|
|
|
|
return values;
|
|
|
|
}
|
|
|
|
|
|
|
|
replace(target, scopedVars?, format?) {
|
2017-12-19 09:06:54 -06:00
|
|
|
if (!target) {
|
|
|
|
return target;
|
|
|
|
}
|
2017-10-22 00:03:26 -05:00
|
|
|
|
2018-05-02 16:03:37 -05:00
|
|
|
var variable, systemValue, value, fmt;
|
2017-10-22 00:03:26 -05:00
|
|
|
this.regex.lastIndex = 0;
|
|
|
|
|
2018-02-23 08:51:19 -06:00
|
|
|
return target.replace(this.regex, (match, var1, var2, fmt2, var3, fmt3) => {
|
|
|
|
variable = this.index[var1 || var2 || var3];
|
2018-05-02 16:03:37 -05:00
|
|
|
fmt = fmt2 || fmt3 || format;
|
2017-10-22 00:03:26 -05:00
|
|
|
if (scopedVars) {
|
2018-02-23 08:51:19 -06:00
|
|
|
value = scopedVars[var1 || var2 || var3];
|
2017-10-22 00:03:26 -05:00
|
|
|
if (value) {
|
2018-05-02 16:03:37 -05:00
|
|
|
return this.formatValue(value.value, fmt, variable);
|
2017-10-22 00:03:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!variable) {
|
|
|
|
return match;
|
|
|
|
}
|
|
|
|
|
|
|
|
systemValue = this.grafanaVariables[variable.current.value];
|
|
|
|
if (systemValue) {
|
2018-05-02 16:03:37 -05:00
|
|
|
return this.formatValue(systemValue, fmt, variable);
|
2017-10-22 00:03:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
value = variable.current.value;
|
|
|
|
if (this.isAllValue(value)) {
|
|
|
|
value = this.getAllValue(variable);
|
2018-04-13 12:48:37 -05:00
|
|
|
// skip formatting of custom all values
|
2017-10-22 00:03:26 -05:00
|
|
|
if (variable.allValue) {
|
2018-08-17 09:04:32 -05:00
|
|
|
return this.replace(value);
|
2017-10-22 00:03:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-26 14:52:57 -05:00
|
|
|
const res = this.formatValue(value, fmt, variable);
|
2017-10-22 00:03:26 -05:00
|
|
|
return res;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
isAllValue(value) {
|
2017-12-21 01:39:31 -06:00
|
|
|
return value === '$__all' || (Array.isArray(value) && value[0] === '$__all');
|
2017-10-22 00:03:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
replaceWithText(target, scopedVars) {
|
2017-12-19 09:06:54 -06:00
|
|
|
if (!target) {
|
|
|
|
return target;
|
|
|
|
}
|
2017-10-22 00:03:26 -05:00
|
|
|
|
|
|
|
var variable;
|
|
|
|
this.regex.lastIndex = 0;
|
|
|
|
|
2018-02-23 08:51:19 -06:00
|
|
|
return target.replace(this.regex, (match, var1, var2, fmt2, var3) => {
|
2017-10-22 00:03:26 -05:00
|
|
|
if (scopedVars) {
|
2018-08-26 14:52:57 -05:00
|
|
|
const option = scopedVars[var1 || var2 || var3];
|
2017-12-19 09:06:54 -06:00
|
|
|
if (option) {
|
|
|
|
return option.text;
|
|
|
|
}
|
2017-10-22 00:03:26 -05:00
|
|
|
}
|
|
|
|
|
2018-02-23 08:51:19 -06:00
|
|
|
variable = this.index[var1 || var2 || var3];
|
2017-12-19 09:06:54 -06:00
|
|
|
if (!variable) {
|
|
|
|
return match;
|
|
|
|
}
|
2017-10-22 00:03:26 -05:00
|
|
|
|
2017-12-21 01:39:31 -06:00
|
|
|
return this.grafanaVariables[variable.current.value] || variable.current.text;
|
2017-10-22 00:03:26 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fillVariableValuesForUrl(params, scopedVars) {
|
|
|
|
_.each(this.variables, function(variable) {
|
|
|
|
if (scopedVars && scopedVars[variable.name] !== void 0) {
|
2018-07-11 12:06:36 -05:00
|
|
|
if (scopedVars[variable.name].skipUrlSync) {
|
|
|
|
return;
|
|
|
|
}
|
2017-12-20 05:33:33 -06:00
|
|
|
params['var-' + variable.name] = scopedVars[variable.name].value;
|
2017-10-22 00:03:26 -05:00
|
|
|
} else {
|
2018-07-11 12:06:36 -05:00
|
|
|
if (variable.skipUrlSync) {
|
|
|
|
return;
|
|
|
|
}
|
2017-12-20 05:33:33 -06:00
|
|
|
params['var-' + variable.name] = variable.getValueForUrl();
|
2017-10-22 00:03:26 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
distributeVariable(value, variable) {
|
|
|
|
value = _.map(value, function(val, index) {
|
|
|
|
if (index !== 0) {
|
2017-12-20 05:33:33 -06:00
|
|
|
return variable + '=' + val;
|
2017-10-22 00:03:26 -05:00
|
|
|
} else {
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
});
|
2017-12-20 05:33:33 -06:00
|
|
|
return value.join(',');
|
2017-10-22 00:03:26 -05:00
|
|
|
}
|
|
|
|
}
|
2017-12-14 05:31:57 -06:00
|
|
|
|
|
|
|
export default new TemplateSrv();
|