templateValuesSrv.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. define([
  2. 'angular',
  3. 'lodash',
  4. 'kbn',
  5. ],
  6. function (angular, _, kbn) {
  7. 'use strict';
  8. var module = angular.module('grafana.services');
  9. module.service('templateValuesSrv', function($q, $rootScope, datasourceSrv, $location, templateSrv, timeSrv) {
  10. var self = this;
  11. $rootScope.onAppEvent('time-range-changed', function() {
  12. var variable = _.findWhere(self.variables, { type: 'interval' });
  13. if (variable) {
  14. self.updateAutoInterval(variable);
  15. }
  16. });
  17. this.init = function(dashboard) {
  18. this.variables = dashboard.templating.list;
  19. templateSrv.init(this.variables);
  20. var queryParams = $location.search();
  21. var promises = [];
  22. for (var i = 0; i < this.variables.length; i++) {
  23. var variable = this.variables[i];
  24. var urlValue = queryParams['var-' + variable.name];
  25. if (urlValue !== void 0) {
  26. promises.push(this.setVariableFromUrl(variable, urlValue));
  27. }
  28. else if (variable.refresh) {
  29. promises.push(this.updateOptions(variable));
  30. }
  31. else if (variable.type === 'interval') {
  32. this.updateAutoInterval(variable);
  33. }
  34. }
  35. return $q.all(promises);
  36. };
  37. this.setVariableFromUrl = function(variable, urlValue) {
  38. if (variable.refresh) {
  39. var self = this;
  40. //refresh the list of options before setting the value
  41. return this.updateOptions(variable).then(function() {
  42. var option = _.findWhere(variable.options, { text: urlValue });
  43. option = option || { text: urlValue, value: urlValue };
  44. self.updateAutoInterval(variable);
  45. return self.setVariableValue(variable, option);
  46. });
  47. }
  48. var option = _.findWhere(variable.options, { text: urlValue });
  49. option = option || { text: urlValue, value: urlValue };
  50. this.updateAutoInterval(variable);
  51. return this.setVariableValue(variable, option);
  52. };
  53. this.updateAutoInterval = function(variable) {
  54. if (!variable.auto) { return; }
  55. // add auto option if missing
  56. if (variable.options.length && variable.options[0].text !== 'auto') {
  57. variable.options.unshift({ text: 'auto', value: '$__auto_interval' });
  58. }
  59. var interval = kbn.calculateInterval(timeSrv.timeRange(), variable.auto_count);
  60. templateSrv.setGrafanaVariable('$__auto_interval', interval);
  61. };
  62. this.setVariableValue = function(variable, option) {
  63. variable.current = option;
  64. templateSrv.updateTemplateData();
  65. return this.updateOptionsInChildVariables(variable);
  66. };
  67. this.variableUpdated = function(variable) {
  68. templateSrv.updateTemplateData();
  69. return this.updateOptionsInChildVariables(variable);
  70. };
  71. this.updateOptionsInChildVariables = function(updatedVariable) {
  72. var promises = _.map(self.variables, function(otherVariable) {
  73. if (otherVariable === updatedVariable) {
  74. return;
  75. }
  76. if (templateSrv.containsVariable(otherVariable.query, updatedVariable.name)) {
  77. return self.updateOptions(otherVariable);
  78. }
  79. });
  80. return $q.all(promises);
  81. };
  82. this._updateNonQueryVariable = function(variable) {
  83. // extract options in comma seperated string
  84. variable.options = _.map(variable.query.split(/[,]+/), function(text) {
  85. return { text: text.trim(), value: text.trim() };
  86. });
  87. if (variable.type === 'interval') {
  88. self.updateAutoInterval(variable);
  89. }
  90. };
  91. this.updateOptions = function(variable) {
  92. if (variable.type !== 'query') {
  93. self._updateNonQueryVariable(variable);
  94. self.setVariableValue(variable, variable.options[0]);
  95. return $q.when([]);
  96. }
  97. return datasourceSrv.get(variable.datasource).then(function(datasource) {
  98. return datasource.metricFindQuery(variable.query).then(function (results) {
  99. variable.options = self.metricNamesToVariableValues(variable, results);
  100. if (variable.includeAll) {
  101. self.addAllOption(variable);
  102. }
  103. // if parameter has current value
  104. // if it exists in options array keep value
  105. if (variable.current) {
  106. var currentOption = _.findWhere(variable.options, { text: variable.current.text });
  107. if (currentOption) {
  108. return self.setVariableValue(variable, currentOption);
  109. }
  110. }
  111. return self.setVariableValue(variable, variable.options[0]);
  112. });
  113. });
  114. };
  115. this.metricNamesToVariableValues = function(variable, metricNames) {
  116. var regex, options, i, matches;
  117. options = {}; // use object hash to remove duplicates
  118. if (variable.regex) {
  119. regex = kbn.stringToJsRegex(templateSrv.replace(variable.regex));
  120. }
  121. for (i = 0; i < metricNames.length; i++) {
  122. var value = metricNames[i].text;
  123. if (regex) {
  124. matches = regex.exec(value);
  125. if (!matches) { continue; }
  126. if (matches.length > 1) {
  127. value = matches[1];
  128. }
  129. }
  130. options[value] = value;
  131. }
  132. return _.map(_.keys(options), function(key) {
  133. return { text: key, value: key };
  134. });
  135. };
  136. this.addAllOption = function(variable) {
  137. var allValue = '';
  138. switch(variable.allFormat) {
  139. case 'wildcard':
  140. allValue = '*';
  141. break;
  142. case 'regex wildcard':
  143. allValue = '.*';
  144. break;
  145. case 'regex values':
  146. allValue = '(' + _.pluck(variable.options, 'text').join('|') + ')';
  147. break;
  148. default:
  149. allValue = '{';
  150. allValue += _.pluck(variable.options, 'text').join(',');
  151. allValue += '}';
  152. }
  153. variable.options.unshift({text: 'All', value: allValue});
  154. };
  155. });
  156. });