templateSrv.js 6.1 KB

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