query_variable.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. import {VariableSrv} from './variable_srv';
  6. function getNoneOption() {
  7. return { text: 'None', value: '', isNone: true };
  8. }
  9. export class QueryVariable implements Variable {
  10. datasource: any;
  11. query: any;
  12. regex: any;
  13. sort: any;
  14. options: any;
  15. current: any;
  16. refresh: number;
  17. hide: number;
  18. name: string;
  19. multi: boolean;
  20. includeAll: boolean;
  21. useTags: boolean;
  22. tagsQuery: string;
  23. tagValuesQuery: string;
  24. tags: any[];
  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. };
  45. /** @ngInject **/
  46. constructor(private model, private datasourceSrv, private templateSrv, private variableSrv, private $q, 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.get(this.datasource)
  73. .then(this.updateOptionsFromMetricFindQuery.bind(this))
  74. .then(this.updateTags.bind(this))
  75. .then(this.variableSrv.validateVariableSelectionState.bind(this.variableSrv, this));
  76. }
  77. updateTags(datasource) {
  78. if (this.useTags) {
  79. return this.metricFindQuery(datasource, this.tagsQuery).then(results => {
  80. this.tags = [];
  81. for (var i = 0; i < results.length; i++) {
  82. this.tags.push(results[i].text);
  83. }
  84. return datasource;
  85. });
  86. } else {
  87. delete this.tags;
  88. }
  89. return datasource;
  90. }
  91. getValuesForTag(tagKey) {
  92. return this.datasourceSrv.get(this.datasource).then(datasource => {
  93. var query = this.tagValuesQuery.replace('$tag', tagKey);
  94. return this.metricFindQuery(datasource, query).then(function (results) {
  95. return _.map(results, function(value) {
  96. return value.text;
  97. });
  98. });
  99. });
  100. }
  101. updateOptionsFromMetricFindQuery(datasource) {
  102. return this.metricFindQuery(datasource, this.query).then(results => {
  103. this.options = this.metricNamesToVariableValues(results);
  104. if (this.includeAll) {
  105. this.addAllOption();
  106. }
  107. if (!this.options.length) {
  108. this.options.push(getNoneOption());
  109. }
  110. return datasource;
  111. });
  112. }
  113. metricFindQuery(datasource, query) {
  114. var options = {range: undefined};
  115. if (this.refresh === 2) {
  116. options.range = this.timeSrv.timeRange();
  117. }
  118. return datasource.metricFindQuery(query, options);
  119. }
  120. addAllOption() {
  121. this.options.unshift({text: 'All', value: "$__all"});
  122. }
  123. metricNamesToVariableValues(metricNames) {
  124. var regex, options, i, matches;
  125. options = [];
  126. if (this.regex) {
  127. regex = kbn.stringToJsRegex(this.templateSrv.replace(this.regex, {}, 'regex'));
  128. }
  129. for (i = 0; i < metricNames.length; i++) {
  130. var item = metricNames[i];
  131. var text = item.text === undefined || item.text === null
  132. ? item.value
  133. : item.text;
  134. var value = item.value === undefined || item.value === null
  135. ? item.text
  136. : item.value;
  137. if (_.isNumber(value)) {
  138. value = value.toString();
  139. }
  140. if (_.isNumber(text)) {
  141. text = text.toString();
  142. }
  143. if (regex) {
  144. matches = regex.exec(value);
  145. if (!matches) { continue; }
  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. var sortType = Math.ceil(sortOrder / 2);
  161. var reverseSort = (sortOrder % 2 === 0);
  162. if (sortType === 1) {
  163. options = _.sortBy(options, 'text');
  164. } else if (sortType === 2) {
  165. options = _.sortBy(options, (opt) => {
  166. var 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. }
  174. if (reverseSort) {
  175. options = options.reverse();
  176. }
  177. return options;
  178. }
  179. dependsOn(variable) {
  180. return containsVariable(this.query, this.datasource, variable.name);
  181. }
  182. }
  183. variableTypes['query'] = {
  184. name: 'Query',
  185. ctor: QueryVariable,
  186. description: 'Variable values are fetched from a datasource query',
  187. supportsMulti: true,
  188. };