51 lines
1.4 KiB
C#
51 lines
1.4 KiB
C#
namespace StellaOps.Concelier.Connector.Common.Cursors;
|
|
|
|
/// <summary>
|
|
/// Utility methods for computing sliding time-window ranges used by connectors.
|
|
/// </summary>
|
|
public static class TimeWindowCursorPlanner
|
|
{
|
|
public static TimeWindow GetNextWindow(DateTimeOffset now, TimeWindowCursorState? state, TimeWindowCursorOptions options)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(options);
|
|
options.EnsureValid();
|
|
|
|
var effectiveState = state ?? TimeWindowCursorState.Empty;
|
|
|
|
var earliest = now - options.InitialBackfill;
|
|
var anchorEnd = effectiveState.LastWindowEnd ?? earliest;
|
|
if (anchorEnd < earliest)
|
|
{
|
|
anchorEnd = earliest;
|
|
}
|
|
|
|
var start = anchorEnd - options.Overlap;
|
|
if (start < earliest)
|
|
{
|
|
start = earliest;
|
|
}
|
|
|
|
var end = start + options.WindowSize;
|
|
if (end > now)
|
|
{
|
|
end = now;
|
|
}
|
|
|
|
if (end <= start)
|
|
{
|
|
end = start + options.MinimumWindowSize;
|
|
if (end > now)
|
|
{
|
|
end = now;
|
|
}
|
|
}
|
|
|
|
if (end <= start)
|
|
{
|
|
throw new InvalidOperationException("Unable to compute a non-empty time window with the provided options.");
|
|
}
|
|
|
|
return new TimeWindow(start, end);
|
|
}
|
|
}
|