templateSrv.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. return str.indexOf('$' + variableName) !== -1 || str.indexOf('[[' + variableName + ']]') !== -1;
  86. };
  87. this.highlightVariablesAsHtml = function(str) {
  88. if (!str || !_.isString(str)) { return str; }
  89. str = _.escape(str);
  90. this._regex.lastIndex = 0;
  91. return str.replace(this._regex, function(match, g1, g2) {
  92. if (self._index[g1 || g2]) {
  93. return '<span class="template-variable">' + match + '</span>';
  94. }
  95. return match;
  96. });
  97. };
  98. this.getAllValue = function(variable) {
  99. if (variable.allValue) {
  100. return variable.allValue;
  101. }
  102. var values = [];
  103. for (var i = 1; i < variable.options.length; i++) {
  104. values.push(variable.options[i].value);
  105. }
  106. return values;
  107. };
  108. this.replace = function(target, scopedVars, format) {
  109. if (!target) { return target; }
  110. var variable, systemValue, value;
  111. this._regex.lastIndex = 0;
  112. return target.replace(this._regex, function(match, g1, g2) {
  113. variable = self._index[g1 || g2];
  114. if (scopedVars) {
  115. value = scopedVars[g1 || g2];
  116. if (value) {
  117. return self.formatValue(value.value, format, variable);
  118. }
  119. }
  120. if (!variable) {
  121. return match;
  122. }
  123. systemValue = self._grafanaVariables[variable.current.value];
  124. if (systemValue) {
  125. return self.formatValue(systemValue, format, variable);
  126. }
  127. value = variable.current.value;
  128. if (self.isAllValue(value)) {
  129. value = self.getAllValue(variable);
  130. }
  131. var res = self.formatValue(value, format, variable);
  132. return res;
  133. });
  134. };
  135. this.isAllValue = function(value) {
  136. return value === '$__all' || Array.isArray(value) && value[0] === '$__all';
  137. };
  138. this.replaceWithText = function(target, scopedVars) {
  139. if (!target) { return target; }
  140. var variable;
  141. this._regex.lastIndex = 0;
  142. return target.replace(this._regex, function(match, g1, g2) {
  143. if (scopedVars) {
  144. var option = scopedVars[g1 || g2];
  145. if (option) { return option.text; }
  146. }
  147. variable = self._index[g1 || g2];
  148. if (!variable) { return match; }
  149. return self._grafanaVariables[variable.current.value] || variable.current.text;
  150. });
  151. };
  152. this.fillVariableValuesForUrl = function(params, scopedVars) {
  153. _.each(this.variables, function(variable) {
  154. var current = variable.current;
  155. var value = current.value;
  156. if (current.text === 'All') {
  157. value = 'All';
  158. }
  159. if (scopedVars && scopedVars[variable.name] !== void 0) {
  160. value = scopedVars[variable.name].value;
  161. }
  162. params['var-' + variable.name] = value;
  163. });
  164. };
  165. });
  166. });