query_variable.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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) {
  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 datasource.metricFindQuery(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 datasource.metricFindQuery(query).then(function (results) {
  95. return _.map(results, function(value) {
  96. return value.text;
  97. });
  98. });
  99. });
  100. }
  101. updateOptionsFromMetricFindQuery(datasource) {
  102. return datasource.metricFindQuery(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. addAllOption() {
  114. this.options.unshift({text: 'All', value: "$__all"});
  115. }
  116. metricNamesToVariableValues(metricNames) {
  117. var regex, options, i, matches;
  118. options = [];
  119. if (this.regex) {
  120. regex = kbn.stringToJsRegex(this.templateSrv.replace(this.regex, {}, 'regex'));
  121. }
  122. for (i = 0; i < metricNames.length; i++) {
  123. var item = metricNames[i];
  124. var text = item.text === undefined || item.text === null
  125. ? item.value
  126. : item.text;
  127. var value = item.value === undefined || item.value === null
  128. ? item.text
  129. : item.value;
  130. if (_.isNumber(value)) {
  131. value = value.toString();
  132. }
  133. if (_.isNumber(text)) {
  134. text = text.toString();
  135. }
  136. if (regex) {
  137. matches = regex.exec(value);
  138. if (!matches) { continue; }
  139. if (matches.length > 1) {
  140. value = matches[1];
  141. text = matches[1];
  142. }
  143. }
  144. options.push({text: text, value: value});
  145. }
  146. options = _.uniqBy(options, 'value');
  147. return this.sortVariableValues(options, this.sort);
  148. }
  149. sortVariableValues(options, sortOrder) {
  150. if (sortOrder === 0) {
  151. return options;
  152. }
  153. var sortType = Math.ceil(sortOrder / 2);
  154. var reverseSort = (sortOrder % 2 === 0);
  155. if (sortType === 1) {
  156. options = _.sortBy(options, 'text');
  157. } else if (sortType === 2) {
  158. options = _.sortBy(options, (opt) => {
  159. var matches = opt.text.match(/.*?(\d+).*/);
  160. if (!matches || matches.length < 2) {
  161. return -1;
  162. } else {
  163. return parseInt(matches[1], 10);
  164. }
  165. });
  166. }
  167. if (reverseSort) {
  168. options = options.reverse();
  169. }
  170. return options;
  171. }
  172. dependsOn(variable) {
  173. return containsVariable(this.query, this.datasource, variable.name);
  174. }
  175. }
  176. variableTypes['query'] = {
  177. name: 'Query',
  178. ctor: QueryVariable,
  179. description: 'Variable values are fetched from a datasource query',
  180. supportsMulti: true,
  181. };