template_srv.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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)) {
  114. return str;
  115. }
  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) {
  137. return target;
  138. }
  139. var variable, systemValue, value;
  140. this.regex.lastIndex = 0;
  141. return target.replace(this.regex, (match, g1, g2) => {
  142. variable = this.index[g1 || g2];
  143. if (scopedVars) {
  144. value = scopedVars[g1 || g2];
  145. if (value) {
  146. return this.formatValue(value.value, format, variable);
  147. }
  148. }
  149. if (!variable) {
  150. return match;
  151. }
  152. systemValue = this.grafanaVariables[variable.current.value];
  153. if (systemValue) {
  154. return this.formatValue(systemValue, format, variable);
  155. }
  156. value = variable.current.value;
  157. if (this.isAllValue(value)) {
  158. value = this.getAllValue(variable);
  159. // skip formating of custom all values
  160. if (variable.allValue) {
  161. return value;
  162. }
  163. }
  164. var res = this.formatValue(value, format, variable);
  165. return res;
  166. });
  167. }
  168. isAllValue(value) {
  169. return value === '$__all' || (Array.isArray(value) && value[0] === '$__all');
  170. }
  171. replaceWithText(target, scopedVars) {
  172. if (!target) {
  173. return target;
  174. }
  175. var variable;
  176. this.regex.lastIndex = 0;
  177. return target.replace(this.regex, (match, g1, g2) => {
  178. if (scopedVars) {
  179. var option = scopedVars[g1 || g2];
  180. if (option) {
  181. return option.text;
  182. }
  183. }
  184. variable = this.index[g1 || g2];
  185. if (!variable) {
  186. return match;
  187. }
  188. return this.grafanaVariables[variable.current.value] || variable.current.text;
  189. });
  190. }
  191. fillVariableValuesForUrl(params, scopedVars) {
  192. _.each(this.variables, function(variable) {
  193. if (scopedVars && scopedVars[variable.name] !== void 0) {
  194. params['var-' + variable.name] = scopedVars[variable.name].value;
  195. } else {
  196. params['var-' + variable.name] = variable.getValueForUrl();
  197. }
  198. });
  199. }
  200. distributeVariable(value, variable) {
  201. value = _.map(value, function(val, index) {
  202. if (index !== 0) {
  203. return variable + '=' + val;
  204. } else {
  205. return val;
  206. }
  207. });
  208. return value.join(',');
  209. }
  210. }
  211. export default new TemplateSrv();