Просмотр исходного кода

feat(elasticsearch): added pipleline aggregation derivative

Torkel Ödegaard 10 лет назад
Родитель
Сommit
b36f644628

+ 3 - 4
public/app/plugins/datasource/elasticsearch/metric_agg.js

@@ -37,14 +37,13 @@ function (angular, _, queryDef) {
       $scope.settingsLinkText = '';
       $scope.aggDef = _.findWhere($scope.metricAggTypes, {value: $scope.agg.type});
 
-      if (!$scope.agg.field) {
-        $scope.agg.field = 'select field';
-      }
-
       if (queryDef.isPipelineAgg($scope.agg)) {
         $scope.agg.pipelineAgg = $scope.agg.pipelineAgg || 'select metric';
         $scope.agg.field = $scope.agg.pipelineAgg;
         $scope.settingsLinkText = 'Options';
+        delete $scope.agg.field;
+      } else if (!$scope.agg.field) {
+        $scope.agg.field = 'select field';
       }
 
       switch($scope.agg.type) {

+ 6 - 3
public/app/plugins/datasource/elasticsearch/query_builder.js

@@ -1,6 +1,7 @@
 define([
+  './query_def'
 ],
-function () {
+function (queryDef) {
   'use strict';
 
   function ElasticQueryBuilder(options) {
@@ -170,9 +171,11 @@ function () {
       var aggField = {};
       var metricAgg = null;
 
-      if (metric.type === 'moving_avg') {
+      if (queryDef.isPipelineAgg(metric)) {
         if (metric.pipelineAgg && /^\d*$/.test(metric.pipelineAgg)) {
-          metricAgg = { buckets_path: metric.pipelineAgg };
+          metricAgg = {
+            buckets_path: metric.pipelineAgg,
+          };
         } else {
           continue;
         }

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

@@ -14,6 +14,7 @@ function (_) {
       {text: "Extended Stats",  value: 'extended_stats', requiresField: true},
       {text: "Percentiles",  value: 'percentiles', requiresField: true},
       {text: "Moving Average",  value: 'moving_avg', requiresField: false },
+      {text: "Derivative",  value: 'derivative', requiresField: false },
       {text: "Unique Count", value: "cardinality", requiresField: true},
       {text: "Raw Document", value: "raw_document", requiresField: false}
     ],
@@ -67,7 +68,7 @@ function (_) {
       {text: '1d', value: '1d'},
     ],
 
-    pipelineAggs: ['moving_avg'],
+    pipelineAggs: ['moving_avg', 'derivative'],
 
     isPipelineAgg: function(metric) {
       if (metric.type) {

+ 27 - 2
public/app/plugins/datasource/elasticsearch/specs/query_builder_specs.ts

@@ -193,13 +193,11 @@ describe('ElasticQueryBuilder', function() {
               {
                   id: '2',
                   type: 'moving_avg',
-                  field: '3',
                   pipelineAgg: '3'
               },
               {
                   id: '4',
                   type: 'moving_avg',
-                  field: '3',
                   pipelineAgg: 'Metric to apply moving average'
               }
           ],
@@ -215,4 +213,31 @@ describe('ElasticQueryBuilder', function() {
       expect(firstLevel.aggs["2"].moving_avg.buckets_path).to.be("3");
       expect(firstLevel.aggs["4"]).to.be(undefined);
   });
+
+  it('with derivative', function() {
+    var query = builder.build({
+      metrics: [
+        {
+          id: '3',
+          type: 'sum',
+          field: '@value'
+        },
+        {
+          id: '2',
+          type: 'derivative',
+          pipelineAgg: '3'
+        }
+      ],
+      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"].derivative).not.to.be(undefined);
+    expect(firstLevel.aggs["2"].derivative.buckets_path).to.be("3");
+  });
+
 });