template_srv.ts 6.9 KB

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