template_srv.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. import kbn from 'app/core/utils/kbn';
  2. import _ from 'lodash';
  3. function luceneEscape(value) {
  4. return value.replace(
  5. /([\!\*\+\-\=<>\s\&\|\(\)\[\]\{\}\^\~\?\:\\/"])/g,
  6. '\\$1'
  7. );
  8. }
  9. export class TemplateSrv {
  10. variables: any[];
  11. private regex = /\$(\w+)|\[\[([\s\S]+?)\]\]/g;
  12. private index = {};
  13. private grafanaVariables = {};
  14. private builtIns = {};
  15. constructor() {
  16. this.builtIns['__interval'] = { text: '1s', value: '1s' };
  17. this.builtIns['__interval_ms'] = { text: '100', value: '100' };
  18. }
  19. init(variables) {
  20. this.variables = variables;
  21. this.updateTemplateData();
  22. }
  23. updateTemplateData() {
  24. this.index = {};
  25. for (var i = 0; i < this.variables.length; i++) {
  26. var variable = this.variables[i];
  27. if (
  28. !variable.current ||
  29. (!variable.current.isNone && !variable.current.value)
  30. ) {
  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. return '(' + escapedValues.join('|') + ')';
  79. }
  80. case 'lucene': {
  81. return this.luceneFormat(value);
  82. }
  83. case 'pipe': {
  84. if (typeof value === 'string') {
  85. return value;
  86. }
  87. return value.join('|');
  88. }
  89. case 'distributed': {
  90. if (typeof value === 'string') {
  91. return value;
  92. }
  93. return this.distributeVariable(value, variable.name);
  94. }
  95. default: {
  96. if (_.isArray(value)) {
  97. return '{' + value.join(',') + '}';
  98. }
  99. return value;
  100. }
  101. }
  102. }
  103. setGrafanaVariable(name, value) {
  104. this.grafanaVariables[name] = value;
  105. }
  106. getVariableName(expression) {
  107. this.regex.lastIndex = 0;
  108. var match = this.regex.exec(expression);
  109. if (!match) {
  110. return null;
  111. }
  112. return match[1] || match[2];
  113. }
  114. variableExists(expression) {
  115. var name = this.getVariableName(expression);
  116. return name && this.index[name] !== void 0;
  117. }
  118. highlightVariablesAsHtml(str) {
  119. if (!str || !_.isString(str)) {
  120. return str;
  121. }
  122. str = _.escape(str);
  123. this.regex.lastIndex = 0;
  124. return str.replace(this.regex, (match, g1, g2) => {
  125. if (this.index[g1 || g2] || this.builtIns[g1 || g2]) {
  126. return '<span class="template-variable">' + match + '</span>';
  127. }
  128. return match;
  129. });
  130. }
  131. getAllValue(variable) {
  132. if (variable.allValue) {
  133. return variable.allValue;
  134. }
  135. var values = [];
  136. for (var i = 1; i < variable.options.length; i++) {
  137. values.push(variable.options[i].value);
  138. }
  139. return values;
  140. }
  141. replace(target, scopedVars?, format?) {
  142. if (!target) {
  143. return target;
  144. }
  145. var variable, systemValue, value;
  146. this.regex.lastIndex = 0;
  147. return target.replace(this.regex, (match, g1, g2) => {
  148. variable = this.index[g1 || g2];
  149. if (scopedVars) {
  150. value = scopedVars[g1 || g2];
  151. if (value) {
  152. return this.formatValue(value.value, format, variable);
  153. }
  154. }
  155. if (!variable) {
  156. return match;
  157. }
  158. systemValue = this.grafanaVariables[variable.current.value];
  159. if (systemValue) {
  160. return this.formatValue(systemValue, format, variable);
  161. }
  162. value = variable.current.value;
  163. if (this.isAllValue(value)) {
  164. value = this.getAllValue(variable);
  165. // skip formating of custom all values
  166. if (variable.allValue) {
  167. return value;
  168. }
  169. }
  170. var res = this.formatValue(value, format, variable);
  171. return res;
  172. });
  173. }
  174. isAllValue(value) {
  175. return (
  176. value === '$__all' || (Array.isArray(value) && value[0] === '$__all')
  177. );
  178. }
  179. replaceWithText(target, scopedVars) {
  180. if (!target) {
  181. return target;
  182. }
  183. var variable;
  184. this.regex.lastIndex = 0;
  185. return target.replace(this.regex, (match, g1, g2) => {
  186. if (scopedVars) {
  187. var option = scopedVars[g1 || g2];
  188. if (option) {
  189. return option.text;
  190. }
  191. }
  192. variable = this.index[g1 || g2];
  193. if (!variable) {
  194. return match;
  195. }
  196. return (
  197. this.grafanaVariables[variable.current.value] || variable.current.text
  198. );
  199. });
  200. }
  201. fillVariableValuesForUrl(params, scopedVars) {
  202. _.each(this.variables, function(variable) {
  203. if (scopedVars && scopedVars[variable.name] !== void 0) {
  204. params['var-' + variable.name] = scopedVars[variable.name].value;
  205. } else {
  206. params['var-' + variable.name] = variable.getValueForUrl();
  207. }
  208. });
  209. }
  210. distributeVariable(value, variable) {
  211. value = _.map(value, function(val, index) {
  212. if (index !== 0) {
  213. return variable + '=' + val;
  214. } else {
  215. return val;
  216. }
  217. });
  218. return value.join(',');
  219. }
  220. }
  221. export default new TemplateSrv();