fix(kpi-history): backlogTotal trend records the real site-backlog aggregate instead of a hardwired zero

This commit is contained in:
Joseph Doherty
2026-07-09 07:51:55 -04:00
parent 1dd993ec2f
commit 1b53de1933
6 changed files with 271 additions and 3 deletions
@@ -1,5 +1,6 @@
using NSubstitute;
using ZB.MOM.WW.ScadaBridge.AuditLog.Kpi;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Kpi;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;
using ZB.MOM.WW.ScadaBridge.Commons.Types;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Kpi;
@@ -97,6 +98,57 @@ public class AuditLogKpiSampleSourceTests
Assert.Empty(samples);
}
[Fact]
public async Task CollectAsync_uses_backlog_provider_when_present()
{
// The repository's snapshot carries the hardwired zero the append-only
// repo always emits; the provider carries the real cross-site aggregate.
var snapshot = new AuditLogKpiSnapshot(
TotalEventsLastHour: 10,
ErrorEventsLastHour: 1,
BacklogTotal: 0,
AsOfUtc: CapturedAt);
var repo = Substitute.For<IAuditLogRepository>();
repo.GetKpiSnapshotAsync(Arg.Any<TimeSpan>(), Arg.Any<DateTime?>(), Arg.Any<CancellationToken>())
.Returns(snapshot);
var backlog = Substitute.For<IAuditBacklogProvider>();
backlog.GetPendingBacklogTotal().Returns(42L);
var sut = new AuditLogKpiSampleSource(repo, backlog);
var samples = await sut.CollectAsync(CapturedAt);
// Volume + error metrics still come from the snapshot; backlog is the
// provider's real aggregate, not the snapshot's zero.
AssertMetric(samples, "totalEventsLastHour", 10);
AssertMetric(samples, "errorEventsLastHour", 1);
AssertMetric(samples, "backlogTotal", 42);
}
[Fact]
public async Task CollectAsync_falls_back_to_snapshot_backlog_when_provider_absent()
{
// No provider (sites / unit tests): the sample source degrades to the
// snapshot's own BacklogTotal — unchanged pre-Task-10 behavior.
var snapshot = new AuditLogKpiSnapshot(
TotalEventsLastHour: 10,
ErrorEventsLastHour: 1,
BacklogTotal: 7,
AsOfUtc: CapturedAt);
var repo = Substitute.For<IAuditLogRepository>();
repo.GetKpiSnapshotAsync(Arg.Any<TimeSpan>(), Arg.Any<DateTime?>(), Arg.Any<CancellationToken>())
.Returns(snapshot);
var sut = new AuditLogKpiSampleSource(repo, backlogProvider: null);
var samples = await sut.CollectAsync(CapturedAt);
AssertMetric(samples, "backlogTotal", 7);
}
private static void AssertMetric(
IReadOnlyList<Commons.Entities.Kpi.KpiSample> samples,
string metric,