query_variable.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. definition: '',
  45. };
  46. /** @ngInject */
  47. constructor(private model, private datasourceSrv, private templateSrv, private variableSrv, private timeSrv) {
  48. // copy model properties to this instance
  49. assignModelProperties(this, model, this.defaults);
  50. }
  51. getSaveModel() {
  52. // copy back model properties to model
  53. assignModelProperties(this.model, this, this.defaults);
  54. // remove options
  55. if (this.refresh !== 0) {
  56. this.model.options = [];
  57. }
  58. return this.model;
  59. }
  60. setValue(option) {
  61. return this.variableSrv.setOptionAsCurrent(this, option);
  62. }
  63. setValueFromUrl(urlValue) {
  64. return this.variableSrv.setOptionFromUrl(this, urlValue);
  65. }
  66. getValueForUrl() {
  67. if (this.current.text === 'All') {
  68. return 'All';
  69. }
  70. return this.current.value;
  71. }
  72. updateOptions() {
  73. return this.datasourceSrv
  74. .get(this.datasource)
  75. .then(this.updateOptionsFromMetricFindQuery.bind(this))
  76. .then(this.updateTags.bind(this))
  77. .then(this.variableSrv.validateVariableSelectionState.bind(this.variableSrv, this));
  78. }
  79. updateTags(datasource) {
  80. if (this.useTags) {
  81. return this.metricFindQuery(datasource, this.tagsQuery).then(results => {
  82. this.tags = [];
  83. for (let i = 0; i < results.length; i++) {
  84. this.tags.push(results[i].text);
  85. }
  86. return datasource;
  87. });
  88. } else {
  89. delete this.tags;
  90. }
  91. return datasource;
  92. }
  93. getValuesForTag(tagKey) {
  94. return this.datasourceSrv.get(this.datasource).then(datasource => {
  95. const query = this.tagValuesQuery.replace('$tag', tagKey);
  96. return this.metricFindQuery(datasource, query).then(results => {
  97. return _.map(results, value => {
  98. return value.text;
  99. });
  100. });
  101. });
  102. }
  103. updateOptionsFromMetricFindQuery(datasource) {
  104. return this.metricFindQuery(datasource, this.query).then(results => {
  105. this.options = this.metricNamesToVariableValues(results);
  106. if (this.includeAll) {
  107. this.addAllOption();
  108. }
  109. if (!this.options.length) {
  110. this.options.push(getNoneOption());
  111. }
  112. return datasource;
  113. });
  114. }
  115. metricFindQuery(datasource, query) {
  116. const options = { range: undefined, variable: this };
  117. if (this.refresh === 2) {
  118. options.range = this.timeSrv.timeRange();
  119. }
  120. return datasource.metricFindQuery(query, options);
  121. }
  122. addAllOption() {
  123. this.options.unshift({ text: 'All', value: '$__all' });
  124. }
  125. metricNamesToVariableValues(metricNames) {
  126. let regex, options, i, matches;
  127. options = [];
  128. if (this.regex) {
  129. regex = kbn.stringToJsRegex(this.templateSrv.replace(this.regex, {}, 'regex'));
  130. }
  131. for (i = 0; i < metricNames.length; i++) {
  132. const item = metricNames[i];
  133. let text = item.text === undefined || item.text === null ? item.value : item.text;
  134. let value = item.value === undefined || item.value === null ? item.text : 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) {
  144. continue;
  145. }
  146. if (matches.length > 1) {
  147. value = matches[1];
  148. text = matches[1];
  149. }
  150. }
  151. options.push({ text: text, value: value });
  152. }
  153. options = _.uniqBy(options, 'value');
  154. return this.sortVariableValues(options, this.sort);
  155. }
  156. sortVariableValues(options, sortOrder) {
  157. if (sortOrder === 0) {
  158. return options;
  159. }
  160. const sortType = Math.ceil(sortOrder / 2);
  161. const reverseSort = sortOrder % 2 === 0;
  162. if (sortType === 1) {
  163. options = _.sortBy(options, 'text');
  164. } else if (sortType === 2) {
  165. options = _.sortBy(options, opt => {
  166. const matches = opt.text.match(/.*?(\d+).*/);
  167. if (!matches || matches.length < 2) {
  168. return -1;
  169. } else {
  170. return parseInt(matches[1], 10);
  171. }
  172. });
  173. } else if (sortType === 3) {
  174. options = _.sortBy(options, opt => {
  175. return _.toLower(opt.text);
  176. });
  177. }
  178. if (reverseSort) {
  179. options = options.reverse();
  180. }
  181. return options;
  182. }
  183. dependsOn(variable) {
  184. return containsVariable(this.query, this.datasource, this.regex, variable.name);
  185. }
  186. }
  187. variableTypes['query'] = {
  188. name: 'Query',
  189. ctor: QueryVariable,
  190. description: 'Variable values are fetched from a datasource query',
  191. supportsMulti: true,
  192. };