query_variable.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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.get(this.datasource)
  71. .then(this.updateOptionsFromMetricFindQuery.bind(this))
  72. .then(this.updateTags.bind(this))
  73. .then(this.variableSrv.validateVariableSelectionState.bind(this.variableSrv, this));
  74. }
  75. updateTags(datasource) {
  76. if (this.useTags) {
  77. return this.metricFindQuery(datasource, this.tagsQuery).then(results => {
  78. this.tags = [];
  79. for (var i = 0; i < results.length; i++) {
  80. this.tags.push(results[i].text);
  81. }
  82. return datasource;
  83. });
  84. } else {
  85. delete this.tags;
  86. }
  87. return datasource;
  88. }
  89. getValuesForTag(tagKey) {
  90. return this.datasourceSrv.get(this.datasource).then(datasource => {
  91. var query = this.tagValuesQuery.replace('$tag', tagKey);
  92. return this.metricFindQuery(datasource, query).then(function (results) {
  93. return _.map(results, function(value) {
  94. return value.text;
  95. });
  96. });
  97. });
  98. }
  99. updateOptionsFromMetricFindQuery(datasource) {
  100. return this.metricFindQuery(datasource, this.query).then(results => {
  101. this.options = this.metricNamesToVariableValues(results);
  102. if (this.includeAll) {
  103. this.addAllOption();
  104. }
  105. if (!this.options.length) {
  106. this.options.push(getNoneOption());
  107. }
  108. return datasource;
  109. });
  110. }
  111. metricFindQuery(datasource, query) {
  112. var options = {range: undefined, variable: this};
  113. if (this.refresh === 2) {
  114. options.range = this.timeSrv.timeRange();
  115. }
  116. return datasource.metricFindQuery(query, options);
  117. }
  118. addAllOption() {
  119. this.options.unshift({text: 'All', value: "$__all"});
  120. }
  121. metricNamesToVariableValues(metricNames) {
  122. var regex, options, i, matches;
  123. options = [];
  124. if (this.regex) {
  125. regex = kbn.stringToJsRegex(this.templateSrv.replace(this.regex, {}, 'regex'));
  126. }
  127. for (i = 0; i < metricNames.length; i++) {
  128. var item = metricNames[i];
  129. var text = item.text === undefined || item.text === null
  130. ? item.value
  131. : item.text;
  132. var value = item.value === undefined || item.value === null
  133. ? item.text
  134. : item.value;
  135. if (_.isNumber(value)) {
  136. value = value.toString();
  137. }
  138. if (_.isNumber(text)) {
  139. text = text.toString();
  140. }
  141. if (regex) {
  142. matches = regex.exec(value);
  143. if (!matches) { continue; }
  144. if (matches.length > 1) {
  145. value = matches[1];
  146. text = matches[1];
  147. }
  148. }
  149. options.push({text: text, value: value});
  150. }
  151. options = _.uniqBy(options, 'value');
  152. return this.sortVariableValues(options, this.sort);
  153. }
  154. sortVariableValues(options, sortOrder) {
  155. if (sortOrder === 0) {
  156. return options;
  157. }
  158. var sortType = Math.ceil(sortOrder / 2);
  159. var reverseSort = (sortOrder % 2 === 0);
  160. if (sortType === 1) {
  161. options = _.sortBy(options, 'text');
  162. } else if (sortType === 2) {
  163. options = _.sortBy(options, (opt) => {
  164. var matches = opt.text.match(/.*?(\d+).*/);
  165. if (!matches || matches.length < 2) {
  166. return -1;
  167. } else {
  168. return parseInt(matches[1], 10);
  169. }
  170. });
  171. }
  172. if (reverseSort) {
  173. options = options.reverse();
  174. }
  175. return options;
  176. }
  177. dependsOn(variable) {
  178. return containsVariable(this.query, this.datasource, variable.name);
  179. }
  180. }
  181. variableTypes['query'] = {
  182. name: 'Query',
  183. ctor: QueryVariable,
  184. description: 'Variable values are fetched from a datasource query',
  185. supportsMulti: true,
  186. };