query_ctrl.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. if (aggs.length !== 0) {
  40. this.aggregators = aggs;
  41. }
  42. });
  43. this.datasource.getFilterTypes().then((filterTypes) => {
  44. if (filterTypes.length !== 0) {
  45. this.filterTypes = filterTypes;
  46. }
  47. });
  48. // needs to be defined here as it is called from typeahead
  49. this.suggestMetrics = (query, callback) => {
  50. this.datasource.metricFindQuery('metrics(' + query + ')')
  51. .then(this.getTextValues)
  52. .then(callback);
  53. };
  54. this.suggestTagKeys = (query, callback) => {
  55. this.datasource.suggestTagKeys(this.target.metric).then(callback);
  56. };
  57. this.suggestTagValues = (query, callback) => {
  58. this.datasource.metricFindQuery('suggest_tagv(' + query + ')')
  59. .then(this.getTextValues)
  60. .then(callback);
  61. };
  62. }
  63. targetBlur() {
  64. this.errors = this.validateTarget();
  65. this.refresh();
  66. }
  67. getTextValues(metricFindResult) {
  68. return _.map(metricFindResult, function(value) { return value.text; });
  69. }
  70. addTag() {
  71. if (this.target.filters && this.target.filters.length > 0) {
  72. this.errors.tags = "Please remove filters to use tags, tags and filters are mutually exclusive.";
  73. }
  74. if (!this.addTagMode) {
  75. this.addTagMode = true;
  76. return;
  77. }
  78. if (!this.target.tags) {
  79. this.target.tags = {};
  80. }
  81. this.errors = this.validateTarget();
  82. if (!this.errors.tags) {
  83. this.target.tags[this.target.currentTagKey] = this.target.currentTagValue;
  84. this.target.currentTagKey = '';
  85. this.target.currentTagValue = '';
  86. this.targetBlur();
  87. }
  88. this.addTagMode = false;
  89. }
  90. removeTag(key) {
  91. delete this.target.tags[key];
  92. this.targetBlur();
  93. }
  94. editTag(key, value) {
  95. this.removeTag(key);
  96. this.target.currentTagKey = key;
  97. this.target.currentTagValue = value;
  98. this.addTag();
  99. }
  100. closeAddTagMode() {
  101. this.addTagMode = false;
  102. return;
  103. }
  104. addFilter() {
  105. if (this.target.tags && _.size(this.target.tags) > 0) {
  106. this.errors.filters = "Please remove tags to use filters, tags and filters are mutually exclusive.";
  107. }
  108. if (!this.addFilterMode) {
  109. this.addFilterMode = true;
  110. return;
  111. }
  112. if (!this.target.filters) {
  113. this.target.filters = [];
  114. }
  115. if (!this.target.currentFilterType) {
  116. this.target.currentFilterType = 'iliteral_or';
  117. }
  118. if (!this.target.currentFilterGroupBy) {
  119. this.target.currentFilterGroupBy = false;
  120. }
  121. this.errors = this.validateTarget();
  122. if (!this.errors.filters) {
  123. var currentFilter = {
  124. type: this.target.currentFilterType,
  125. tagk: this.target.currentFilterKey,
  126. filter: this.target.currentFilterValue,
  127. groupBy: this.target.currentFilterGroupBy
  128. };
  129. this.target.filters.push(currentFilter);
  130. this.target.currentFilterType = 'literal_or';
  131. this.target.currentFilterKey = '';
  132. this.target.currentFilterValue = '';
  133. this.target.currentFilterGroupBy = false;
  134. this.targetBlur();
  135. }
  136. this.addFilterMode = false;
  137. }
  138. removeFilter(index) {
  139. this.target.filters.splice(index, 1);
  140. this.targetBlur();
  141. }
  142. editFilter(fil, index) {
  143. this.removeFilter(index);
  144. this.target.currentFilterKey = fil.tagk;
  145. this.target.currentFilterValue = fil.filter;
  146. this.target.currentFilterType = fil.type;
  147. this.target.currentFilterGroupBy = fil.groupBy;
  148. this.addFilter();
  149. }
  150. closeAddFilterMode() {
  151. this.addFilterMode = false;
  152. return;
  153. }
  154. validateTarget() {
  155. var errs: any = {};
  156. if (this.target.shouldDownsample) {
  157. try {
  158. if (this.target.downsampleInterval) {
  159. kbn.describe_interval(this.target.downsampleInterval);
  160. } else {
  161. errs.downsampleInterval = "You must supply a downsample interval (e.g. '1m' or '1h').";
  162. }
  163. } catch (err) {
  164. errs.downsampleInterval = err.message;
  165. }
  166. }
  167. if (this.target.tags && _.has(this.target.tags, this.target.currentTagKey)) {
  168. errs.tags = "Duplicate tag key '" + this.target.currentTagKey + "'.";
  169. }
  170. return errs;
  171. }
  172. }