Browse Source

move target extraction logic to query def

carl bergquist 10 năm trước cách đây
mục cha
commit
b4763290de

+ 13 - 1
public/app/plugins/datasource/elasticsearch/query_def.js

@@ -13,7 +13,7 @@ function (_) {
       {text: "Min",  value: 'min', requiresField: true},
       {text: "Extended Stats",  value: 'extended_stats', requiresField: true},
       {text: "Percentiles",  value: 'percentiles', requiresField: true},
-      {text: "Moving Avg",  value: 'moving_avg', requiresField: true, requiresBucketsPath: true},
+      {text: "Moving Avg",  value: 'moving_avg', requiresField: false, requiresBucketsPath: true},
       {text: "Unique Count", value: "cardinality", requiresField: true},
       {text: "Raw Document", value: "raw_document", requiresField: false}
     ],
@@ -67,6 +67,18 @@ function (_) {
       {text: '1d', value: '1d'},
     ],
 
+    getMovingAverageSourceOptions: function(targets) {
+      var self = this;
+      var result = [];
+      _.each(targets.metrics, function(metric) {
+        if (metric.type !== 'moving_avg') {
+          result.push({text: self.describeMetric(metric), value: metric.id });
+        }
+      });
+
+      return result;
+    },
+
     getOrderByOptions: function(target) {
       var self = this;
       var metricRefs = [];

+ 48 - 0
public/app/plugins/datasource/elasticsearch/specs/query_def_specs.ts

@@ -0,0 +1,48 @@
+///<amd-dependency path="../query_def" name="QueryDef" />
+///<amd-dependency path="test/specs/helpers" name="helpers" />
+
+import {describe, beforeEach, it, sinon, expect, angularMocks} from 'test/lib/common';
+
+declare var helpers: any;
+declare var QueryDef: any;
+
+describe('ElasticQueryDef', function() {
+
+  describe('with zero targets', function() {
+    var response = QueryDef.getMovingAverageSourceOptions([]);
+
+    it('should return zero', function() {
+      expect(response.length).to.be(0);
+    });
+  });
+
+  describe('with count and sum targets', function() {
+    var targets = {
+      metrics: [
+        { type: 'count', field: '@value' },
+        { type: 'sum', field: '@value' }
+      ]
+    };
+
+    var response = QueryDef.getMovingAverageSourceOptions(targets);
+
+    it('should return zero', function() {
+      expect(response.length).to.be(2);
+    });
+  });
+
+  describe('with count and moving average targets', function() {
+    var targets = {
+      metrics: [
+        { type: 'count', field: '@value' },
+        { type: 'moving_avg', field: '@value' }
+      ]
+    };
+
+    var response = QueryDef.getMovingAverageSourceOptions(targets);
+
+    it('should return zero', function() {
+      expect(response.length).to.be(1);
+    });
+  });
+});