templateSrv.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. define([
  2. 'angular',
  3. 'lodash',
  4. 'app/core/utils/kbn',
  5. ],
  6. function (angular, _, kbn) {
  7. 'use strict';
  8. var module = angular.module('grafana.services');
  9. module.service('templateSrv', function() {
  10. var self = this;
  11. this._regex = /\$(\w+)|\[\[([\s\S]+?)\]\]/g;
  12. this._index = {};
  13. this._texts = {};
  14. this._grafanaVariables = {};
  15. this._adhocVariables = {};
  16. // default built ins
  17. this._builtIns = {};
  18. this._builtIns['__interval'] = {text: '1s', value: '1s'};
  19. this._builtIns['__interval_ms'] = {text: '100', value: '100'};
  20. this.init = function(variables) {
  21. this.variables = variables;
  22. this.updateTemplateData();
  23. };
  24. this.updateTemplateData = function() {
  25. this._index = {};
  26. this._filters = {};
  27. this._adhocVariables = {};
  28. for (var i = 0; i < this.variables.length; i++) {
  29. var variable = this.variables[i];
  30. // add adhoc filters to it's own index
  31. if (variable.type === 'adhoc') {
  32. this._adhocVariables[variable.datasource] = variable;
  33. continue;
  34. }
  35. if (!variable.current || !variable.current.isNone && !variable.current.value) {
  36. continue;
  37. }
  38. this._index[variable.name] = variable;
  39. }
  40. };
  41. this.variableInitialized = function(variable) {
  42. this._index[variable.name] = variable;
  43. };
  44. this.getAdhocFilters = function(datasourceName) {
  45. var variable = this._adhocVariables[datasourceName];
  46. if (variable) {
  47. return variable.filters || [];
  48. }
  49. return [];
  50. };
  51. function luceneEscape(value) {
  52. return value.replace(/([\!\*\+\-\=<>\s\&\|\(\)\[\]\{\}\^\~\?\:\\/"])/g, "\\$1");
  53. }
  54. this.luceneFormat = function(value) {
  55. if (typeof value === 'string') {
  56. return luceneEscape(value);
  57. }
  58. var quotedValues = _.map(value, function(val) {
  59. return '\"' + luceneEscape(val) + '\"';
  60. });
  61. return '(' + quotedValues.join(' OR ') + ')';
  62. };
  63. this.formatValue = function(value, format, variable) {
  64. // for some scopedVars there is no variable
  65. variable = variable || {};
  66. if (typeof format === 'function') {
  67. return format(value, variable, this.formatValue);
  68. }
  69. switch(format) {
  70. case "regex": {
  71. if (typeof value === 'string') {
  72. return kbn.regexEscape(value);
  73. }
  74. var escapedValues = _.map(value, kbn.regexEscape);
  75. return '(' + escapedValues.join('|') + ')';
  76. }
  77. case "lucene": {
  78. return this.luceneFormat(value, format, variable);
  79. }
  80. case "pipe": {
  81. if (typeof value === 'string') {
  82. return value;
  83. }
  84. return value.join('|');
  85. }
  86. case "distributed": {
  87. if (typeof value === 'string') {
  88. return value;
  89. }
  90. return this.distributeVariable(value, variable.name);
  91. }
  92. default: {
  93. if (_.isArray(value)) {
  94. return '{' + value.join(',') + '}';
  95. }
  96. return value;
  97. }
  98. }
  99. };
  100. this.setGrafanaVariable = function (name, value) {
  101. this._grafanaVariables[name] = value;
  102. };
  103. this.getVariableName = function(expression) {
  104. this._regex.lastIndex = 0;
  105. var match = this._regex.exec(expression);
  106. if (!match) {
  107. return null;
  108. }
  109. return match[1] || match[2];
  110. };
  111. this.variableExists = function(expression) {
  112. var name = this.getVariableName(expression);
  113. return name && (self._index[name] !== void 0);
  114. };
  115. this.highlightVariablesAsHtml = function(str) {
  116. if (!str || !_.isString(str)) { return str; }
  117. str = _.escape(str);
  118. this._regex.lastIndex = 0;
  119. return str.replace(this._regex, function(match, g1, g2) {
  120. if (self._index[g1 || g2] || self._builtIns[g1 || g2]) {
  121. return '<span class="template-variable">' + match + '</span>';
  122. }
  123. return match;
  124. });
  125. };
  126. this.getAllValue = function(variable) {
  127. if (variable.allValue) {
  128. return variable.allValue;
  129. }
  130. var values = [];
  131. for (var i = 1; i < variable.options.length; i++) {
  132. values.push(variable.options[i].value);
  133. }
  134. return values;
  135. };
  136. this.replace = function(target, scopedVars, format) {
  137. if (!target) { return target; }
  138. var variable, systemValue, value;
  139. this._regex.lastIndex = 0;
  140. return target.replace(this._regex, function(match, g1, g2) {
  141. variable = self._index[g1 || g2];
  142. if (scopedVars) {
  143. value = scopedVars[g1 || g2];
  144. if (value) {
  145. return self.formatValue(value.value, format, variable);
  146. }
  147. }
  148. if (!variable) {
  149. return match;
  150. }
  151. systemValue = self._grafanaVariables[variable.current.value];
  152. if (systemValue) {
  153. return self.formatValue(systemValue, format, variable);
  154. }
  155. value = variable.current.value;
  156. if (self.isAllValue(value)) {
  157. value = self.getAllValue(variable);
  158. // skip formating of custom all values
  159. if (variable.allValue) {
  160. return value;
  161. }
  162. }
  163. var res = self.formatValue(value, format, variable);
  164. return res;
  165. });
  166. };
  167. this.isAllValue = function(value) {
  168. return value === '$__all' || Array.isArray(value) && value[0] === '$__all';
  169. };
  170. this.replaceWithText = function(target, scopedVars) {
  171. if (!target) { return target; }
  172. var variable;
  173. this._regex.lastIndex = 0;
  174. return target.replace(this._regex, function(match, g1, g2) {
  175. if (scopedVars) {
  176. var option = scopedVars[g1 || g2];
  177. if (option) { return option.text; }
  178. }
  179. variable = self._index[g1 || g2];
  180. if (!variable) { return match; }
  181. return self._grafanaVariables[variable.current.value] || variable.current.text;
  182. });
  183. };
  184. this.fillVariableValuesForUrl = function(params, scopedVars) {
  185. _.each(this.variables, function(variable) {
  186. if (scopedVars && scopedVars[variable.name] !== void 0) {
  187. params['var-' + variable.name] = scopedVars[variable.name].value;
  188. } else {
  189. params['var-' + variable.name] = variable.getValueForUrl();
  190. }
  191. });
  192. };
  193. this.distributeVariable = function(value, variable) {
  194. value = _.map(value, function(val, index) {
  195. if (index !== 0) {
  196. return variable + "=" + val;
  197. } else {
  198. return val;
  199. }
  200. });
  201. return value.join(',');
  202. };
  203. });
  204. });