query_ctrl.ts 3.2 KB

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