query_variable.ts 5.3 KB

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