templateSrv.js 5.6 KB

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