query_variable.ts 3.9 KB

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