templateSrv.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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.formatValue = function(value, format, variable) {
  37. // for some scopedVars there is no variable
  38. variable = variable || {};
  39. if (typeof format === 'function') {
  40. return format(value, variable, this.formatValue);
  41. }
  42. switch(format) {
  43. case "regex": {
  44. if (typeof value === 'string') {
  45. return regexEscape(value);
  46. }
  47. var escapedValues = _.map(value, regexEscape);
  48. return '(' + escapedValues.join('|') + ')';
  49. }
  50. case "lucene": {
  51. if (typeof value === 'string') {
  52. return luceneEscape(value);
  53. }
  54. var quotedValues = _.map(value, function(val) {
  55. return '\"' + luceneEscape(val) + '\"';
  56. });
  57. return '(' + quotedValues.join(' OR ') + ')';
  58. }
  59. case "pipe": {
  60. if (typeof value === 'string') {
  61. return value;
  62. }
  63. return value.join('|');
  64. }
  65. default: {
  66. if (typeof value === 'string') {
  67. return value;
  68. }
  69. return '{' + value.join(',') + '}';
  70. }
  71. }
  72. };
  73. this.setGrafanaVariable = function (name, value) {
  74. this._grafanaVariables[name] = value;
  75. };
  76. this.variableExists = function(expression) {
  77. this._regex.lastIndex = 0;
  78. var match = this._regex.exec(expression);
  79. return match && (self._index[match[1] || match[2]] !== void 0);
  80. };
  81. this.containsVariable = function(str, variableName) {
  82. if (!str) {
  83. return false;
  84. }
  85. var match = this._regex.exec(str);
  86. return match && (match[1] === variableName || match[2] === variableName);
  87. };
  88. this.highlightVariablesAsHtml = function(str) {
  89. if (!str || !_.isString(str)) { return str; }
  90. str = _.escape(str);
  91. this._regex.lastIndex = 0;
  92. return str.replace(this._regex, function(match, g1, g2) {
  93. if (self._index[g1 || g2]) {
  94. return '<span class="template-variable">' + match + '</span>';
  95. }
  96. return match;
  97. });
  98. };
  99. this.getAllValue = function(variable) {
  100. if (variable.allValue) {
  101. return variable.allValue;
  102. }
  103. var values = [];
  104. for (var i = 1; i < variable.options.length; i++) {
  105. values.push(variable.options[i].value);
  106. }
  107. return values;
  108. };
  109. this.replace = function(target, scopedVars, format) {
  110. if (!target) { return target; }
  111. var variable, systemValue, value;
  112. this._regex.lastIndex = 0;
  113. return target.replace(this._regex, function(match, g1, g2) {
  114. variable = self._index[g1 || g2];
  115. if (scopedVars) {
  116. value = scopedVars[g1 || g2];
  117. if (value) {
  118. return self.formatValue(value.value, format, variable);
  119. }
  120. }
  121. if (!variable) {
  122. return match;
  123. }
  124. systemValue = self._grafanaVariables[variable.current.value];
  125. if (systemValue) {
  126. return self.formatValue(systemValue, format, variable);
  127. }
  128. value = variable.current.value;
  129. if (self.isAllValue(value)) {
  130. value = self.getAllValue(variable);
  131. // skip formating of custom all values
  132. if (variable.allValue) {
  133. return value;
  134. }
  135. }
  136. var res = self.formatValue(value, format, variable);
  137. return res;
  138. });
  139. };
  140. this.isAllValue = function(value) {
  141. return value === '$__all' || Array.isArray(value) && value[0] === '$__all';
  142. };
  143. this.replaceWithText = function(target, scopedVars) {
  144. if (!target) { return target; }
  145. var variable;
  146. this._regex.lastIndex = 0;
  147. return target.replace(this._regex, function(match, g1, g2) {
  148. if (scopedVars) {
  149. var option = scopedVars[g1 || g2];
  150. if (option) { return option.text; }
  151. }
  152. variable = self._index[g1 || g2];
  153. if (!variable) { return match; }
  154. return self._grafanaVariables[variable.current.value] || variable.current.text;
  155. });
  156. };
  157. this.fillVariableValuesForUrl = function(params, scopedVars) {
  158. _.each(this.variables, function(variable) {
  159. var current = variable.current;
  160. var value = current.value;
  161. if (current.text === 'All') {
  162. value = 'All';
  163. }
  164. if (scopedVars && scopedVars[variable.name] !== void 0) {
  165. value = scopedVars[variable.name].value;
  166. }
  167. params['var-' + variable.name] = value;
  168. });
  169. };
  170. });
  171. });