using InnovEnergy.Lib.S3.Metadata; using InnovEnergy.Lib.S3.Records; using InnovEnergy.Lib.S3.Records.Operations; using InnovEnergy.Lib.Utils; namespace InnovEnergy.Lib.S3.Drivers.Internal.Util; public class Aggregator { public AggregationLevel AggregationLevel { get; } private Record[] Buffer { get; } private UInt32 Index { get; set; } public Aggregator(AggregationLevel thisLevel, AggregationLevel levelBelow, IEnumerable initialRecords) { var ratio = thisLevel.SamplePeriod / levelBelow.SamplePeriod; AggregationLevel = thisLevel; Index = 0; Buffer = new Record[ratio]; Clear(); foreach (var record in initialRecords) Aggregate(record); } public Record? Aggregate(Record r) { Buffer[Index++] = r; return IsFull ? ForceAggregation() : null; } public Record? ForceAggregation() { if (IsEmpty) return null; // nothing to aggregate var aggregated = Buffer.Aggregate(); Clear(); return aggregated; } private void Clear() { Buffer.Fill(Record.Empty); Index = 0; } private Boolean IsFull => Index == Buffer.Length; private Boolean IsEmpty => Index == 0; }