2014-08-27 15:54:30 +02:00
|
|
|
define([
|
|
|
|
|
'angular',
|
|
|
|
|
'lodash',
|
2014-12-31 09:34:54 +01:00
|
|
|
'./editorCtrl',
|
|
|
|
|
'./templateValuesSrv',
|
2014-08-27 15:54:30 +02:00
|
|
|
],
|
2014-08-28 16:44:16 +02:00
|
|
|
function (angular, _) {
|
2014-08-27 15:54:30 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
var module = angular.module('grafana.services');
|
|
|
|
|
|
2014-09-08 11:03:14 +02:00
|
|
|
module.service('templateSrv', function() {
|
2014-09-04 17:34:36 +02:00
|
|
|
var self = this;
|
2014-08-27 15:54:30 +02:00
|
|
|
|
2014-09-05 12:07:48 +02:00
|
|
|
this._regex = /\$(\w+)|\[\[([\s\S]+?)\]\]/g;
|
2014-09-11 13:54:59 +02:00
|
|
|
this._values = {};
|
|
|
|
|
this._texts = {};
|
2014-09-05 12:07:48 +02:00
|
|
|
this._grafanaVariables = {};
|
|
|
|
|
|
2014-08-28 12:44:01 +02:00
|
|
|
this.init = function(variables) {
|
|
|
|
|
this.variables = variables;
|
2014-09-11 13:54:59 +02:00
|
|
|
this.updateTemplateData();
|
2014-08-27 17:58:49 +02:00
|
|
|
};
|
|
|
|
|
|
2014-09-08 11:03:14 +02:00
|
|
|
this.updateTemplateData = function() {
|
2014-09-11 13:54:59 +02:00
|
|
|
this._values = {};
|
2014-09-05 12:07:48 +02:00
|
|
|
|
2014-08-28 12:44:01 +02:00
|
|
|
_.each(this.variables, function(variable) {
|
2016-02-28 11:44:11 +01:00
|
|
|
if (!variable.current || !variable.current.isNone && !variable.current.value) { return; }
|
|
|
|
|
this._values[variable.name] = variable.current.value;
|
|
|
|
|
}, this);
|
2014-08-27 17:58:49 +02:00
|
|
|
};
|
|
|
|
|
|
2016-03-01 08:43:54 +01:00
|
|
|
function regexEscape(value) {
|
2016-02-29 17:31:31 +01:00
|
|
|
return value.replace(/[-[\]{}()*+!<=:?.\/\\^$|#\s,]/g, '\\$&');
|
2016-03-01 08:43:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function luceneEscape(value) {
|
|
|
|
|
return value.replace(/([\!\*\+\-\=<>\s\&\|\(\)\[\]\{\}\^\~\?\:\\/"])/g, "\\$1");
|
|
|
|
|
}
|
2016-02-29 17:31:31 +01:00
|
|
|
|
2016-02-28 11:44:11 +01:00
|
|
|
this.formatValue = function(value, format) {
|
2016-03-01 08:43:54 +01:00
|
|
|
switch(format) {
|
|
|
|
|
case "regex": {
|
|
|
|
|
var escapedValues = _.map(value, regexEscape);
|
|
|
|
|
return '(' + escapedValues.join('|') + ')';
|
|
|
|
|
}
|
|
|
|
|
case "lucene": {
|
|
|
|
|
if (typeof value === 'string') {
|
|
|
|
|
return luceneEscape(value);
|
2015-09-23 11:22:57 +05:30
|
|
|
}
|
2016-03-01 08:43:54 +01:00
|
|
|
var quotedValues = _.map(value, function(val) {
|
|
|
|
|
return '\"' + luceneEscape(val) + '\"';
|
|
|
|
|
});
|
|
|
|
|
return '(' + quotedValues.join(' OR ') + ')';
|
|
|
|
|
}
|
|
|
|
|
case "pipe": {
|
|
|
|
|
return value.join('|');
|
|
|
|
|
}
|
|
|
|
|
default: {
|
|
|
|
|
if (typeof value === 'string') {
|
|
|
|
|
return value;
|
2015-09-22 14:29:41 +02:00
|
|
|
}
|
2016-03-01 08:43:54 +01:00
|
|
|
return '{' + value.join(',') + '}';
|
2015-03-19 23:09:50 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2014-09-05 12:07:48 +02:00
|
|
|
this.setGrafanaVariable = function (name, value) {
|
|
|
|
|
this._grafanaVariables[name] = value;
|
2014-09-02 07:05:24 +02:00
|
|
|
};
|
|
|
|
|
|
2014-09-05 08:26:50 +02:00
|
|
|
this.variableExists = function(expression) {
|
2014-09-05 12:07:48 +02:00
|
|
|
this._regex.lastIndex = 0;
|
|
|
|
|
var match = this._regex.exec(expression);
|
2014-09-11 13:54:59 +02:00
|
|
|
return match && (self._values[match[1] || match[2]] !== void 0);
|
2014-09-05 08:26:50 +02:00
|
|
|
};
|
|
|
|
|
|
2014-09-08 11:03:14 +02:00
|
|
|
this.containsVariable = function(str, variableName) {
|
2015-02-20 10:45:23 +01:00
|
|
|
if (!str) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2014-09-08 11:03:14 +02:00
|
|
|
return str.indexOf('$' + variableName) !== -1 || str.indexOf('[[' + variableName + ']]') !== -1;
|
|
|
|
|
};
|
|
|
|
|
|
2014-09-05 08:26:50 +02:00
|
|
|
this.highlightVariablesAsHtml = function(str) {
|
2014-09-05 09:11:50 +02:00
|
|
|
if (!str || !_.isString(str)) { return str; }
|
2014-09-05 08:26:50 +02:00
|
|
|
|
2015-12-10 13:27:57 +01:00
|
|
|
str = _.escape(str);
|
2014-09-05 12:07:48 +02:00
|
|
|
this._regex.lastIndex = 0;
|
|
|
|
|
return str.replace(this._regex, function(match, g1, g2) {
|
2014-09-11 13:54:59 +02:00
|
|
|
if (self._values[g1 || g2]) {
|
2014-09-05 08:26:50 +02:00
|
|
|
return '<span class="template-variable">' + match + '</span>';
|
|
|
|
|
}
|
2014-09-05 14:03:36 +02:00
|
|
|
return match;
|
2014-09-05 08:26:50 +02:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2016-02-28 11:44:11 +01:00
|
|
|
this.replace = function(target, scopedVars, format) {
|
2015-05-08 10:56:54 +02:00
|
|
|
if (!target) { return target; }
|
2014-08-27 15:54:30 +02:00
|
|
|
|
2016-02-28 11:44:11 +01:00
|
|
|
var value, systemValue;
|
2014-09-05 12:07:48 +02:00
|
|
|
this._regex.lastIndex = 0;
|
|
|
|
|
|
|
|
|
|
return target.replace(this._regex, function(match, g1, g2) {
|
2015-03-17 12:30:42 -04:00
|
|
|
if (scopedVars) {
|
|
|
|
|
value = scopedVars[g1 || g2];
|
2016-02-28 11:44:11 +01:00
|
|
|
if (value) {
|
|
|
|
|
return self.formatValue(value.value);
|
|
|
|
|
}
|
2015-03-17 12:30:42 -04:00
|
|
|
}
|
|
|
|
|
|
2014-09-11 13:54:59 +02:00
|
|
|
value = self._values[g1 || g2];
|
2016-02-28 11:44:11 +01:00
|
|
|
if (!value) {
|
|
|
|
|
return match;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
systemValue = self._grafanaVariables[value];
|
|
|
|
|
if (systemValue) {
|
|
|
|
|
return self.formatValue(systemValue);
|
|
|
|
|
}
|
2014-09-05 12:07:48 +02:00
|
|
|
|
2016-03-01 08:43:54 +01:00
|
|
|
var res = self.formatValue(value, format);
|
|
|
|
|
console.log('replace: ' + value, res);
|
|
|
|
|
return res;
|
2014-09-04 17:34:36 +02:00
|
|
|
});
|
2014-08-27 15:54:30 +02:00
|
|
|
};
|
|
|
|
|
|
2015-03-17 13:33:58 -04:00
|
|
|
this.replaceWithText = function(target, scopedVars) {
|
2015-05-08 10:56:54 +02:00
|
|
|
if (!target) { return target; }
|
2014-09-11 13:54:59 +02:00
|
|
|
|
|
|
|
|
var value;
|
|
|
|
|
var text;
|
|
|
|
|
this._regex.lastIndex = 0;
|
|
|
|
|
|
|
|
|
|
return target.replace(this._regex, function(match, g1, g2) {
|
2015-03-17 13:33:58 -04:00
|
|
|
if (scopedVars) {
|
|
|
|
|
var option = scopedVars[g1 || g2];
|
|
|
|
|
if (option) { return option.text; }
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-11 13:54:59 +02:00
|
|
|
value = self._values[g1 || g2];
|
|
|
|
|
text = self._texts[g1 || g2];
|
|
|
|
|
if (!value) { return match; }
|
|
|
|
|
|
|
|
|
|
return self._grafanaVariables[value] || text;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2015-07-30 08:28:42 +02:00
|
|
|
this.fillVariableValuesForUrl = function(params, scopedVars) {
|
|
|
|
|
_.each(this.variables, function(variable) {
|
|
|
|
|
var current = variable.current;
|
|
|
|
|
var value = current.value;
|
|
|
|
|
|
2015-07-07 14:28:49 +02:00
|
|
|
if (current.text === 'All') {
|
2015-07-30 08:28:42 +02:00
|
|
|
value = 'All';
|
2015-07-07 14:28:49 +02:00
|
|
|
}
|
|
|
|
|
|
2015-07-30 08:28:42 +02:00
|
|
|
if (scopedVars && scopedVars[variable.name] !== void 0) {
|
|
|
|
|
value = scopedVars[variable.name].value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
params['var-' + variable.name] = value;
|
2015-05-08 10:56:54 +02:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2014-08-27 15:54:30 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
});
|