templateSrv.js 5.7 KB

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