templateValuesSrv.js 5.0 KB

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