templateValuesSrv.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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, recursive) {
  63. variable.current = option;
  64. templateSrv.updateTemplateData();
  65. return this.updateOptionsInChildVariables(variable)
  66. .then(function() {
  67. if (!recursive) {
  68. $rootScope.$broadcast('refresh');
  69. }
  70. });
  71. };
  72. this.updateOptionsInChildVariables = function(updatedVariable) {
  73. var promises = _.map(self.variables, function(otherVariable) {
  74. if (otherVariable === updatedVariable) {
  75. return;
  76. }
  77. if (templateSrv.containsVariable(otherVariable.query, updatedVariable.name)) {
  78. return self.updateOptions(otherVariable);
  79. }
  80. });
  81. return $q.all(promises);
  82. };
  83. this._updateNonQueryVariable = function(variable) {
  84. // extract options in comma seperated string
  85. variable.options = _.map(variable.query.split(/[,]+/), function(text) {
  86. return { text: text.trim(), value: text.trim() };
  87. });
  88. if (variable.type === 'interval') {
  89. self.updateAutoInterval(variable);
  90. }
  91. };
  92. this.updateOptions = function(variable) {
  93. if (variable.type !== 'query') {
  94. self._updateNonQueryVariable(variable);
  95. self.setVariableValue(variable, variable.options[0]);
  96. return $q.when([]);
  97. }
  98. return datasourceSrv.get(variable.datasource).then(function(datasource) {
  99. return datasource.metricFindQuery(variable.query).then(function (results) {
  100. variable.options = self.metricNamesToVariableValues(variable, results);
  101. if (variable.includeAll) {
  102. self.addAllOption(variable);
  103. }
  104. // if parameter has current value
  105. // if it exists in options array keep value
  106. if (variable.current) {
  107. var currentOption = _.findWhere(variable.options, { text: variable.current.text });
  108. if (currentOption) {
  109. return self.setVariableValue(variable, currentOption, true);
  110. }
  111. }
  112. return self.setVariableValue(variable, variable.options[0], true);
  113. });
  114. });
  115. };
  116. this.metricNamesToVariableValues = function(variable, metricNames) {
  117. var regex, options, i, matches;
  118. options = {}; // use object hash to remove duplicates
  119. if (variable.regex) {
  120. regex = kbn.stringToJsRegex(templateSrv.replace(variable.regex));
  121. }
  122. for (i = 0; i < metricNames.length; i++) {
  123. var value = metricNames[i].text;
  124. if (regex) {
  125. matches = regex.exec(value);
  126. if (!matches) { continue; }
  127. if (matches.length > 1) {
  128. value = matches[1];
  129. }
  130. }
  131. options[value] = value;
  132. }
  133. return _.map(_.keys(options), function(key) {
  134. return { text: key, value: key };
  135. });
  136. };
  137. this.addAllOption = function(variable) {
  138. var allValue = '';
  139. switch(variable.allFormat) {
  140. case 'wildcard':
  141. allValue = '*';
  142. break;
  143. case 'regex wildcard':
  144. allValue = '.*';
  145. break;
  146. case 'regex values':
  147. allValue = '(' + _.pluck(variable.options, 'text').join('|') + ')';
  148. break;
  149. default:
  150. allValue = '{';
  151. allValue += _.pluck(variable.options, 'text').join(',');
  152. allValue += '}';
  153. }
  154. variable.options.unshift({text: 'All', value: allValue});
  155. };
  156. });
  157. });