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 = {};
|
|
|
|
|
this._texts = {};
|
2014-09-05 12:07:48 +02:00
|
|
|
|
2014-08-28 12:44:01 +02:00
|
|
|
_.each(this.variables, function(variable) {
|
2015-06-26 14:05:37 -04:00
|
|
|
if (!variable.current || !variable.current.isNone && !variable.current.value) { return; }
|
2014-09-05 12:07:48 +02:00
|
|
|
|
2015-03-19 23:09:50 -04:00
|
|
|
this._values[variable.name] = this.renderVariableValue(variable);
|
2014-09-11 13:54:59 +02:00
|
|
|
this._texts[variable.name] = variable.current.text;
|
|
|
|
|
}, this);
|
2014-08-27 17:58:49 +02:00
|
|
|
};
|
|
|
|
|
|
2015-03-19 23:09:50 -04:00
|
|
|
this.renderVariableValue = function(variable) {
|
|
|
|
|
var value = variable.current.value;
|
|
|
|
|
if (_.isString(value)) {
|
|
|
|
|
return value;
|
|
|
|
|
} else {
|
2015-09-22 14:29:41 +02:00
|
|
|
switch(variable.multiFormat) {
|
|
|
|
|
case "regex values": {
|
|
|
|
|
return '(' + value.join('|') + ')';
|
|
|
|
|
}
|
|
|
|
|
case "lucene": {
|
2015-09-28 11:28:08 +02:00
|
|
|
var quotedValues = _.map(value, function(val) {
|
|
|
|
|
return '\\\"' + val + '\\\"';
|
|
|
|
|
});
|
|
|
|
|
return '(' + quotedValues.join(' OR ') + ')';
|
2015-09-22 14:29:41 +02:00
|
|
|
}
|
2015-09-23 11:22:57 +05:30
|
|
|
case "pipe": {
|
|
|
|
|
return value.join('|');
|
|
|
|
|
}
|
2015-09-22 14:29:41 +02:00
|
|
|
default: {
|
|
|
|
|
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
|
|
|
|
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
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2015-03-17 12:30:42 -04:00
|
|
|
this.replace = function(target, scopedVars) {
|
2015-05-08 10:56:54 +02:00
|
|
|
if (!target) { return target; }
|
2014-08-27 15:54:30 +02:00
|
|
|
|
2014-09-05 12:07:48 +02:00
|
|
|
var value;
|
|
|
|
|
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];
|
2015-03-17 13:33:58 -04:00
|
|
|
if (value) { return value.value; }
|
2015-03-17 12:30:42 -04:00
|
|
|
}
|
|
|
|
|
|
2014-09-11 13:54:59 +02:00
|
|
|
value = self._values[g1 || g2];
|
2014-09-05 12:07:48 +02:00
|
|
|
if (!value) { return match; }
|
|
|
|
|
|
|
|
|
|
return self._grafanaVariables[value] || value;
|
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
|
|
|
});
|
|
|
|
|
|
|
|
|
|
});
|