query_variable.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. ///<reference path="../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. import kbn from 'app/core/utils/kbn';
  4. import {Variable, containsVariable, assignModelProperties, variableTypes} from './variable';
  5. function getNoneOption() {
  6. return { text: 'None', value: '', isNone: true };
  7. }
  8. export class QueryVariable implements Variable {
  9. datasource: any;
  10. query: any;
  11. regex: any;
  12. sort: any;
  13. options: any;
  14. current: any;
  15. refresh: number;
  16. hide: number;
  17. name: string;
  18. multi: boolean;
  19. includeAll: boolean;
  20. useTags: boolean;
  21. tagsQuery: string;
  22. tagValuesQuery: string;
  23. tags: any[];
  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. };
  44. /** @ngInject **/
  45. constructor(private model, private datasourceSrv, private templateSrv, private variableSrv, private timeSrv) {
  46. // copy model properties to this instance
  47. assignModelProperties(this, model, this.defaults);
  48. }
  49. getSaveModel() {
  50. // copy back model properties to model
  51. assignModelProperties(this.model, this, this.defaults);
  52. // remove options
  53. if (this.refresh !== 0) {
  54. this.model.options = [];
  55. }
  56. return this.model;
  57. }
  58. setValue(option) {
  59. return this.variableSrv.setOptionAsCurrent(this, option);
  60. }
  61. setValueFromUrl(urlValue) {
  62. return this.variableSrv.setOptionFromUrl(this, urlValue);
  63. }
  64. getValueForUrl() {
  65. if (this.current.text === 'All') {
  66. return 'All';
  67. }
  68. return this.current.value;
  69. }
  70. updateOptions() {
  71. return this.datasourceSrv.get(this.datasource)
  72. .then(this.updateOptionsFromMetricFindQuery.bind(this))
  73. .then(this.updateTags.bind(this))
  74. .then(this.variableSrv.validateVariableSelectionState.bind(this.variableSrv, this));
  75. }
  76. updateTags(datasource) {
  77. if (this.useTags) {
  78. return this.metricFindQuery(datasource, this.tagsQuery).then(results => {
  79. this.tags = [];
  80. for (var i = 0; i < results.length; i++) {
  81. this.tags.push(results[i].text);
  82. }
  83. return datasource;
  84. });
  85. } else {
  86. delete this.tags;
  87. }
  88. return datasource;
  89. }
  90. getValuesForTag(tagKey) {
  91. return this.datasourceSrv.get(this.datasource).then(datasource => {
  92. var query = this.tagValuesQuery.replace('$tag', tagKey);
  93. return this.metricFindQuery(datasource, query).then(function (results) {
  94. return _.map(results, function(value) {
  95. return value.text;
  96. });
  97. });
  98. });
  99. }
  100. updateOptionsFromMetricFindQuery(datasource) {
  101. return this.metricFindQuery(datasource, this.query).then(results => {
  102. this.options = this.metricNamesToVariableValues(results);
  103. if (this.includeAll) {
  104. this.addAllOption();
  105. }
  106. if (!this.options.length) {
  107. this.options.push(getNoneOption());
  108. }
  109. return datasource;
  110. });
  111. }
  112. metricFindQuery(datasource, query) {
  113. var options = {range: undefined, variable: this};
  114. if (this.refresh === 2) {
  115. options.range = this.timeSrv.timeRange();
  116. }
  117. return datasource.metricFindQuery(query, options);
  118. }
  119. addAllOption() {
  120. this.options.unshift({text: 'All', value: "$__all"});
  121. }
  122. metricNamesToVariableValues(metricNames) {
  123. var regex, options, i, matches;
  124. options = [];
  125. if (this.regex) {
  126. regex = kbn.stringToJsRegex(this.templateSrv.replace(this.regex, {}, 'regex'));
  127. }
  128. for (i = 0; i < metricNames.length; i++) {
  129. var item = metricNames[i];
  130. var text = item.text === undefined || item.text === null
  131. ? item.value
  132. : item.text;
  133. var value = item.value === undefined || item.value === null
  134. ? item.text
  135. : 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) { continue; }
  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. }
  173. if (reverseSort) {
  174. options = options.reverse();
  175. }
  176. return options;
  177. }
  178. dependsOn(variable) {
  179. return containsVariable(this.query, this.datasource, variable.name);
  180. }
  181. }
  182. variableTypes['query'] = {
  183. name: 'Query',
  184. ctor: QueryVariable,
  185. description: 'Variable values are fetched from a datasource query',
  186. supportsMulti: true,
  187. };