template_srv.ts 6.6 KB

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