Skip to content

Commit 56c01a6

Browse files
russcamMpdreamz
authored andcommitted
Add AggregationsHelper for SerialDifferencing (#2826)
Use lag in the usage test that return values
1 parent 0ed0a41 commit 56c01a6

File tree

4 files changed

+65
-18
lines changed

4 files changed

+65
-18
lines changed

src/Nest/Aggregations/AggregationsHelper.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ public AggregationsHelper(IReadOnlyDictionary<string, IAggregate> aggregations)
4646

4747
public ValueAggregate BucketScript(string key) => this.TryGet<ValueAggregate>(key);
4848

49+
public ValueAggregate SerialDifferencing(string key) => this.TryGet<ValueAggregate>(key);
50+
4951
public KeyedValueAggregate MaxBucket(string key) => this.TryGet<KeyedValueAggregate>(key);
5052

5153
public KeyedValueAggregate MinBucket(string key) => this.TryGet<KeyedValueAggregate>(key);
@@ -153,7 +155,6 @@ public TermsAggregate<TKey> Terms<TKey>(string key)
153155

154156
public MatrixStatsAggregate MatrixStats(string key) => this.TryGet<MatrixStatsAggregate>(key);
155157

156-
157158
private TAggregate TryGet<TAggregate>(string key)
158159
where TAggregate : class, IAggregate
159160
{

src/Tests/Aggregations/Pipeline/MovingAverage/MovingAverageHoltLinearAggregationUsageTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public MovingAverageHoltLinearAggregationUsageTests(ReadOnlyCluster cluster, End
2424
date_histogram = new
2525
{
2626
field = "startedOn",
27-
interval = "month",
27+
interval = "month"
2828
},
2929
aggs = new
3030
{
@@ -44,7 +44,7 @@ public MovingAverageHoltLinearAggregationUsageTests(ReadOnlyCluster cluster, End
4444
settings = new
4545
{
4646
alpha = 0.5,
47-
beta = 0.5,
47+
beta = 0.5
4848
}
4949
}
5050
}
@@ -90,7 +90,7 @@ public MovingAverageHoltLinearAggregationUsageTests(ReadOnlyCluster cluster, End
9090
Model = new HoltLinearModel
9191
{
9292
Alpha = 0.5f,
93-
Beta = 0.5f,
93+
Beta = 0.5f
9494
}
9595
}
9696
}

src/Tests/Aggregations/Pipeline/MovingAverage/MovingAverageHoltWintersAggregationUsageTests.cs

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public MovingAverageHoltWintersUsageTests(ReadOnlyCluster cluster, EndpointUsage
2323
date_histogram = new
2424
{
2525
field = "startedOn",
26-
interval = "month",
26+
interval = "month"
2727
},
2828
aggs = new
2929
{
@@ -39,15 +39,15 @@ public MovingAverageHoltWintersUsageTests(ReadOnlyCluster cluster, EndpointUsage
3939
moving_avg = new
4040
{
4141
buckets_path = "commits",
42-
window = 60,
42+
window = 4,
4343
model = "holt_winters",
4444
settings = new
4545
{
4646
type = "mult",
4747
alpha = 0.5,
4848
beta = 0.5,
4949
gamma = 0.5,
50-
period = 30,
50+
period = 2,
5151
pad = false
5252
}
5353
}
@@ -69,14 +69,14 @@ public MovingAverageHoltWintersUsageTests(ReadOnlyCluster cluster, EndpointUsage
6969
)
7070
.MovingAverage("commits_moving_avg", mv => mv
7171
.BucketsPath("commits")
72-
.Window(60)
72+
.Window(4)
7373
.Model(m => m
7474
.HoltWinters(hw => hw
7575
.Type(HoltWintersType.Multiplicative)
7676
.Alpha(0.5f)
7777
.Beta(0.5f)
7878
.Gamma(0.5f)
79-
.Period(30)
79+
.Period(2)
8080
.Pad(false)
8181
)
8282
)
@@ -96,14 +96,14 @@ public MovingAverageHoltWintersUsageTests(ReadOnlyCluster cluster, EndpointUsage
9696
new SumAggregation("commits", "numberOfCommits") &&
9797
new MovingAverageAggregation("commits_moving_avg", "commits")
9898
{
99-
Window = 60,
99+
Window = 4,
100100
Model = new HoltWintersModel
101101
{
102102
Type = HoltWintersType.Multiplicative,
103103
Alpha = 0.5f,
104104
Beta = 0.5f,
105105
Gamma = 0.5f,
106-
Period = 30,
106+
Period = 2,
107107
Pad = false
108108
}
109109
}
@@ -113,6 +113,35 @@ public MovingAverageHoltWintersUsageTests(ReadOnlyCluster cluster, EndpointUsage
113113
protected override void ExpectResponse(ISearchResponse<Project> response)
114114
{
115115
response.ShouldBeValid();
116+
117+
var projectsPerMonth = response.Aggs.DateHistogram("projects_started_per_month");
118+
projectsPerMonth.Should().NotBeNull();
119+
projectsPerMonth.Buckets.Should().NotBeNull();
120+
projectsPerMonth.Buckets.Count.Should().BeGreaterThan(0);
121+
122+
int bucketCount = 0;
123+
foreach (var item in projectsPerMonth.Buckets)
124+
{
125+
bucketCount++;
126+
127+
var commits = item.Sum("commits");
128+
commits.Should().NotBeNull();
129+
commits.Value.Should().BeGreaterThan(0);
130+
131+
var movingAverage = item.MovingAverage("commits_moving_avg");
132+
133+
// Moving Average specifies a window of 4 so
134+
// moving average values should exist from 5th bucketr onwards
135+
if (bucketCount <= 4)
136+
{
137+
movingAverage.Should().BeNull();
138+
}
139+
else
140+
{
141+
movingAverage.Should().NotBeNull();
142+
movingAverage.Value.Should().BeGreaterThan(0);
143+
}
144+
}
116145
}
117146
}
118147
}

src/Tests/Aggregations/Pipeline/SerialDifferencing/SerialDifferencingAggregationUsageTests.cs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public SerialDifferencingAggregationUsageTests(ReadOnlyCluster cluster, Endpoint
2222
date_histogram = new
2323
{
2424
field = "startedOn",
25-
interval = "month",
25+
interval = "month"
2626
},
2727
aggs = new
2828
{
@@ -33,12 +33,12 @@ public SerialDifferencingAggregationUsageTests(ReadOnlyCluster cluster, Endpoint
3333
field = "numberOfCommits"
3434
}
3535
},
36-
thirtieth_difference = new
36+
second_difference = new
3737
{
3838
serial_diff = new
3939
{
4040
buckets_path = "commits",
41-
lag = 30
41+
lag = 2
4242
}
4343
}
4444
}
@@ -56,9 +56,9 @@ public SerialDifferencingAggregationUsageTests(ReadOnlyCluster cluster, Endpoint
5656
.Sum("commits", sm => sm
5757
.Field(p => p.NumberOfCommits)
5858
)
59-
.SerialDifferencing("thirtieth_difference", d => d
59+
.SerialDifferencing("second_difference", d => d
6060
.BucketsPath("commits")
61-
.Lag(30)
61+
.Lag(2)
6262
)
6363
)
6464
)
@@ -73,9 +73,9 @@ public SerialDifferencingAggregationUsageTests(ReadOnlyCluster cluster, Endpoint
7373
Interval = DateInterval.Month,
7474
Aggregations =
7575
new SumAggregation("commits", "numberOfCommits") &&
76-
new SerialDifferencingAggregation("thirtieth_difference", "commits")
76+
new SerialDifferencingAggregation("second_difference", "commits")
7777
{
78-
Lag = 30
78+
Lag = 2
7979
}
8080
}
8181
};
@@ -89,11 +89,28 @@ protected override void ExpectResponse(ISearchResponse<Project> response)
8989
projectsPerMonth.Buckets.Should().NotBeNull();
9090
projectsPerMonth.Buckets.Count.Should().BeGreaterThan(0);
9191

92+
var differenceCount = 0;
93+
9294
foreach (var item in projectsPerMonth.Buckets)
9395
{
96+
differenceCount++;
9497
var commits = item.Sum("commits");
9598
commits.Should().NotBeNull();
9699
commits.Value.Should().NotBe(null);
100+
101+
var secondDifference = item.SerialDifferencing("second_difference");
102+
103+
// serial differencing specified a lag of 2, so
104+
// only expect values from the 3rd bucket onwards
105+
if (differenceCount <= 2)
106+
{
107+
secondDifference.Should().BeNull();
108+
}
109+
else
110+
{
111+
secondDifference.Should().NotBeNull();
112+
secondDifference.Value.Should().NotBe(null);
113+
}
97114
}
98115
}
99116
}

0 commit comments

Comments
 (0)