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