using InnovEnergy.Lib.Time.Unix; namespace InnovEnergy.Lib.S3.Metadata; public record AggregationLevel { public UnixTimeSpan RetentionPeriod { get; } public UnixTimeSpan SamplePeriod { get; } public UInt32 RetentionBufferSize { get; } public AggregationLevel(UnixTimeSpan samplePeriod, UnixTimeSpan retentionPeriod) { SamplePeriod = samplePeriod; RetentionPeriod = retentionPeriod; RetentionBufferSize = retentionPeriod / samplePeriod; } public IEnumerable RangeExclusive(UnixTime from, UnixTime to) { if (from > to) throw new ArgumentOutOfRangeException(nameof(to)); for (var t = GetPeriodStartTime(from); t < to; t += SamplePeriod) yield return t; } // TODO: public IEnumerable RangeInclusive(UnixTime from, UnixTime to) { if (from >= to) throw new ArgumentOutOfRangeException(nameof(to)); from = GetPeriodStartTime(from); to = GetPeriodStartTime(to) + SamplePeriod; for (var t = GetPeriodStartTime(from); t < to; t += SamplePeriod) yield return t; } public UInt32 GetRetentionIndex(UnixTime t) { return t / SamplePeriod % RetentionBufferSize; } public UnixTime GetPeriodStartTime(UnixTime t) { return UnixTime.Epoch + t / SamplePeriod * SamplePeriod; // integer division! } public override String ToString() => SamplePeriod.ToString(); }