query_variable.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. ///<reference path="../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. import kbn from 'app/core/utils/kbn';
  4. import {Variable, containsVariable} from './variable';
  5. import {VariableSrv, variableConstructorMap} from './variable_srv';
  6. function getNoneOption() {
  7. return { text: 'None', value: '', isNone: true };
  8. }
  9. export class QueryVariable implements Variable {
  10. datasource: any;
  11. query: any;
  12. regex: any;
  13. sort: any;
  14. options: any;
  15. current: any;
  16. includeAll: boolean;
  17. refresh: number;
  18. constructor(private model, private datasourceSrv, private templateSrv, private variableSrv, private $q) {
  19. _.extend(this, model);
  20. }
  21. setValue(option){
  22. this.variableSrv.setOptionAsCurrent(this, option);
  23. }
  24. setValueFromUrl(urlValue) {
  25. return this.variableSrv.setOptionFromUrl(this, urlValue);
  26. }
  27. updateOptions() {
  28. return this.datasourceSrv.get(this.datasource)
  29. .then(this.updateOptionsFromMetricFindQuery.bind(this))
  30. .then(() => {
  31. this.variableSrv.validateVariableSelectionState(this);
  32. });
  33. }
  34. updateOptionsFromMetricFindQuery(datasource) {
  35. return datasource.metricFindQuery(this.query).then(results => {
  36. this.options = this.metricNamesToVariableValues(results);
  37. if (this.includeAll) {
  38. this.addAllOption();
  39. }
  40. if (!this.options.length) {
  41. this.options.push(getNoneOption());
  42. }
  43. return datasource;
  44. });
  45. }
  46. addAllOption() {
  47. this.options.unshift({text: 'All', value: "$__all"});
  48. }
  49. metricNamesToVariableValues(metricNames) {
  50. var regex, options, i, matches;
  51. options = [];
  52. if (this.model.regex) {
  53. regex = kbn.stringToJsRegex(this.templateSrv.replace(this.regex));
  54. }
  55. for (i = 0; i < metricNames.length; i++) {
  56. var item = metricNames[i];
  57. var value = item.value || item.text;
  58. var text = item.text || item.value;
  59. if (_.isNumber(value)) {
  60. value = value.toString();
  61. }
  62. if (_.isNumber(text)) {
  63. text = text.toString();
  64. }
  65. if (regex) {
  66. matches = regex.exec(value);
  67. if (!matches) { continue; }
  68. if (matches.length > 1) {
  69. value = matches[1];
  70. text = value;
  71. }
  72. }
  73. options.push({text: text, value: value});
  74. }
  75. options = _.uniq(options, 'value');
  76. return this.sortVariableValues(options, this.sort);
  77. }
  78. sortVariableValues(options, sortOrder) {
  79. if (sortOrder === 0) {
  80. return options;
  81. }
  82. var sortType = Math.ceil(sortOrder / 2);
  83. var reverseSort = (sortOrder % 2 === 0);
  84. if (sortType === 1) {
  85. options = _.sortBy(options, 'text');
  86. } else if (sortType === 2) {
  87. options = _.sortBy(options, function(opt) {
  88. var matches = opt.text.match(/.*?(\d+).*/);
  89. if (!matches) {
  90. return 0;
  91. } else {
  92. return parseInt(matches[1], 10);
  93. }
  94. });
  95. }
  96. if (reverseSort) {
  97. options = options.reverse();
  98. }
  99. return options;
  100. }
  101. dependsOn(variable) {
  102. return containsVariable(this.query, this.datasource, variable.name);
  103. }
  104. }
  105. variableConstructorMap['query'] = QueryVariable;