query_variable.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. ///<reference path="../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. import kbn from 'app/core/utils/kbn';
  4. import {Variable, containsVariable, assignModelProperties} 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. refresh: number;
  17. hide: number;
  18. name: string;
  19. multi: boolean;
  20. includeAll: boolean;
  21. defaults = {
  22. type: 'query',
  23. query: '',
  24. regex: '',
  25. sort: 0,
  26. datasource: null,
  27. refresh: 0,
  28. hide: 0,
  29. name: '',
  30. multi: false,
  31. includeAll: false,
  32. options: [],
  33. current: {text: '', value: ''},
  34. };
  35. supportsMulti = true;
  36. constructor(private model, private datasourceSrv, private templateSrv, private variableSrv, private $q) {
  37. // copy model properties to this instance
  38. assignModelProperties(this, model, this.defaults);
  39. }
  40. getModel() {
  41. // copy back model properties to model
  42. assignModelProperties(this.model, this, this.defaults);
  43. return this.model;
  44. }
  45. setValue(option){
  46. return this.variableSrv.setOptionAsCurrent(this, option);
  47. }
  48. setValueFromUrl(urlValue) {
  49. return this.variableSrv.setOptionFromUrl(this, urlValue);
  50. }
  51. updateOptions() {
  52. return this.datasourceSrv.get(this.datasource)
  53. .then(this.updateOptionsFromMetricFindQuery.bind(this))
  54. .then(this.variableSrv.validateVariableSelectionState.bind(this.variableSrv, this));
  55. }
  56. updateOptionsFromMetricFindQuery(datasource) {
  57. return datasource.metricFindQuery(this.query).then(results => {
  58. this.options = this.metricNamesToVariableValues(results);
  59. if (this.includeAll) {
  60. this.addAllOption();
  61. }
  62. if (!this.options.length) {
  63. this.options.push(getNoneOption());
  64. }
  65. return datasource;
  66. });
  67. }
  68. addAllOption() {
  69. this.options.unshift({text: 'All', value: "$__all"});
  70. }
  71. metricNamesToVariableValues(metricNames) {
  72. var regex, options, i, matches;
  73. options = [];
  74. if (this.model.regex) {
  75. regex = kbn.stringToJsRegex(this.templateSrv.replace(this.regex));
  76. }
  77. for (i = 0; i < metricNames.length; i++) {
  78. var item = metricNames[i];
  79. var value = item.value || item.text;
  80. var text = item.text || item.value;
  81. if (_.isNumber(value)) {
  82. value = value.toString();
  83. }
  84. if (_.isNumber(text)) {
  85. text = text.toString();
  86. }
  87. if (regex) {
  88. matches = regex.exec(value);
  89. if (!matches) { continue; }
  90. if (matches.length > 1) {
  91. value = matches[1];
  92. text = value;
  93. }
  94. }
  95. options.push({text: text, value: value});
  96. }
  97. options = _.uniq(options, 'value');
  98. return this.sortVariableValues(options, this.sort);
  99. }
  100. sortVariableValues(options, sortOrder) {
  101. if (sortOrder === 0) {
  102. return options;
  103. }
  104. var sortType = Math.ceil(sortOrder / 2);
  105. var reverseSort = (sortOrder % 2 === 0);
  106. if (sortType === 1) {
  107. options = _.sortBy(options, 'text');
  108. } else if (sortType === 2) {
  109. options = _.sortBy(options, function(opt) {
  110. var matches = opt.text.match(/.*?(\d+).*/);
  111. if (!matches) {
  112. return 0;
  113. } else {
  114. return parseInt(matches[1], 10);
  115. }
  116. });
  117. }
  118. if (reverseSort) {
  119. options = options.reverse();
  120. }
  121. return options;
  122. }
  123. dependsOn(variable) {
  124. return containsVariable(this.query, this.datasource, variable.name);
  125. }
  126. }
  127. variableConstructorMap['query'] = QueryVariable;