query_ctrl.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import _ from 'lodash';
  2. import kbn from 'app/core/utils/kbn';
  3. import { QueryCtrl } from 'app/plugins/sdk';
  4. export class OpenTsQueryCtrl extends QueryCtrl {
  5. static templateUrl = 'partials/query.editor.html';
  6. aggregators: any;
  7. fillPolicies: any;
  8. filterTypes: any;
  9. tsdbVersion: any;
  10. aggregator: any;
  11. downsampleInterval: any;
  12. downsampleAggregator: any;
  13. downsampleFillPolicy: any;
  14. errors: any;
  15. suggestMetrics: any;
  16. suggestTagKeys: any;
  17. suggestTagValues: any;
  18. addTagMode: boolean;
  19. addFilterMode: boolean;
  20. /** @ngInject */
  21. constructor($scope, $injector) {
  22. super($scope, $injector);
  23. this.errors = this.validateTarget();
  24. this.aggregators = ['avg', 'sum', 'min', 'max', 'dev', 'zimsum', 'mimmin', 'mimmax'];
  25. this.fillPolicies = ['none', 'nan', 'null', 'zero'];
  26. this.filterTypes = [
  27. 'wildcard',
  28. 'iliteral_or',
  29. 'not_iliteral_or',
  30. 'not_literal_or',
  31. 'iwildcard',
  32. 'literal_or',
  33. 'regexp',
  34. ];
  35. this.tsdbVersion = this.datasource.tsdbVersion;
  36. if (!this.target.aggregator) {
  37. this.target.aggregator = 'sum';
  38. }
  39. if (!this.target.downsampleAggregator) {
  40. this.target.downsampleAggregator = 'avg';
  41. }
  42. if (!this.target.downsampleFillPolicy) {
  43. this.target.downsampleFillPolicy = 'none';
  44. }
  45. this.datasource.getAggregators().then(aggs => {
  46. if (aggs.length !== 0) {
  47. this.aggregators = aggs;
  48. }
  49. });
  50. this.datasource.getFilterTypes().then(filterTypes => {
  51. if (filterTypes.length !== 0) {
  52. this.filterTypes = filterTypes;
  53. }
  54. });
  55. // needs to be defined here as it is called from typeahead
  56. this.suggestMetrics = (query, callback) => {
  57. this.datasource
  58. .metricFindQuery('metrics(' + query + ')')
  59. .then(this.getTextValues)
  60. .then(callback);
  61. };
  62. this.suggestTagKeys = (query, callback) => {
  63. this.datasource.suggestTagKeys(this.target.metric).then(callback);
  64. };
  65. this.suggestTagValues = (query, callback) => {
  66. this.datasource
  67. .metricFindQuery('suggest_tagv(' + query + ')')
  68. .then(this.getTextValues)
  69. .then(callback);
  70. };
  71. }
  72. targetBlur() {
  73. this.errors = this.validateTarget();
  74. this.refresh();
  75. }
  76. getTextValues(metricFindResult) {
  77. return _.map(metricFindResult, value => {
  78. return value.text;
  79. });
  80. }
  81. addTag() {
  82. if (this.target.filters && this.target.filters.length > 0) {
  83. this.errors.tags = 'Please remove filters to use tags, tags and filters are mutually exclusive.';
  84. }
  85. if (!this.addTagMode) {
  86. this.addTagMode = true;
  87. return;
  88. }
  89. if (!this.target.tags) {
  90. this.target.tags = {};
  91. }
  92. this.errors = this.validateTarget();
  93. if (!this.errors.tags) {
  94. this.target.tags[this.target.currentTagKey] = this.target.currentTagValue;
  95. this.target.currentTagKey = '';
  96. this.target.currentTagValue = '';
  97. this.targetBlur();
  98. }
  99. this.addTagMode = false;
  100. }
  101. removeTag(key) {
  102. delete this.target.tags[key];
  103. this.targetBlur();
  104. }
  105. editTag(key, value) {
  106. this.removeTag(key);
  107. this.target.currentTagKey = key;
  108. this.target.currentTagValue = value;
  109. this.addTag();
  110. }
  111. closeAddTagMode() {
  112. this.addTagMode = false;
  113. return;
  114. }
  115. addFilter() {
  116. if (this.target.tags && _.size(this.target.tags) > 0) {
  117. this.errors.filters = 'Please remove tags to use filters, tags and filters are mutually exclusive.';
  118. }
  119. if (!this.addFilterMode) {
  120. this.addFilterMode = true;
  121. return;
  122. }
  123. if (!this.target.filters) {
  124. this.target.filters = [];
  125. }
  126. if (!this.target.currentFilterType) {
  127. this.target.currentFilterType = 'iliteral_or';
  128. }
  129. if (!this.target.currentFilterGroupBy) {
  130. this.target.currentFilterGroupBy = false;
  131. }
  132. this.errors = this.validateTarget();
  133. if (!this.errors.filters) {
  134. const currentFilter = {
  135. type: this.target.currentFilterType,
  136. tagk: this.target.currentFilterKey,
  137. filter: this.target.currentFilterValue,
  138. groupBy: this.target.currentFilterGroupBy,
  139. };
  140. this.target.filters.push(currentFilter);
  141. this.target.currentFilterType = 'literal_or';
  142. this.target.currentFilterKey = '';
  143. this.target.currentFilterValue = '';
  144. this.target.currentFilterGroupBy = false;
  145. this.targetBlur();
  146. }
  147. this.addFilterMode = false;
  148. }
  149. removeFilter(index) {
  150. this.target.filters.splice(index, 1);
  151. this.targetBlur();
  152. }
  153. editFilter(fil, index) {
  154. this.removeFilter(index);
  155. this.target.currentFilterKey = fil.tagk;
  156. this.target.currentFilterValue = fil.filter;
  157. this.target.currentFilterType = fil.type;
  158. this.target.currentFilterGroupBy = fil.groupBy;
  159. this.addFilter();
  160. }
  161. closeAddFilterMode() {
  162. this.addFilterMode = false;
  163. return;
  164. }
  165. validateTarget() {
  166. const errs: any = {};
  167. if (this.target.shouldDownsample) {
  168. try {
  169. if (this.target.downsampleInterval) {
  170. kbn.describe_interval(this.target.downsampleInterval);
  171. } else {
  172. errs.downsampleInterval = "You must supply a downsample interval (e.g. '1m' or '1h').";
  173. }
  174. } catch (err) {
  175. errs.downsampleInterval = err.message;
  176. }
  177. }
  178. if (this.target.tags && _.has(this.target.tags, this.target.currentTagKey)) {
  179. errs.tags = "Duplicate tag key '" + this.target.currentTagKey + "'.";
  180. }
  181. return errs;
  182. }
  183. }