template_srv.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. const 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. const 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. if (value instanceof Array && value.length === 0) {
  62. return '__empty__';
  63. }
  64. const quotedValues = _.map(value, function(val) {
  65. return '"' + luceneEscape(val) + '"';
  66. });
  67. return '(' + quotedValues.join(' OR ') + ')';
  68. }
  69. formatValue(value, format, variable) {
  70. // for some scopedVars there is no variable
  71. variable = variable || {};
  72. if (typeof format === 'function') {
  73. return format(value, variable, this.formatValue);
  74. }
  75. switch (format) {
  76. case 'regex': {
  77. if (typeof value === 'string') {
  78. return kbn.regexEscape(value);
  79. }
  80. const escapedValues = _.map(value, kbn.regexEscape);
  81. if (escapedValues.length === 1) {
  82. return escapedValues[0];
  83. }
  84. return '(' + escapedValues.join('|') + ')';
  85. }
  86. case 'lucene': {
  87. return this.luceneFormat(value);
  88. }
  89. case 'pipe': {
  90. if (typeof value === 'string') {
  91. return value;
  92. }
  93. return value.join('|');
  94. }
  95. case 'distributed': {
  96. if (typeof value === 'string') {
  97. return value;
  98. }
  99. return this.distributeVariable(value, variable.name);
  100. }
  101. case 'csv': {
  102. if (_.isArray(value)) {
  103. return value.join(',');
  104. }
  105. return value;
  106. }
  107. default: {
  108. if (_.isArray(value)) {
  109. return '{' + value.join(',') + '}';
  110. }
  111. return value;
  112. }
  113. }
  114. }
  115. setGrafanaVariable(name, value) {
  116. this.grafanaVariables[name] = value;
  117. }
  118. getVariableName(expression) {
  119. this.regex.lastIndex = 0;
  120. const match = this.regex.exec(expression);
  121. if (!match) {
  122. return null;
  123. }
  124. return match[1] || match[2];
  125. }
  126. variableExists(expression) {
  127. const name = this.getVariableName(expression);
  128. return name && this.index[name] !== void 0;
  129. }
  130. highlightVariablesAsHtml(str) {
  131. if (!str || !_.isString(str)) {
  132. return str;
  133. }
  134. str = _.escape(str);
  135. this.regex.lastIndex = 0;
  136. return str.replace(this.regex, (match, var1, var2, fmt2, var3) => {
  137. if (this.index[var1 || var2 || var3] || this.builtIns[var1 || var2 || var3]) {
  138. return '<span class="template-variable">' + match + '</span>';
  139. }
  140. return match;
  141. });
  142. }
  143. getAllValue(variable) {
  144. if (variable.allValue) {
  145. return variable.allValue;
  146. }
  147. const values = [];
  148. for (var i = 1; i < variable.options.length; i++) {
  149. values.push(variable.options[i].value);
  150. }
  151. return values;
  152. }
  153. replace(target, scopedVars?, format?) {
  154. if (!target) {
  155. return target;
  156. }
  157. var variable, systemValue, value, fmt;
  158. this.regex.lastIndex = 0;
  159. return target.replace(this.regex, (match, var1, var2, fmt2, var3, fmt3) => {
  160. variable = this.index[var1 || var2 || var3];
  161. fmt = fmt2 || fmt3 || format;
  162. if (scopedVars) {
  163. value = scopedVars[var1 || var2 || var3];
  164. if (value) {
  165. return this.formatValue(value.value, fmt, variable);
  166. }
  167. }
  168. if (!variable) {
  169. return match;
  170. }
  171. systemValue = this.grafanaVariables[variable.current.value];
  172. if (systemValue) {
  173. return this.formatValue(systemValue, fmt, variable);
  174. }
  175. value = variable.current.value;
  176. if (this.isAllValue(value)) {
  177. value = this.getAllValue(variable);
  178. // skip formatting of custom all values
  179. if (variable.allValue) {
  180. return this.replace(value);
  181. }
  182. }
  183. const res = this.formatValue(value, fmt, variable);
  184. return res;
  185. });
  186. }
  187. isAllValue(value) {
  188. return value === '$__all' || (Array.isArray(value) && value[0] === '$__all');
  189. }
  190. replaceWithText(target, scopedVars) {
  191. if (!target) {
  192. return target;
  193. }
  194. var variable;
  195. this.regex.lastIndex = 0;
  196. return target.replace(this.regex, (match, var1, var2, fmt2, var3) => {
  197. if (scopedVars) {
  198. const option = scopedVars[var1 || var2 || var3];
  199. if (option) {
  200. return option.text;
  201. }
  202. }
  203. variable = this.index[var1 || var2 || var3];
  204. if (!variable) {
  205. return match;
  206. }
  207. return this.grafanaVariables[variable.current.value] || variable.current.text;
  208. });
  209. }
  210. fillVariableValuesForUrl(params, scopedVars) {
  211. _.each(this.variables, function(variable) {
  212. if (scopedVars && scopedVars[variable.name] !== void 0) {
  213. if (scopedVars[variable.name].skipUrlSync) {
  214. return;
  215. }
  216. params['var-' + variable.name] = scopedVars[variable.name].value;
  217. } else {
  218. if (variable.skipUrlSync) {
  219. return;
  220. }
  221. params['var-' + variable.name] = variable.getValueForUrl();
  222. }
  223. });
  224. }
  225. distributeVariable(value, variable) {
  226. value = _.map(value, function(val, index) {
  227. if (index !== 0) {
  228. return variable + '=' + val;
  229. } else {
  230. return val;
  231. }
  232. });
  233. return value.join(',');
  234. }
  235. }
  236. export default new TemplateSrv();