templateSrv.js 5.2 KB

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