From b36f6446280504284bc7974e266e2350e0b9bf8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Thu, 10 Dec 2015 11:46:19 +0100 Subject: [PATCH] feat(elasticsearch): added pipleline aggregation derivative --- .../datasource/elasticsearch/metric_agg.js | 7 ++--- .../datasource/elasticsearch/query_builder.js | 9 ++++-- .../datasource/elasticsearch/query_def.js | 3 +- .../specs/query_builder_specs.ts | 29 +++++++++++++++++-- 4 files changed, 38 insertions(+), 10 deletions(-) diff --git a/public/app/plugins/datasource/elasticsearch/metric_agg.js b/public/app/plugins/datasource/elasticsearch/metric_agg.js index 37fe224b586..feaa4bac0b6 100644 --- a/public/app/plugins/datasource/elasticsearch/metric_agg.js +++ b/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) { diff --git a/public/app/plugins/datasource/elasticsearch/query_builder.js b/public/app/plugins/datasource/elasticsearch/query_builder.js index 64128943f40..c68827df853 100644 --- a/public/app/plugins/datasource/elasticsearch/query_builder.js +++ b/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; } diff --git a/public/app/plugins/datasource/elasticsearch/query_def.js b/public/app/plugins/datasource/elasticsearch/query_def.js index 6fed5580576..a7c30a7a710 100644 --- a/public/app/plugins/datasource/elasticsearch/query_def.js +++ b/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) { diff --git a/public/app/plugins/datasource/elasticsearch/specs/query_builder_specs.ts b/public/app/plugins/datasource/elasticsearch/specs/query_builder_specs.ts index 91cdba7248e..0be35b20a38 100644 --- a/public/app/plugins/datasource/elasticsearch/specs/query_builder_specs.ts +++ b/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"); + }); + });