templateValuesSrv.js 5.2 KB

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