query_variable.ts 4.0 KB

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