query_ctrl.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. this.datasource.getAggregators().then(function(aggs) {
  34. this.aggregators = aggs;
  35. });
  36. // needs to be defined here as it is called from typeahead
  37. this.suggestMetrics = (query, callback) => {
  38. this.datasource.metricFindQuery('metrics(' + query + ')')
  39. .then(this.getTextValues)
  40. .then(callback);
  41. };
  42. this.suggestTagKeys = (query, callback) => {
  43. this.datasource.suggestTagKeys(this.target.metric).then(callback);
  44. };
  45. this.suggestTagValues = (query, callback) => {
  46. this.datasource.metricFindQuery('suggest_tagv(' + query + ')')
  47. .then(this.getTextValues)
  48. .then(callback);
  49. };
  50. }
  51. targetBlur() {
  52. this.errors = this.validateTarget();
  53. this.refresh();
  54. }
  55. getTextValues(metricFindResult) {
  56. return _.map(metricFindResult, function(value) { return value.text; });
  57. }
  58. addTag() {
  59. if (!this.addTagMode) {
  60. this.addTagMode = true;
  61. return;
  62. }
  63. if (!this.target.tags) {
  64. this.target.tags = {};
  65. }
  66. this.errors = this.validateTarget();
  67. if (!this.errors.tags) {
  68. this.target.tags[this.target.currentTagKey] = this.target.currentTagValue;
  69. this.target.currentTagKey = '';
  70. this.target.currentTagValue = '';
  71. this.targetBlur();
  72. }
  73. this.addTagMode = false;
  74. }
  75. removeTag(key) {
  76. delete this.target.tags[key];
  77. this.targetBlur();
  78. }
  79. editTag(key, value) {
  80. this.removeTag(key);
  81. this.target.currentTagKey = key;
  82. this.target.currentTagValue = value;
  83. this.addTag();
  84. }
  85. validateTarget() {
  86. var errs: any = {};
  87. if (this.target.shouldDownsample) {
  88. try {
  89. if (this.target.downsampleInterval) {
  90. kbn.describe_interval(this.target.downsampleInterval);
  91. } else {
  92. errs.downsampleInterval = "You must supply a downsample interval (e.g. '1m' or '1h').";
  93. }
  94. } catch (err) {
  95. errs.downsampleInterval = err.message;
  96. }
  97. }
  98. if (this.target.tags && _.has(this.target.tags, this.target.currentTagKey)) {
  99. errs.tags = "Duplicate tag key '" + this.target.currentTagKey + "'.";
  100. }
  101. return errs;
  102. }
  103. }