query_ctrl.ts 5.2 KB

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