templateValuesSrv.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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) {
  63. variable.current = angular.copy(option);
  64. if (_.isArray(variable.current.value)) {
  65. variable.current.text = variable.current.value.join(' + ');
  66. }
  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)
  101. .then(_.partial(this.updateOptionsFromMetricFindQuery, variable))
  102. .then(_.partial(this.updateTags, variable))
  103. .then(_.partial(this.validateVariableSelectionState, variable));
  104. };
  105. this.validateVariableSelectionState = function(variable) {
  106. if (!variable.current) {
  107. if (!variable.options.length) { return; }
  108. return self.setVariableValue(variable, variable.options[0]);
  109. }
  110. if (_.isArray(variable.current.value)) {
  111. for (var i = 0; i < variable.current.value.length; i++) {
  112. var value = variable.current.value[i];
  113. for (var y = 0; y < variable.options.length; y++) {
  114. var option = variable.options[y];
  115. if (option.value === value) {
  116. option.selected = true;
  117. }
  118. }
  119. }
  120. } else {
  121. var currentOption = _.findWhere(variable.options, { text: variable.current.text });
  122. if (currentOption) {
  123. return self.setVariableValue(variable, currentOption);
  124. } else {
  125. if (!variable.options.length) { return; }
  126. return self.setVariableValue(variable, variable.options[0]);
  127. }
  128. }
  129. };
  130. this.updateTags = function(variable, datasource) {
  131. if (variable.useTags) {
  132. return datasource.metricFindQuery(variable.tagsQuery).then(function (results) {
  133. variable.tags = [];
  134. for (var i = 0; i < results.length; i++) {
  135. variable.tags.push(results[i].text);
  136. }
  137. return datasource;
  138. });
  139. } else {
  140. delete variable.tags;
  141. }
  142. return datasource;
  143. };
  144. this.updateOptionsFromMetricFindQuery = function(variable, datasource) {
  145. return datasource.metricFindQuery(variable.query).then(function (results) {
  146. variable.options = self.metricNamesToVariableValues(variable, results);
  147. if (variable.includeAll) {
  148. self.addAllOption(variable);
  149. }
  150. return datasource;
  151. });
  152. };
  153. this.getValuesForTag = function(variable, tagKey) {
  154. return datasourceSrv.get(variable.datasource).then(function(datasource) {
  155. var query = variable.tagValuesQuery.replace('$tag', tagKey);
  156. return datasource.metricFindQuery(query).then(function (results) {
  157. return _.map(results, function(value) {
  158. return value.text;
  159. });
  160. });
  161. });
  162. };
  163. this.metricNamesToVariableValues = function(variable, metricNames) {
  164. var regex, options, i, matches;
  165. options = {}; // use object hash to remove duplicates
  166. if (variable.regex) {
  167. regex = kbn.stringToJsRegex(templateSrv.replace(variable.regex));
  168. }
  169. for (i = 0; i < metricNames.length; i++) {
  170. var value = metricNames[i].text;
  171. if (regex) {
  172. matches = regex.exec(value);
  173. if (!matches) { continue; }
  174. if (matches.length > 1) {
  175. value = matches[1];
  176. }
  177. }
  178. options[value] = value;
  179. }
  180. return _.map(_.keys(options).sort(), function(key) {
  181. return { text: key, value: key };
  182. });
  183. };
  184. this.addAllOption = function(variable) {
  185. var allValue = '';
  186. switch(variable.allFormat) {
  187. case 'wildcard':
  188. allValue = '*';
  189. break;
  190. case 'regex wildcard':
  191. allValue = '.*';
  192. break;
  193. case 'regex values':
  194. allValue = '(' + _.pluck(variable.options, 'text').join('|') + ')';
  195. break;
  196. default:
  197. allValue = '{';
  198. allValue += _.pluck(variable.options, 'text').join(',');
  199. allValue += '}';
  200. }
  201. variable.options.unshift({text: 'All', value: allValue});
  202. };
  203. });
  204. });