Innovenergy_trunk/csharp/Lib/S3/Drivers/Internal/Util/Sampler.cs

43 lines
1.4 KiB
C#

using InnovEnergy.Lib.S3.Metadata;
using InnovEnergy.Lib.S3.Records;
using InnovEnergy.Lib.S3.Records.Specialized;
using InnovEnergy.Lib.Time.Unix;
namespace InnovEnergy.Lib.S3.Drivers.Internal.Util;
internal class Sampler
{
public Record CurrentRecord { get; set; }
public UnixTime CurrentTimeStamp { get; set; }
public AggregationLevel AggregationLevel { get; }
private UnixTimeSpan SamplePeriod => AggregationLevel.SamplePeriod;
public Sampler(AggregationLevel aggregationLevel, Record record, UnixTime currentTime)
{
AggregationLevel = aggregationLevel;
CurrentRecord = record;
CurrentTimeStamp = aggregationLevel.GetPeriodStartTime(currentTime);
}
// TODO: repeat/max age
public IEnumerable<AggregatedRecord> Sample(Record record, UnixTime timeStamp)
{
timeStamp = AggregationLevel.GetPeriodStartTime(timeStamp);
if (timeStamp < CurrentTimeStamp)
yield break; //throw new IndexOutOfRangeException(nameof(index)); // TODO: log
if (timeStamp > CurrentTimeStamp)
yield return new AggregatedRecord(CurrentRecord, AggregationLevel, CurrentTimeStamp);
for (var t = CurrentTimeStamp + SamplePeriod; t < timeStamp; t += SamplePeriod)
yield return new AggregatedRecord(Record.Empty, AggregationLevel, t);
CurrentTimeStamp = timeStamp;
CurrentRecord = record;
}
}