query_variable.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. ///<reference path="../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. import kbn from 'app/core/utils/kbn';
  4. import {Variable, containsVariable, assignModelProperties, variableTypes} from './variable';
  5. import {VariableSrv} 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. return 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(this.variableSrv.validateVariableSelectionState.bind(this.variableSrv, this));
  54. }
  55. updateOptionsFromMetricFindQuery(datasource) {
  56. return datasource.metricFindQuery(this.query).then(results => {
  57. this.options = this.metricNamesToVariableValues(results);
  58. if (this.includeAll) {
  59. this.addAllOption();
  60. }
  61. if (!this.options.length) {
  62. this.options.push(getNoneOption());
  63. }
  64. return datasource;
  65. });
  66. }
  67. addAllOption() {
  68. this.options.unshift({text: 'All', value: "$__all"});
  69. }
  70. metricNamesToVariableValues(metricNames) {
  71. var regex, options, i, matches;
  72. options = [];
  73. if (this.model.regex) {
  74. regex = kbn.stringToJsRegex(this.templateSrv.replace(this.regex));
  75. }
  76. for (i = 0; i < metricNames.length; i++) {
  77. var item = metricNames[i];
  78. var value = item.value || item.text;
  79. var text = item.text || item.value;
  80. if (_.isNumber(value)) {
  81. value = value.toString();
  82. }
  83. if (_.isNumber(text)) {
  84. text = text.toString();
  85. }
  86. if (regex) {
  87. matches = regex.exec(value);
  88. if (!matches) { continue; }
  89. if (matches.length > 1) {
  90. value = matches[1];
  91. text = value;
  92. }
  93. }
  94. options.push({text: text, value: value});
  95. }
  96. options = _.uniq(options, 'value');
  97. return this.sortVariableValues(options, this.sort);
  98. }
  99. sortVariableValues(options, sortOrder) {
  100. if (sortOrder === 0) {
  101. return options;
  102. }
  103. var sortType = Math.ceil(sortOrder / 2);
  104. var reverseSort = (sortOrder % 2 === 0);
  105. if (sortType === 1) {
  106. options = _.sortBy(options, 'text');
  107. } else if (sortType === 2) {
  108. options = _.sortBy(options, function(opt) {
  109. var matches = opt.text.match(/.*?(\d+).*/);
  110. if (!matches) {
  111. return 0;
  112. } else {
  113. return parseInt(matches[1], 10);
  114. }
  115. });
  116. }
  117. if (reverseSort) {
  118. options = options.reverse();
  119. }
  120. return options;
  121. }
  122. dependsOn(variable) {
  123. return containsVariable(this.query, this.datasource, variable.name);
  124. }
  125. }
  126. variableTypes['query'] = {
  127. name: 'Query',
  128. ctor: QueryVariable,
  129. description: 'Variable values are fetched from a datasource query',
  130. supportsMulti: true,
  131. };