query_ctrl.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. aggregator: any;
  10. downsampleInterval: any;
  11. downsampleAggregator: any;
  12. downsampleFillPolicy: any;
  13. errors: any;
  14. suggestMetrics: any;
  15. suggestTagKeys: any;
  16. suggestTagValues: any;
  17. addTagMode: boolean;
  18. /** @ngInject **/
  19. constructor($scope, $injector) {
  20. super($scope, $injector);
  21. this.errors = this.validateTarget();
  22. this.aggregators = ['avg', 'sum', 'min', 'max', 'dev', 'zimsum', 'mimmin', 'mimmax'];
  23. this.fillPolicies = ['none', 'nan', 'null', 'zero'];
  24. if (!this.target.aggregator) {
  25. this.target.aggregator = 'sum';
  26. }
  27. if (!this.target.downsampleAggregator) {
  28. this.target.downsampleAggregator = 'avg';
  29. }
  30. if (!this.target.downsampleFillPolicy) {
  31. this.target.downsampleFillPolicy = 'none';
  32. }
  33. var self = this;
  34. this.datasource.getAggregators().then(function(aggs) {
  35. self.aggregators = aggs;
  36. });
  37. // needs to be defined here as it is called from typeahead
  38. this.suggestMetrics = (query, callback) => {
  39. this.datasource.metricFindQuery('metrics(' + query + ')')
  40. .then(this.getTextValues)
  41. .then(callback);
  42. };
  43. this.suggestTagKeys = (query, callback) => {
  44. this.datasource.suggestTagKeys(this.target.metric).then(callback);
  45. };
  46. this.suggestTagValues = (query, callback) => {
  47. this.datasource.metricFindQuery('suggest_tagv(' + query + ')')
  48. .then(this.getTextValues)
  49. .then(callback);
  50. };
  51. }
  52. targetBlur() {
  53. this.errors = this.validateTarget();
  54. this.refresh();
  55. }
  56. getTextValues(metricFindResult) {
  57. return _.map(metricFindResult, function(value) { return value.text; });
  58. }
  59. addTag() {
  60. if (!this.addTagMode) {
  61. this.addTagMode = true;
  62. return;
  63. }
  64. if (!this.target.tags) {
  65. this.target.tags = {};
  66. }
  67. this.errors = this.validateTarget();
  68. if (!this.errors.tags) {
  69. this.target.tags[this.target.currentTagKey] = this.target.currentTagValue;
  70. this.target.currentTagKey = '';
  71. this.target.currentTagValue = '';
  72. this.targetBlur();
  73. }
  74. this.addTagMode = false;
  75. }
  76. removeTag(key) {
  77. delete this.target.tags[key];
  78. this.targetBlur();
  79. }
  80. editTag(key, value) {
  81. this.removeTag(key);
  82. this.target.currentTagKey = key;
  83. this.target.currentTagValue = value;
  84. this.addTag();
  85. }
  86. validateTarget() {
  87. var errs: any = {};
  88. if (this.target.shouldDownsample) {
  89. try {
  90. if (this.target.downsampleInterval) {
  91. kbn.describe_interval(this.target.downsampleInterval);
  92. } else {
  93. errs.downsampleInterval = "You must supply a downsample interval (e.g. '1m' or '1h').";
  94. }
  95. } catch (err) {
  96. errs.downsampleInterval = err.message;
  97. }
  98. }
  99. if (this.target.tags && _.has(this.target.tags, this.target.currentTagKey)) {
  100. errs.tags = "Duplicate tag key '" + this.target.currentTagKey + "'.";
  101. }
  102. return errs;
  103. }
  104. }