query_variable.ts 3.9 KB

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