query_variable.ts 5.4 KB

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