query_variable.ts 5.3 KB

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