templateValuesSrv.js 5.8 KB

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