template_srv.ts 5.9 KB

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