query_variable.ts 4.8 KB

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