Innovenergy_trunk/csharp/Lib/S3/Metadata/AggregationLevel.cs

52 lines
1.5 KiB
C#
Raw Normal View History

using InnovEnergy.Lib.Time.Unix;
2023-02-16 12:57:06 +00:00
namespace InnovEnergy.Lib.S3.Metadata;
2023-02-16 12:57:06 +00:00
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<UnixTime> 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<UnixTime> 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();
}