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