templateValuesSrv.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 = angular.copy(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. var queryPromise = 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. // if current value is an array do not do anything
  110. if (_.isArray(variable.current.value)) {
  111. return $q.when([]);
  112. }
  113. var currentOption = _.findWhere(variable.options, { text: variable.current.text });
  114. if (currentOption) {
  115. return self.setVariableValue(variable, currentOption);
  116. }
  117. }
  118. return self.setVariableValue(variable, variable.options[0]);
  119. });
  120. if (variable.useTags) {
  121. return queryPromise.then(function() {
  122. datasource.metricFindQuery(variable.tagsQuery).then(function (results) {
  123. variable.tags = [];
  124. for (var i = 0; i < results.length; i++) {
  125. variable.tags.push(results[i].text);
  126. }
  127. });
  128. });
  129. } else {
  130. delete variable.tags;
  131. return queryPromise;
  132. }
  133. });
  134. };
  135. this.getValuesForTag = function(variable, tagKey) {
  136. return datasourceSrv.get(variable.datasource).then(function(datasource) {
  137. var query = variable.tagValuesQuery.replace('$tag', tagKey);
  138. return datasource.metricFindQuery(query).then(function (results) {
  139. return _.map(results, function(value) {
  140. return value.text;
  141. });
  142. });
  143. });
  144. };
  145. this.metricNamesToVariableValues = function(variable, metricNames) {
  146. var regex, options, i, matches;
  147. options = {}; // use object hash to remove duplicates
  148. if (variable.regex) {
  149. regex = kbn.stringToJsRegex(templateSrv.replace(variable.regex));
  150. }
  151. for (i = 0; i < metricNames.length; i++) {
  152. var value = metricNames[i].text;
  153. if (regex) {
  154. matches = regex.exec(value);
  155. if (!matches) { continue; }
  156. if (matches.length > 1) {
  157. value = matches[1];
  158. }
  159. }
  160. options[value] = value;
  161. }
  162. return _.map(_.keys(options), function(key) {
  163. return { text: key, value: key };
  164. });
  165. };
  166. this.addAllOption = function(variable) {
  167. var allValue = '';
  168. switch(variable.allFormat) {
  169. case 'wildcard':
  170. allValue = '*';
  171. break;
  172. case 'regex wildcard':
  173. allValue = '.*';
  174. break;
  175. case 'regex values':
  176. allValue = '(' + _.pluck(variable.options, 'text').join('|') + ')';
  177. break;
  178. default:
  179. allValue = '{';
  180. allValue += _.pluck(variable.options, 'text').join(',');
  181. allValue += '}';
  182. }
  183. variable.options.unshift({text: 'All', value: allValue});
  184. };
  185. });
  186. });