templateSrv.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. default: {
  82. if (typeof value === 'string') {
  83. return value;
  84. }
  85. return '{' + value.join(',') + '}';
  86. }
  87. }
  88. };
  89. this.setGrafanaVariable = function (name, value) {
  90. this._grafanaVariables[name] = value;
  91. };
  92. this.variableExists = function(expression) {
  93. this._regex.lastIndex = 0;
  94. var match = this._regex.exec(expression);
  95. return match && (self._index[match[1] || match[2]] !== void 0);
  96. };
  97. this.highlightVariablesAsHtml = function(str) {
  98. if (!str || !_.isString(str)) { return str; }
  99. str = _.escape(str);
  100. this._regex.lastIndex = 0;
  101. return str.replace(this._regex, function(match, g1, g2) {
  102. if (self._index[g1 || g2]) {
  103. return '<span class="template-variable">' + match + '</span>';
  104. }
  105. return match;
  106. });
  107. };
  108. this.getAllValue = function(variable) {
  109. if (variable.allValue) {
  110. return variable.allValue;
  111. }
  112. var values = [];
  113. for (var i = 1; i < variable.options.length; i++) {
  114. values.push(variable.options[i].value);
  115. }
  116. return values;
  117. };
  118. this.replace = function(target, scopedVars, format) {
  119. if (!target) { return target; }
  120. var variable, systemValue, value;
  121. this._regex.lastIndex = 0;
  122. return target.replace(this._regex, function(match, g1, g2) {
  123. variable = self._index[g1 || g2];
  124. if (scopedVars) {
  125. value = scopedVars[g1 || g2];
  126. if (value) {
  127. return self.formatValue(value.value, format, variable);
  128. }
  129. }
  130. if (!variable) {
  131. return match;
  132. }
  133. systemValue = self._grafanaVariables[variable.current.value];
  134. if (systemValue) {
  135. return self.formatValue(systemValue, format, variable);
  136. }
  137. value = variable.current.value;
  138. if (self.isAllValue(value)) {
  139. value = self.getAllValue(variable);
  140. // skip formating of custom all values
  141. if (variable.allValue) {
  142. return value;
  143. }
  144. }
  145. var res = self.formatValue(value, format, variable);
  146. return res;
  147. });
  148. };
  149. this.isAllValue = function(value) {
  150. return value === '$__all' || Array.isArray(value) && value[0] === '$__all';
  151. };
  152. this.replaceWithText = function(target, scopedVars) {
  153. if (!target) { return target; }
  154. var variable;
  155. this._regex.lastIndex = 0;
  156. return target.replace(this._regex, function(match, g1, g2) {
  157. if (scopedVars) {
  158. var option = scopedVars[g1 || g2];
  159. if (option) { return option.text; }
  160. }
  161. variable = self._index[g1 || g2];
  162. if (!variable) { return match; }
  163. return self._grafanaVariables[variable.current.value] || variable.current.text;
  164. });
  165. };
  166. this.fillVariableValuesForUrl = function(params, scopedVars) {
  167. _.each(this.variables, function(variable) {
  168. if (scopedVars && scopedVars[variable.name] !== void 0) {
  169. params['var-' + variable.name] = scopedVars[variable.name].value;
  170. } else {
  171. params['var-' + variable.name] = variable.getValueForUrl();
  172. }
  173. });
  174. };
  175. });
  176. });