templateValuesSrv.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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) {
  10. var self = this;
  11. this.init = function(dashboard) {
  12. this.variables = dashboard.templating.list;
  13. templateSrv.init(this.variables);
  14. for (var i = 0; i < this.variables.length; i++) {
  15. var param = this.variables[i];
  16. if (param.refresh) {
  17. this.updateOptions(param);
  18. }
  19. }
  20. };
  21. this.setVariableValue = function(variable, option, recursive) {
  22. variable.current = option;
  23. templateSrv.updateTemplateData();
  24. return this.updateOptionsInChildVariables(variable)
  25. .then(function() {
  26. if (!recursive) {
  27. $rootScope.$broadcast('refresh');
  28. }
  29. });
  30. };
  31. this.updateOptionsInChildVariables = function(updatedVariable) {
  32. var promises = _.map(self.variables, function(otherVariable) {
  33. if (otherVariable === updatedVariable) {
  34. return;
  35. }
  36. if (otherVariable.query.indexOf('[[' + updatedVariable.name + ']]') !== -1) {
  37. return self.updateOptions(otherVariable);
  38. }
  39. });
  40. return $q.all(promises);
  41. };
  42. this.updateOptions = function(variable) {
  43. if (variable.type === 'time period') {
  44. variable.options = _.map(variable.query.split(','), function(text) {
  45. return { text: text, value: text };
  46. });
  47. self.setVariableValue(variable, variable.options[0]);
  48. return;
  49. }
  50. var datasource = datasourceSrv.get(variable.datasource);
  51. return datasource.metricFindQuery(variable.query)
  52. .then(function (results) {
  53. variable.options = self.metricNamesToVariableValues(variable, results);
  54. if (variable.includeAll) {
  55. self.addAllOption(variable);
  56. }
  57. // if parameter has current value
  58. // if it exists in options array keep value
  59. if (variable.current) {
  60. var currentExists = _.findWhere(variable.options, { value: variable.current.value });
  61. if (currentExists) {
  62. return self.setVariableValue(variable, variable.current, true);
  63. }
  64. }
  65. return self.setVariableValue(variable, variable.options[0], true);
  66. });
  67. };
  68. this.metricNamesToVariableValues = function(variable, metricNames) {
  69. var regex, options, i, matches;
  70. options = [];
  71. if (variable.regex) {
  72. regex = kbn.stringToJsRegex(variable.regex);
  73. }
  74. for (i = 0; i < metricNames.length; i++) {
  75. var value = metricNames[i].text;
  76. if (regex) {
  77. matches = regex.exec(value);
  78. if (!matches) { continue; }
  79. if (matches.length > 1) {
  80. value = matches[1];
  81. }
  82. }
  83. options.push({text: value, value: value});
  84. }
  85. return options;
  86. };
  87. this.addAllOption = function(variable) {
  88. var allValue = '';
  89. switch(variable.allFormat) {
  90. case 'wildcard':
  91. allValue = '*';
  92. break;
  93. case 'regex wildcard':
  94. allValue = '.*';
  95. break;
  96. default:
  97. allValue = '{';
  98. _.each(variable.options, function(option) {
  99. allValue += option.text + ',';
  100. });
  101. allValue = allValue.substring(0, allValue.length - 1) + '}';
  102. }
  103. variable.options.unshift({text: 'All', value: allValue});
  104. };
  105. });
  106. });