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

52 lines
1.5 KiB
C#
Raw Normal View History

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