|  | 
|  | 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.Runtime.Serialization; | 
|  | 6 | +using Elasticsearch.Net; | 
|  | 7 | +using Elasticsearch.Net.Utf8Json; | 
|  | 8 | + | 
|  | 9 | +namespace Nest | 
|  | 10 | +{ | 
|  | 11 | +	[InterfaceDataContract] | 
|  | 12 | +	[ReadAs(typeof(RateAggregation))] | 
|  | 13 | +	public interface IRateAggregation : IMetricAggregation | 
|  | 14 | +	{ | 
|  | 15 | +		/// <summary> | 
|  | 16 | +		/// The <see cref="DateInterval"/> to use as the rate unit. | 
|  | 17 | +		/// </summary> | 
|  | 18 | +		[DataMember(Name = "unit")] | 
|  | 19 | +		DateInterval? Unit { get; set; } | 
|  | 20 | + | 
|  | 21 | +		/// <summary> | 
|  | 22 | +		/// The mode to use in the rate calculation. By default, "sum" mode is used. | 
|  | 23 | +		/// </summary> | 
|  | 24 | +		/// <remarks> | 
|  | 25 | +		/// The mode may be either "sum", where the rate calculates the sum of field values, or | 
|  | 26 | +		/// "value_count", where the rate uses the number of values in the field. | 
|  | 27 | +		/// </remarks> | 
|  | 28 | +		[DataMember(Name = "mode")] | 
|  | 29 | +		RateMode? Mode { get; set; } | 
|  | 30 | +	} | 
|  | 31 | + | 
|  | 32 | +	public class RateAggregation : MetricAggregationBase, IRateAggregation | 
|  | 33 | +	{ | 
|  | 34 | +		public RateAggregation(string name) : base(name, null) { } | 
|  | 35 | +		public RateAggregation(string name, Field field) : base(name, field) { } | 
|  | 36 | + | 
|  | 37 | +		internal override void WrapInContainer(AggregationContainer c) => c.Rate = this; | 
|  | 38 | + | 
|  | 39 | +		/// <inheritdoc cref ="IRateAggregation.Unit"/> | 
|  | 40 | +		public DateInterval? Unit { get; set; } | 
|  | 41 | + | 
|  | 42 | +		/// <inheritdoc cref ="IRateAggregation.Mode"/> | 
|  | 43 | +		public RateMode? Mode { get; set; } | 
|  | 44 | +	} | 
|  | 45 | + | 
|  | 46 | +	public class RateAggregationDescriptor<T> | 
|  | 47 | +		: MetricAggregationDescriptorBase<RateAggregationDescriptor<T>, IRateAggregation, T>, IRateAggregation | 
|  | 48 | +		where T : class | 
|  | 49 | +	{ | 
|  | 50 | +		DateInterval? IRateAggregation.Unit { get; set; } | 
|  | 51 | + | 
|  | 52 | +		RateMode? IRateAggregation.Mode { get; set; } | 
|  | 53 | +		 | 
|  | 54 | +		/// <inheritdoc cref ="IRateAggregation.Unit"/> | 
|  | 55 | +		public RateAggregationDescriptor<T> Unit(DateInterval? dateInterval) => | 
|  | 56 | +			Assign(dateInterval, (a, v) => a.Unit = v); | 
|  | 57 | + | 
|  | 58 | +		/// <inheritdoc cref ="IRateAggregation.Mode"/> | 
|  | 59 | +		public RateAggregationDescriptor<T> Mode(RateMode? mode) => | 
|  | 60 | +			Assign(mode, (a, v) => a.Mode = v); | 
|  | 61 | +	} | 
|  | 62 | + | 
|  | 63 | +	[StringEnum] | 
|  | 64 | +	public enum RateMode | 
|  | 65 | +	{ | 
|  | 66 | +		/// <summary> | 
|  | 67 | +		/// Rate calculates the sum of field values. | 
|  | 68 | +		/// </summary> | 
|  | 69 | +		[EnumMember(Value = "sum")] | 
|  | 70 | +		Sum, | 
|  | 71 | + | 
|  | 72 | +		/// <summary> | 
|  | 73 | +		/// Rate uses the number of values in the field. | 
|  | 74 | +		/// </summary> | 
|  | 75 | +		[EnumMember(Value = "value_count")] | 
|  | 76 | +		ValueCount | 
|  | 77 | +	} | 
|  | 78 | +} | 
0 commit comments