fix(kpi): rollup backfill plans from its own persisted watermark, not the periodic fold's MAX (KPI-1, WP4 task 4.10)

This commit is contained in:
Joseph Doherty
2026-12-03 10:23:13 -05:00
parent 5c67559a25
commit 2a41122a39
9 changed files with 438 additions and 23 deletions
@@ -487,6 +487,40 @@ public class KpiHistoryRepositoryTests
Assert.Empty(ctx.ChangeTracker.Entries<KpiSample>());
}
/// <summary>
/// KPI-1: the backfill's own watermark is absent until a slice advances it, then moves only
/// forward, and a contiguity-guarded advance neither creates it nor jumps a gap.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task RollupBackfillWatermark_IsMonotonic_AndContiguityGuarded()
{
await using var ctx = NewContext();
var repo = new KpiHistoryRepository(ctx);
Assert.Null(await repo.GetRollupBackfillWatermarkAsync());
// A contiguity-guarded advance (the periodic fold's) never creates the marker.
await repo.AdvanceRollupBackfillWatermarkAsync(Base.AddHours(5), onlyIfReachedUtc: Base.AddHours(2));
Assert.Null(await repo.GetRollupBackfillWatermarkAsync());
// A backfill slice creates and advances it.
await repo.AdvanceRollupBackfillWatermarkAsync(Base.AddHours(24));
Assert.Equal(Base.AddHours(24), await repo.GetRollupBackfillWatermarkAsync());
// Never backwards.
await repo.AdvanceRollupBackfillWatermarkAsync(Base.AddHours(10));
Assert.Equal(Base.AddHours(24), await repo.GetRollupBackfillWatermarkAsync());
// A periodic fold whose window starts past the marker (a gap) does not move it…
await repo.AdvanceRollupBackfillWatermarkAsync(Base.AddHours(30), onlyIfReachedUtc: Base.AddHours(27));
Assert.Equal(Base.AddHours(24), await repo.GetRollupBackfillWatermarkAsync());
// …one that the marker reaches does.
await repo.AdvanceRollupBackfillWatermarkAsync(Base.AddHours(26), onlyIfReachedUtc: Base.AddHours(23));
Assert.Equal(Base.AddHours(26), await repo.GetRollupBackfillWatermarkAsync());
}
/// <summary>Verifies that the latest rollup hour is null when no rollups exist.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]