|
| 1 | +// Licensed to Elasticsearch B.V under one or more agreements. |
| 2 | +// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. |
| 3 | +// See the LICENSE file in the project root for more information |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Linq; |
| 7 | +using Elastic.Elasticsearch.Xunit.XunitPlumbing; |
| 8 | +using FluentAssertions; |
| 9 | +using Nest; |
| 10 | +using Tests.Core.Extensions; |
| 11 | +using Tests.Core.ManagedElasticsearch.Clusters; |
| 12 | +using Tests.Domain; |
| 13 | +using Tests.Framework.EndpointTests.TestState; |
| 14 | + |
| 15 | +namespace Tests.Aggregations.Pipeline.MovingPercentiles |
| 16 | +{ |
| 17 | + /** |
| 18 | + * Given an ordered series of percentiles, the Moving Percentile aggregation will slide a window across those |
| 19 | + * percentiles and allow the user to compute the cumulative percentile. |
| 20 | + * |
| 21 | + * This is conceptually very similar to the Moving Function pipeline aggregation, except it works on the percentiles sketches instead of the actual buckets values. |
| 22 | + * |
| 23 | + * NOTE: Available in Elasticsearch 7.9.0+ with at least basic license level |
| 24 | + */ |
| 25 | + [SkipVersion("<7.9.0", "introduced in 7.9.0+")] |
| 26 | + public class MovingPercentilesAggregationUsageTests : AggregationUsageTestBase |
| 27 | + { |
| 28 | + public MovingPercentilesAggregationUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { } |
| 29 | + |
| 30 | + protected override object AggregationJson => new |
| 31 | + { |
| 32 | + projects_started_per_month = new |
| 33 | + { |
| 34 | + date_histogram = new |
| 35 | + { |
| 36 | + field = "startedOn", |
| 37 | + calendar_interval = "month", |
| 38 | + min_doc_count = 0 |
| 39 | + }, |
| 40 | + aggs = new |
| 41 | + { |
| 42 | + percentiles = new |
| 43 | + { |
| 44 | + percentiles = new |
| 45 | + { |
| 46 | + field = "numberOfCommits" |
| 47 | + } |
| 48 | + }, |
| 49 | + moving_percentiles = new |
| 50 | + { |
| 51 | + moving_percentiles = new |
| 52 | + { |
| 53 | + buckets_path = "percentiles", |
| 54 | + window = 10, |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + }; |
| 60 | + |
| 61 | + protected override Func<AggregationContainerDescriptor<Project>, IAggregationContainer> FluentAggs => a => a |
| 62 | + .DateHistogram("projects_started_per_month", dh => dh |
| 63 | + .Field(p => p.StartedOn) |
| 64 | + .CalendarInterval(DateInterval.Month) |
| 65 | + .MinimumDocumentCount(0) |
| 66 | + .Aggregations(aa => aa |
| 67 | + .Percentiles("percentiles", sm => sm |
| 68 | + .Field(p => p.NumberOfCommits) |
| 69 | + ) |
| 70 | + .MovingPercentiles("moving_percentiles", mv => mv |
| 71 | + .BucketsPath("percentiles") |
| 72 | + .Window(10) |
| 73 | + ) |
| 74 | + ) |
| 75 | + ); |
| 76 | + |
| 77 | + protected override AggregationDictionary InitializerAggs => |
| 78 | + new DateHistogramAggregation("projects_started_per_month") |
| 79 | + { |
| 80 | + Field = "startedOn", |
| 81 | + CalendarInterval = DateInterval.Month, |
| 82 | + MinimumDocumentCount = 0, |
| 83 | + Aggregations = |
| 84 | + new PercentilesAggregation("percentiles", "numberOfCommits") |
| 85 | + && new MovingPercentilesAggregation("moving_percentiles", "percentiles") |
| 86 | + { |
| 87 | + Window = 10 |
| 88 | + } |
| 89 | + }; |
| 90 | + |
| 91 | + protected override void ExpectResponse(ISearchResponse<Project> response) |
| 92 | + { |
| 93 | + response.ShouldBeValid(); |
| 94 | + |
| 95 | + var projectsPerMonth = response.Aggregations.DateHistogram("projects_started_per_month"); |
| 96 | + projectsPerMonth.Should().NotBeNull(); |
| 97 | + projectsPerMonth.Buckets.Should().NotBeNull(); |
| 98 | + projectsPerMonth.Buckets.Count.Should().BeGreaterThan(0); |
| 99 | + |
| 100 | + // percentiles not calculated for the first bucket |
| 101 | + foreach (var item in projectsPerMonth.Buckets.Skip(1)) |
| 102 | + { |
| 103 | + var movingPercentiles = item.MovingPercentiles("moving_percentiles"); |
| 104 | + movingPercentiles.Should().NotBeNull(); |
| 105 | + movingPercentiles.Items.Should().NotBeNull(); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments