templateSrv.js 6.3 KB

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