diff --git a/docs/requirements/Component-AuditLog.md b/docs/requirements/Component-AuditLog.md index 492025e3..759747b7 100644 --- a/docs/requirements/Component-AuditLog.md +++ b/docs/requirements/Component-AuditLog.md @@ -730,6 +730,15 @@ MS SQL for direct-write events). Unredacted secrets never persist. and emits an `AuditLogPurgedEvent` (partition range, rowcount, duration) per switched partition. A partition-maintenance step rolls forward each month, creating the next month's partition ahead of time. +- **Partition 1 is purged and archived too (AUD-6, 2026-12).** Rows stamped before + the first boundary (a site whose clock was reset, a back-dated row) land in + partition 1, which has no lower boundary, so the per-boundary enumeration never + offered it and those rows outlived retention and the cold archive indefinitely. + `GetPartitionBoundariesOlderThanAsync` now also returns partition 1 — once its + newest row is older than the window — as the **month start of its newest row**, a + value inside partition 1: the switch-out and the archive resolve the partition + with `$partition.pf_AuditLog_Month(value)`, so they handle it like any month, and + the archive is labelled (and its `RetentionDays` counted) by the age of that data. - **Purge failure is a health metric, not just a log line:** every purge step that throws — a partition switch-out, a per-channel override DELETE, or the pre-purge boundary enumeration — publishes an `AuditLogPurgeFailedEvent` (month boundary, diff --git a/src/ZB.MOM.WW.ScadaBridge.Commons/Interfaces/Repositories/IAuditLogRepository.cs b/src/ZB.MOM.WW.ScadaBridge.Commons/Interfaces/Repositories/IAuditLogRepository.cs index f4905224..c3b70f8d 100644 --- a/src/ZB.MOM.WW.ScadaBridge.Commons/Interfaces/Repositories/IAuditLogRepository.cs +++ b/src/ZB.MOM.WW.ScadaBridge.Commons/Interfaces/Repositories/IAuditLogRepository.cs @@ -203,6 +203,13 @@ public interface IAuditLogRepository /// . Boundaries whose partition is empty are /// excluded (a no-op switch is wasted work). Used by the purge actor /// to enumerate retention-eligible months on every tick. + /// + /// Partition 1 — everything before the first boundary, which has no lower boundary of its + /// own — is included too (AUD-6): it is reported as the month start of its newest row, a + /// value that lies inside partition 1, so and the cold + /// archive (both resolve the partition with $partition.pf_AuditLog_Month(value)) + /// purge and archive it like any other month. + /// /// /// Only partitions whose data is entirely older than this UTC datetime are returned. /// Cancellation token. diff --git a/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Repositories/AuditLogRepository.cs b/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Repositories/AuditLogRepository.cs index 1c6daddc..c91c6939 100644 --- a/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Repositories/AuditLogRepository.cs +++ b/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Repositories/AuditLogRepository.cs @@ -827,7 +827,23 @@ VALUES ) x WHERE x.MaxOccurredAt IS NOT NULL AND x.MaxOccurredAt < CAST('{thresholdStr}' AS datetime2(7)) - ORDER BY b.BoundaryValue;"; + UNION ALL + -- AUD-6: partition 1 (everything before the FIRST boundary — back-dated rows from a + -- site whose clock was reset, or test rows) has no boundary of its own, so the + -- per-boundary enumeration above never offered it and its rows outlived retention + -- and the cold archive forever. It is identified by a stand-in value inside it: the + -- month start of its newest row (always below the first boundary, so + -- $partition.pf_AuditLog_Month maps it to partition 1 for the switch-out and the + -- archive, and it labels the archive by the age of the data). + SELECT CAST(DATEFROMPARTS(YEAR(p1.MaxOccurredAt), MONTH(p1.MaxOccurredAt), 1) AS datetime2(7)) + FROM ( + SELECT MAX(a.OccurredAtUtc) AS MaxOccurredAt + FROM dbo.AuditLog a + WHERE $PARTITION.pf_AuditLog_Month(a.OccurredAtUtc) = 1 + ) p1 + WHERE p1.MaxOccurredAt IS NOT NULL + AND p1.MaxOccurredAt < CAST('{thresholdStr}' AS datetime2(7)) + ORDER BY 1;"; var conn = _context.Database.GetDbConnection(); var openedHere = false; diff --git a/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/Repositories/AuditLogRepositoryTests.cs b/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/Repositories/AuditLogRepositoryTests.cs index ea7e59b3..a99e1fb7 100644 --- a/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/Repositories/AuditLogRepositoryTests.cs +++ b/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/Repositories/AuditLogRepositoryTests.cs @@ -1209,6 +1209,54 @@ public class AuditLogRepositoryTests : IClassFixture Assert.DoesNotContain(new DateTime(2026, 8, 1, 0, 0, 0, DateTimeKind.Utc), boundaries); } + /// + /// AUD-6 regression: rows stamped before the FIRST partition boundary (partition 1 — a site + /// with a reset clock, a back-dated row) used to be invisible to the purge enumeration, so + /// they outlived retention and the archive forever. Partition 1 is now offered as the month + /// start of its newest row, and switching that value out purges exactly partition 1. + /// + /// A task that represents the asynchronous operation. + [SkippableFact] + public async Task GetPartitionBoundariesOlderThanAsync_IncludesPartitionOne_AndItSwitchesOut() + { + Skip.IfNot(_fixture.Available, _fixture.SkipReason); + + var siteId = NewSiteId(); + await using var context = CreateContext(); + var repo = new AuditLogRepository(context); + + // Two rows before the first boundary (2026-01-01) and one inside a regular partition. + await repo.InsertIfNotExistsAsync(NewEvent(siteId, occurredAtUtc: new DateTime(2019, 3, 10, 0, 0, 0, DateTimeKind.Utc))); + await repo.InsertIfNotExistsAsync(NewEvent(siteId, occurredAtUtc: new DateTime(2019, 5, 15, 12, 0, 0, DateTimeKind.Utc))); + var survivor = NewEvent(siteId, occurredAtUtc: new DateTime(2026, 7, 12, 0, 0, 0, DateTimeKind.Utc)); + await repo.InsertIfNotExistsAsync(survivor); + + var boundaries = await repo.GetPartitionBoundariesOlderThanAsync(new DateTime(2026, 7, 1, 0, 0, 0, DateTimeKind.Utc)); + + // Partition 1 is offered as the month start of its newest row. + var partitionOne = new DateTime(2019, 5, 1, 0, 0, 0, DateTimeKind.Utc); + Assert.Contains(partitionOne, boundaries); + Assert.Equal(partitionOne, boundaries[0]); + + var purged = await repo.SwitchOutPartitionAsync(partitionOne); + Assert.True(purged >= 2, $"expected the two partition-1 rows to be purged, got {purged}"); + + await using var read = CreateContext(); + var left = await read.Database + .SqlQueryRaw("SELECT COUNT(*) AS Value FROM dbo.AuditLog WHERE OccurredAtUtc < '2026-01-01'") + .SingleAsync(); + Assert.Equal(0, left); + var survivorId = survivor.EventId; + var survived = await read.Database + .SqlQuery($"SELECT COUNT(*) AS Value FROM dbo.AuditLog WHERE EventId = {survivorId}") + .SingleAsync(); + Assert.Equal(1, survived); + + // Nothing is left in partition 1, so it is no longer offered. + var after = await repo.GetPartitionBoundariesOlderThanAsync(new DateTime(2026, 7, 1, 0, 0, 0, DateTimeKind.Utc)); + Assert.DoesNotContain(after, b => b < new DateTime(2026, 1, 1, 0, 0, 0, DateTimeKind.Utc)); + } + // ------------------------------------------------------------------------ // GetKpiSnapshotAsync — Health-dashboard Audit KPI tiles // ------------------------------------------------------------------------