Przeglądaj źródła

adds spec for query builder

carl bergquist 10 lat temu
rodzic
commit
0b285845d1

+ 1 - 1
public/app/plugins/datasource/elasticsearch/metric_agg.js

@@ -37,7 +37,7 @@ function (angular, _, queryDef) {
       $scope.settingsLinkText = '';
       $scope.aggDef = _.findWhere($scope.metricAggTypes, {value: $scope.agg.type});
 
-      if (!$scope.agg.field && $scope.agg.type !== 'moving_avg') {
+      if (!$scope.agg.field) {
         $scope.agg.field = 'select field';
       }
 

+ 5 - 1
public/app/plugins/datasource/elasticsearch/query_builder.js

@@ -171,7 +171,11 @@ function () {
       var metricAgg = null;
 
       if (metric.type === 'moving_avg') {
-        metricAgg = {buckets_path: "1"};
+        if (metric.mavgSource && /^\d*$/.test(metric.mavgSource)) {
+          metricAgg = { buckets_path: metric.mavgSource };
+        } else {
+          continue;
+        }
       } else {
         metricAgg = {field: metric.field};
       }

+ 43 - 4
public/app/plugins/datasource/elasticsearch/specs/query_builder_specs.ts

@@ -158,11 +158,16 @@ describe('ElasticQueryBuilder', function() {
   it('with moving average', function() {
     var query = builder.build({
       metrics: [
+        {
+          id: '3',
+          type: 'sum',
+          field: '@value'
+        },
         {
           id: '2',
           type: 'moving_avg',
-          field: '@value',
-          aggregation: 'sum'
+          field: '3',
+          mavgSource: '3'
         }
       ],
       bucketAggs: [
@@ -171,9 +176,43 @@ describe('ElasticQueryBuilder', function() {
     });
 
     var firstLevel = query.aggs["3"];
-    console.log(JSON.stringify(query));
+
     expect(firstLevel.aggs["2"]).not.to.be(undefined);
     expect(firstLevel.aggs["2"].moving_avg).not.to.be(undefined);
-    expect(firstLevel.aggs["2"].moving_avg.buckets_path).to.be("99");
+    expect(firstLevel.aggs["2"].moving_avg.buckets_path).to.be("3");
+  });
+
+  it('with broken moving average', function() {
+      var query = builder.build({
+          metrics: [
+              {
+                  id: '3',
+                  type: 'sum',
+                  field: '@value'
+              },
+              {
+                  id: '2',
+                  type: 'moving_avg',
+                  field: '3',
+                  mavgSource: '3'
+              },
+              {
+                  id: '4',
+                  type: 'moving_avg',
+                  field: '3',
+                  mavgSource: 'Metric to apply moving average'
+              }
+          ],
+          bucketAggs: [
+              { type: 'date_histogram', field: '@timestamp', id: '3' }
+          ],
+      });
+
+      var firstLevel = query.aggs["3"];
+
+      expect(firstLevel.aggs["2"]).not.to.be(undefined);
+      expect(firstLevel.aggs["2"].moving_avg).not.to.be(undefined);
+      expect(firstLevel.aggs["2"].moving_avg.buckets_path).to.be("3");
+      expect(firstLevel.aggs["4"]).to.be(undefined);
   });
 });