|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using Elastic.Xunit.XunitPlumbing; |
| 4 | +using FluentAssertions; |
| 5 | +using Nest; |
| 6 | +using Tests.Core.Extensions; |
| 7 | +using Tests.Core.ManagedElasticsearch.Clusters; |
| 8 | +using Tests.Domain; |
| 9 | +using Tests.Framework; |
| 10 | +using Tests.Framework.Integration; |
| 11 | + |
| 12 | +namespace Tests.Aggregations.Pipeline.MovingFunction |
| 13 | +{ |
| 14 | + public class MovingFunctionAggregationUsageTests : AggregationUsageTestBase |
| 15 | + { |
| 16 | + public MovingFunctionAggregationUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { } |
| 17 | + |
| 18 | + protected override object AggregationJson => new |
| 19 | + { |
| 20 | + projects_started_per_month = new |
| 21 | + { |
| 22 | + date_histogram = new |
| 23 | + { |
| 24 | + field = "startedOn", |
| 25 | + interval = "month", |
| 26 | + }, |
| 27 | + aggs = new |
| 28 | + { |
| 29 | + commits = new |
| 30 | + { |
| 31 | + sum = new |
| 32 | + { |
| 33 | + field = "numberOfCommits" |
| 34 | + } |
| 35 | + }, |
| 36 | + commits_moving_avg = new |
| 37 | + { |
| 38 | + moving_fn = new |
| 39 | + { |
| 40 | + buckets_path = "commits", |
| 41 | + window = 30, |
| 42 | + script = "MovingFunctions.unweightedAvg(values)" |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + }; |
| 48 | + |
| 49 | + protected override Func<AggregationContainerDescriptor<Project>, IAggregationContainer> FluentAggs => a => a |
| 50 | + .DateHistogram("projects_started_per_month", dh => dh |
| 51 | + .Field(p => p.StartedOn) |
| 52 | + .Interval(DateInterval.Month) |
| 53 | + .Aggregations(aa => aa |
| 54 | + .Sum("commits", sm => sm |
| 55 | + .Field(p => p.NumberOfCommits) |
| 56 | + ) |
| 57 | + .MovingFunction("commits_moving_avg", mv => mv |
| 58 | + .BucketsPath("commits") |
| 59 | + .Window(30) |
| 60 | + .Script("MovingFunctions.unweightedAvg(values)") |
| 61 | + ) |
| 62 | + ) |
| 63 | + ); |
| 64 | + |
| 65 | + protected override AggregationDictionary InitializerAggs => |
| 66 | + new DateHistogramAggregation("projects_started_per_month") |
| 67 | + { |
| 68 | + Field = "startedOn", |
| 69 | + Interval = DateInterval.Month, |
| 70 | + Aggregations = |
| 71 | + new SumAggregation("commits", "numberOfCommits") |
| 72 | + && new MovingFunctionAggregation("commits_moving_avg", "commits") |
| 73 | + { |
| 74 | + Window = 30, |
| 75 | + Script = "MovingFunctions.unweightedAvg(values)" |
| 76 | + } |
| 77 | + }; |
| 78 | + |
| 79 | + protected override void ExpectResponse(ISearchResponse<Project> response) |
| 80 | + { |
| 81 | + response.ShouldBeValid(); |
| 82 | + |
| 83 | + var projectsPerMonth = response.Aggregations.DateHistogram("projects_started_per_month"); |
| 84 | + projectsPerMonth.Should().NotBeNull(); |
| 85 | + projectsPerMonth.Buckets.Should().NotBeNull(); |
| 86 | + projectsPerMonth.Buckets.Count.Should().BeGreaterThan(0); |
| 87 | + |
| 88 | + // average not calculated for the first bucket |
| 89 | + foreach(var item in projectsPerMonth.Buckets.Skip(1)) |
| 90 | + { |
| 91 | + var movingAvg = item.Sum("commits_moving_avg"); |
| 92 | + movingAvg.Should().NotBeNull(); |
| 93 | + movingAvg.Value.Should().BeGreaterThan(0); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments