query_variable.ts 5.3 KB

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