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