query_variable.ts 3.9 KB

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