templateSrv.js 6.7 KB

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