fix(kpi-history): backlogTotal trend records the real site-backlog aggregate instead of a hardwired zero
This commit is contained in:
@@ -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,
|
||||
|
||||
+100
@@ -0,0 +1,100 @@
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Health;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Types;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
|
||||
using ZB.MOM.WW.ScadaBridge.HealthMonitoring.Kpi;
|
||||
|
||||
namespace ZB.MOM.WW.ScadaBridge.HealthMonitoring.Tests.Kpi;
|
||||
|
||||
/// <summary>
|
||||
/// Task 10 (arch-review 04) coverage for <see cref="CentralHealthAuditBacklogProvider"/>.
|
||||
/// The provider sums the latest per-site <c>SiteAuditBacklog.PendingCount</c> across the
|
||||
/// in-memory <see cref="ICentralHealthAggregator"/> — the same aggregate the live Audit
|
||||
/// backlog tile shows — so the <c>backlogTotal</c> KPI history stops recording a hardwired
|
||||
/// zero. Sites with no report or a null backlog snapshot contribute nothing.
|
||||
/// </summary>
|
||||
public class CentralHealthAuditBacklogProviderTests
|
||||
{
|
||||
private static readonly DateTime At = new(2026, 6, 17, 12, 0, 0, DateTimeKind.Utc);
|
||||
|
||||
[Fact]
|
||||
public void GetPendingBacklogTotal_Sums_PendingCount_Across_Sites()
|
||||
{
|
||||
var aggregator = new StubAggregator
|
||||
{
|
||||
States =
|
||||
{
|
||||
["site-a"] = StateWithBacklog("site-a", 9),
|
||||
["site-b"] = StateWithBacklog("site-b", 4),
|
||||
["site-c"] = new SiteHealthState { SiteId = "site-c", LatestReport = null }, // heartbeat-only
|
||||
},
|
||||
};
|
||||
|
||||
var provider = new CentralHealthAuditBacklogProvider(aggregator);
|
||||
|
||||
// 9 + 4; the null-report site contributes nothing.
|
||||
Assert.Equal(13, provider.GetPendingBacklogTotal());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetPendingBacklogTotal_Returns_Zero_When_No_Backlog_Reported()
|
||||
{
|
||||
var aggregator = new StubAggregator
|
||||
{
|
||||
States =
|
||||
{
|
||||
["site-a"] = new SiteHealthState { SiteId = "site-a", LatestReport = MinimalReport("site-a") with { SiteAuditBacklog = null } },
|
||||
["site-b"] = new SiteHealthState { SiteId = "site-b", LatestReport = null },
|
||||
},
|
||||
};
|
||||
|
||||
var provider = new CentralHealthAuditBacklogProvider(aggregator);
|
||||
|
||||
Assert.Equal(0, provider.GetPendingBacklogTotal());
|
||||
}
|
||||
|
||||
private static SiteHealthState StateWithBacklog(string siteId, int pending) =>
|
||||
new()
|
||||
{
|
||||
SiteId = siteId,
|
||||
LatestReport = MinimalReport(siteId) with
|
||||
{
|
||||
SiteAuditBacklog = new SiteAuditBacklogSnapshot(
|
||||
PendingCount: pending, OldestPendingUtc: null, OnDiskBytes: 0),
|
||||
},
|
||||
};
|
||||
|
||||
private static SiteHealthReport MinimalReport(string siteId) =>
|
||||
new(
|
||||
SiteId: siteId,
|
||||
SequenceNumber: 1,
|
||||
ReportTimestamp: At,
|
||||
DataConnectionStatuses: new Dictionary<string, ConnectionHealth>(),
|
||||
TagResolutionCounts: new Dictionary<string, TagResolutionStatus>(),
|
||||
ScriptErrorCount: 0,
|
||||
AlarmEvaluationErrorCount: 0,
|
||||
StoreAndForwardBufferDepths: new Dictionary<string, int>(),
|
||||
DeadLetterCount: 0,
|
||||
DeployedInstanceCount: 0,
|
||||
EnabledInstanceCount: 0,
|
||||
DisabledInstanceCount: 0);
|
||||
|
||||
/// <summary>
|
||||
/// Hand-rolled <see cref="ICentralHealthAggregator"/> stub — the HealthMonitoring.Tests
|
||||
/// project has no mocking library. Only <see cref="GetAllSiteStates"/> is exercised.
|
||||
/// </summary>
|
||||
private sealed class StubAggregator : ICentralHealthAggregator
|
||||
{
|
||||
public Dictionary<string, SiteHealthState> States { get; } = new();
|
||||
|
||||
public IReadOnlyDictionary<string, SiteHealthState> GetAllSiteStates() => States;
|
||||
|
||||
public SiteHealthState? GetSiteState(string siteId) =>
|
||||
States.TryGetValue(siteId, out var state) ? state : null;
|
||||
|
||||
public void ProcessReport(SiteHealthReport report) => throw new NotSupportedException();
|
||||
|
||||
public void MarkHeartbeat(string siteId, DateTimeOffset receivedAt) => throw new NotSupportedException();
|
||||
|
||||
public void PruneUnknownSites(IReadOnlyCollection<string> knownSiteIds) => throw new NotSupportedException();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user