From f7752efdacacf9792476299930cc6cc97c001752 Mon Sep 17 00:00:00 2001 From: Russ Cam Date: Wed, 9 Aug 2017 23:47:53 +1000 Subject: [PATCH] Add AggregationsHelper for SerialDifferencing Use lag in the usage test that return values --- src/Nest/Aggregations/AggregationsHelper.cs | 3 +- ...gAverageHoltLinearAggregationUsageTests.cs | 6 +-- ...AverageHoltWintersAggregationUsageTests.cs | 43 ++++++++++++++++--- ...SerialDifferencingAggregationUsageTests.cs | 31 ++++++++++--- 4 files changed, 65 insertions(+), 18 deletions(-) diff --git a/src/Nest/Aggregations/AggregationsHelper.cs b/src/Nest/Aggregations/AggregationsHelper.cs index 3d6b14d52ae..1edf640abd4 100644 --- a/src/Nest/Aggregations/AggregationsHelper.cs +++ b/src/Nest/Aggregations/AggregationsHelper.cs @@ -46,6 +46,8 @@ public AggregationsHelper(IReadOnlyDictionary aggregations) public ValueAggregate BucketScript(string key) => this.TryGet(key); + public ValueAggregate SerialDifferencing(string key) => this.TryGet(key); + public KeyedValueAggregate MaxBucket(string key) => this.TryGet(key); public KeyedValueAggregate MinBucket(string key) => this.TryGet(key); @@ -153,7 +155,6 @@ public TermsAggregate Terms(string key) public MatrixStatsAggregate MatrixStats(string key) => this.TryGet(key); - private TAggregate TryGet(string key) where TAggregate : class, IAggregate { diff --git a/src/Tests/Aggregations/Pipeline/MovingAverage/MovingAverageHoltLinearAggregationUsageTests.cs b/src/Tests/Aggregations/Pipeline/MovingAverage/MovingAverageHoltLinearAggregationUsageTests.cs index bf8f102b760..120ca54cd2f 100644 --- a/src/Tests/Aggregations/Pipeline/MovingAverage/MovingAverageHoltLinearAggregationUsageTests.cs +++ b/src/Tests/Aggregations/Pipeline/MovingAverage/MovingAverageHoltLinearAggregationUsageTests.cs @@ -24,7 +24,7 @@ public MovingAverageHoltLinearAggregationUsageTests(ReadOnlyCluster cluster, End date_histogram = new { field = "startedOn", - interval = "month", + interval = "month" }, aggs = new { @@ -44,7 +44,7 @@ public MovingAverageHoltLinearAggregationUsageTests(ReadOnlyCluster cluster, End settings = new { alpha = 0.5, - beta = 0.5, + beta = 0.5 } } } @@ -90,7 +90,7 @@ public MovingAverageHoltLinearAggregationUsageTests(ReadOnlyCluster cluster, End Model = new HoltLinearModel { Alpha = 0.5f, - Beta = 0.5f, + Beta = 0.5f } } } diff --git a/src/Tests/Aggregations/Pipeline/MovingAverage/MovingAverageHoltWintersAggregationUsageTests.cs b/src/Tests/Aggregations/Pipeline/MovingAverage/MovingAverageHoltWintersAggregationUsageTests.cs index 379dfb35ec4..764ece50ea5 100644 --- a/src/Tests/Aggregations/Pipeline/MovingAverage/MovingAverageHoltWintersAggregationUsageTests.cs +++ b/src/Tests/Aggregations/Pipeline/MovingAverage/MovingAverageHoltWintersAggregationUsageTests.cs @@ -23,7 +23,7 @@ public MovingAverageHoltWintersUsageTests(ReadOnlyCluster cluster, EndpointUsage date_histogram = new { field = "startedOn", - interval = "month", + interval = "month" }, aggs = new { @@ -39,7 +39,7 @@ public MovingAverageHoltWintersUsageTests(ReadOnlyCluster cluster, EndpointUsage moving_avg = new { buckets_path = "commits", - window = 60, + window = 4, model = "holt_winters", settings = new { @@ -47,7 +47,7 @@ public MovingAverageHoltWintersUsageTests(ReadOnlyCluster cluster, EndpointUsage alpha = 0.5, beta = 0.5, gamma = 0.5, - period = 30, + period = 2, pad = false } } @@ -69,14 +69,14 @@ public MovingAverageHoltWintersUsageTests(ReadOnlyCluster cluster, EndpointUsage ) .MovingAverage("commits_moving_avg", mv => mv .BucketsPath("commits") - .Window(60) + .Window(4) .Model(m => m .HoltWinters(hw => hw .Type(HoltWintersType.Multiplicative) .Alpha(0.5f) .Beta(0.5f) .Gamma(0.5f) - .Period(30) + .Period(2) .Pad(false) ) ) @@ -96,14 +96,14 @@ public MovingAverageHoltWintersUsageTests(ReadOnlyCluster cluster, EndpointUsage new SumAggregation("commits", "numberOfCommits") && new MovingAverageAggregation("commits_moving_avg", "commits") { - Window = 60, + Window = 4, Model = new HoltWintersModel { Type = HoltWintersType.Multiplicative, Alpha = 0.5f, Beta = 0.5f, Gamma = 0.5f, - Period = 30, + Period = 2, Pad = false } } @@ -113,6 +113,35 @@ public MovingAverageHoltWintersUsageTests(ReadOnlyCluster cluster, EndpointUsage protected override void ExpectResponse(ISearchResponse response) { response.ShouldBeValid(); + + var projectsPerMonth = response.Aggs.DateHistogram("projects_started_per_month"); + projectsPerMonth.Should().NotBeNull(); + projectsPerMonth.Buckets.Should().NotBeNull(); + projectsPerMonth.Buckets.Count.Should().BeGreaterThan(0); + + int bucketCount = 0; + foreach (var item in projectsPerMonth.Buckets) + { + bucketCount++; + + var commits = item.Sum("commits"); + commits.Should().NotBeNull(); + commits.Value.Should().BeGreaterThan(0); + + var movingAverage = item.MovingAverage("commits_moving_avg"); + + // Moving Average specifies a window of 4 so + // moving average values should exist from 5th bucketr onwards + if (bucketCount <= 4) + { + movingAverage.Should().BeNull(); + } + else + { + movingAverage.Should().NotBeNull(); + movingAverage.Value.Should().BeGreaterThan(0); + } + } } } } diff --git a/src/Tests/Aggregations/Pipeline/SerialDifferencing/SerialDifferencingAggregationUsageTests.cs b/src/Tests/Aggregations/Pipeline/SerialDifferencing/SerialDifferencingAggregationUsageTests.cs index 06628c935d0..a41ed92356d 100644 --- a/src/Tests/Aggregations/Pipeline/SerialDifferencing/SerialDifferencingAggregationUsageTests.cs +++ b/src/Tests/Aggregations/Pipeline/SerialDifferencing/SerialDifferencingAggregationUsageTests.cs @@ -22,7 +22,7 @@ public SerialDifferencingAggregationUsageTests(ReadOnlyCluster cluster, Endpoint date_histogram = new { field = "startedOn", - interval = "month", + interval = "month" }, aggs = new { @@ -33,12 +33,12 @@ public SerialDifferencingAggregationUsageTests(ReadOnlyCluster cluster, Endpoint field = "numberOfCommits" } }, - thirtieth_difference = new + second_difference = new { serial_diff = new { buckets_path = "commits", - lag = 30 + lag = 2 } } } @@ -56,9 +56,9 @@ public SerialDifferencingAggregationUsageTests(ReadOnlyCluster cluster, Endpoint .Sum("commits", sm => sm .Field(p => p.NumberOfCommits) ) - .SerialDifferencing("thirtieth_difference", d => d + .SerialDifferencing("second_difference", d => d .BucketsPath("commits") - .Lag(30) + .Lag(2) ) ) ) @@ -73,9 +73,9 @@ public SerialDifferencingAggregationUsageTests(ReadOnlyCluster cluster, Endpoint Interval = DateInterval.Month, Aggregations = new SumAggregation("commits", "numberOfCommits") && - new SerialDifferencingAggregation("thirtieth_difference", "commits") + new SerialDifferencingAggregation("second_difference", "commits") { - Lag = 30 + Lag = 2 } } }; @@ -89,11 +89,28 @@ protected override void ExpectResponse(ISearchResponse response) projectsPerMonth.Buckets.Should().NotBeNull(); projectsPerMonth.Buckets.Count.Should().BeGreaterThan(0); + var differenceCount = 0; + foreach (var item in projectsPerMonth.Buckets) { + differenceCount++; var commits = item.Sum("commits"); commits.Should().NotBeNull(); commits.Value.Should().NotBe(null); + + var secondDifference = item.SerialDifferencing("second_difference"); + + // serial differencing specified a lag of 2, so + // only expect values from the 3rd bucket onwards + if (differenceCount <= 2) + { + secondDifference.Should().BeNull(); + } + else + { + secondDifference.Should().NotBeNull(); + secondDifference.Value.Should().NotBe(null); + } } } }