Skip to content

Add AggregationsHelper for SerialDifferencing #2826

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Nest/Aggregations/AggregationsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public AggregationsHelper(IReadOnlyDictionary<string, IAggregate> aggregations)

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

public ValueAggregate SerialDifferencing(string key) => this.TryGet<ValueAggregate>(key);

public KeyedValueAggregate MaxBucket(string key) => this.TryGet<KeyedValueAggregate>(key);

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

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


private TAggregate TryGet<TAggregate>(string key)
where TAggregate : class, IAggregate
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public MovingAverageHoltLinearAggregationUsageTests(ReadOnlyCluster cluster, End
date_histogram = new
{
field = "startedOn",
interval = "month",
interval = "month"
},
aggs = new
{
Expand All @@ -44,7 +44,7 @@ public MovingAverageHoltLinearAggregationUsageTests(ReadOnlyCluster cluster, End
settings = new
{
alpha = 0.5,
beta = 0.5,
beta = 0.5
}
}
}
Expand Down Expand Up @@ -90,7 +90,7 @@ public MovingAverageHoltLinearAggregationUsageTests(ReadOnlyCluster cluster, End
Model = new HoltLinearModel
{
Alpha = 0.5f,
Beta = 0.5f,
Beta = 0.5f
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public MovingAverageHoltWintersUsageTests(ReadOnlyCluster cluster, EndpointUsage
date_histogram = new
{
field = "startedOn",
interval = "month",
interval = "month"
},
aggs = new
{
Expand All @@ -39,15 +39,15 @@ public MovingAverageHoltWintersUsageTests(ReadOnlyCluster cluster, EndpointUsage
moving_avg = new
{
buckets_path = "commits",
window = 60,
window = 4,
model = "holt_winters",
settings = new
{
type = "mult",
alpha = 0.5,
beta = 0.5,
gamma = 0.5,
period = 30,
period = 2,
pad = false
}
}
Expand All @@ -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)
)
)
Expand All @@ -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
}
}
Expand All @@ -113,6 +113,35 @@ public MovingAverageHoltWintersUsageTests(ReadOnlyCluster cluster, EndpointUsage
protected override void ExpectResponse(ISearchResponse<Project> 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);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public SerialDifferencingAggregationUsageTests(ReadOnlyCluster cluster, Endpoint
date_histogram = new
{
field = "startedOn",
interval = "month",
interval = "month"
},
aggs = new
{
Expand All @@ -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
}
}
}
Expand All @@ -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)
)
)
)
Expand All @@ -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
}
}
};
Expand All @@ -89,11 +89,28 @@ protected override void ExpectResponse(ISearchResponse<Project> 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);
}
}
}
}
Expand Down