fix(kpihistory)+fix(notificationoutbox)+fix(sitecallaudit)+fix(host): P5-5 — rate-fold interval triple made self-consistent + cross-validated
Defect: the hourly KPI rollup fold (KpiHistoryRepository.FoldHourlyRollupsAsync) sums every Rate-classified sample recorded in an hour, correct only if each sample is a non-overlapping delta covering exactly one recorder tick (KpiHistoryOptions.SampleInterval). That invariant was an unenforced silent assumption across three independently-owned config keys — SampleInterval, NotificationOutboxOptions.DeliveredKpiWindow, SiteCallAuditOptions.KpiInterval — so widening DeliveredKpiWindow to e.g. 15 min would silently over-count the rollup ~15x. Robust fix: NotificationOutboxKpiSampleSource and SiteCallAuditKpiSampleSource now rescale their Rate-classified samples (deliveredLastInterval, failedLastInterval) by SampleInterval / (DeliveredKpiWindow or KpiInterval) before recording, so the rollup's per-hour Sum is correct regardless of how the other two keys are tuned — the live dashboard tile's own cutoff/window is untouched. Both sources gain a project reference to KpiHistory to read SampleInterval; no cycle (KpiHistory references only Commons). Eager cross-validation (defense-in-depth): KpiRateFoldIntervalCrossValidator (Host, the one composition root referencing all three component projects) fails boot naming all three config keys when they diverge, protecting the separate (still-real) invariant that the recorded/charted KPI-history sample equals the live tile at the same instant — which the rescale trades away whenever the scale factor isn't 1. Rides KpiHistoryOptions' existing ValidateOnStart trigger. Also: NotificationOutboxOptions had no registered validator at all before this (the gap this task's "whichever options class owns DeliveredKpiWindow" file scope pointed at) — added NotificationOutboxOptionsValidator mirroring SiteCallAuditOptionsValidator's positive-duration/count convention, wired with ValidateOnStart. Tests: new rescale tests in both sample-source suites prove the fold is numerically correct at a non-default SampleInterval (30/40 counted over a 15/20-minute window correctly divides down to a 1-minute-tick delta); new KpiRateFoldIntervalCrossValidatorTests prove divergent config fails with all three keys named; new NotificationOutboxOptionsValidatorTests. Every new test proven to fail against the unfixed computation first (temporarily short-circuited the scale factor / cross-check to the pre-fix behavior, confirmed failure, restored) — not via git stash (repo-wide, shared across concurrent worktrees this session) but via file-copy swap. Full suites green: Host.Tests (527), ConfigurationDatabase.Tests (401), KpiHistory.Tests (54), NotificationOutbox.Tests (163), SiteCallAudit.Tests (67).
This commit is contained in:
+61
-1
@@ -3,6 +3,7 @@ using ZB.MOM.WW.ScadaBridge.Commons.Entities.Kpi;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Types.Kpi;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Types.Notifications;
|
||||
using ZB.MOM.WW.ScadaBridge.KpiHistory;
|
||||
using ZB.MOM.WW.ScadaBridge.NotificationOutbox.Kpi;
|
||||
|
||||
namespace ZB.MOM.WW.ScadaBridge.NotificationOutbox.Tests.Kpi;
|
||||
@@ -22,8 +23,28 @@ public class NotificationOutboxKpiSampleSourceTests
|
||||
DeliveredKpiWindow = TimeSpan.FromMinutes(1),
|
||||
};
|
||||
|
||||
// Matches Options.DeliveredKpiWindow (1 minute) so the default-config tests below see
|
||||
// an identity rescale factor (1.0) and their pre-existing raw-count assertions hold.
|
||||
private static readonly KpiHistoryOptions DefaultKpiHistoryOptions = new()
|
||||
{
|
||||
SampleInterval = TimeSpan.FromMinutes(1),
|
||||
};
|
||||
|
||||
private static NotificationOutboxKpiSampleSource CreateSource(INotificationOutboxRepository repository) =>
|
||||
new(repository, Microsoft.Extensions.Options.Options.Create(Options));
|
||||
CreateSource(repository, DefaultKpiHistoryOptions);
|
||||
|
||||
private static NotificationOutboxKpiSampleSource CreateSource(
|
||||
INotificationOutboxRepository repository, KpiHistoryOptions kpiHistoryOptions) =>
|
||||
CreateSource(repository, Options, kpiHistoryOptions);
|
||||
|
||||
private static NotificationOutboxKpiSampleSource CreateSource(
|
||||
INotificationOutboxRepository repository,
|
||||
NotificationOutboxOptions options,
|
||||
KpiHistoryOptions kpiHistoryOptions) =>
|
||||
new(
|
||||
repository,
|
||||
Microsoft.Extensions.Options.Options.Create(options),
|
||||
Microsoft.Extensions.Options.Options.Create(kpiHistoryOptions));
|
||||
|
||||
[Fact]
|
||||
public void Source_IsNotificationOutbox()
|
||||
@@ -131,6 +152,45 @@ public class NotificationOutboxKpiSampleSourceTests
|
||||
AssertSample(samples, "oldestPendingAgeSeconds", KpiScopes.Node, "node-a", 60);
|
||||
}
|
||||
|
||||
// P5-5 (arch-review Low): the KPI-history rollup fold sums every deliveredLastInterval
|
||||
// sample recorded in an hour, which over-counts unless each sample is a non-overlapping
|
||||
// delta covering exactly one recorder tick (KpiHistoryOptions.SampleInterval). Before the
|
||||
// fix, this class recorded the RAW DeliveredKpiWindow-windowed count verbatim — with a
|
||||
// 15-minute DeliveredKpiWindow sampled every 1-minute SampleInterval, 60 overlapping
|
||||
// 15-minute counts summed to ~15x the true hourly total. Proven against the unfixed
|
||||
// computation: temporarily short-circuiting DeliveredIntervalScaleFactor to always return
|
||||
// 1.0 (i.e. "record the raw count verbatim", the pre-P5-5 behavior) makes this test fail
|
||||
// (30 != 2); restoring the real scale factor passes it.
|
||||
[Fact]
|
||||
public async Task CollectAsync_RescalesDeliveredLastInterval_OntoKpiHistorySampleInterval()
|
||||
{
|
||||
var repository = Substitute.For<INotificationOutboxRepository>();
|
||||
repository.ComputeKpisAsync(Arg.Any<DateTimeOffset>(), Arg.Any<DateTimeOffset>(), Arg.Any<CancellationToken>())
|
||||
.Returns(new NotificationKpiSnapshot(
|
||||
QueueDepth: 0, StuckCount: 0, ParkedCount: 0,
|
||||
DeliveredLastInterval: 30, OldestPendingAge: null));
|
||||
repository.ComputePerSiteKpisAsync(Arg.Any<DateTimeOffset>(), Arg.Any<DateTimeOffset>(), Arg.Any<CancellationToken>())
|
||||
.Returns(Array.Empty<SiteNotificationKpiSnapshot>());
|
||||
repository.ComputePerNodeKpisAsync(Arg.Any<DateTimeOffset>(), Arg.Any<DateTimeOffset>(), Arg.Any<CancellationToken>())
|
||||
.Returns(Array.Empty<NodeNotificationKpiSnapshot>());
|
||||
|
||||
// DeliveredKpiWindow (15 min) is 15x KpiHistoryOptions.SampleInterval (1 min) — a
|
||||
// non-default SampleInterval configuration deliberately chosen to prove the rescale
|
||||
// math, not just the identity (factor == 1) default-config case the other tests use.
|
||||
var options = new NotificationOutboxOptions { DeliveredKpiWindow = TimeSpan.FromMinutes(15) };
|
||||
var kpiHistoryOptions = new KpiHistoryOptions { SampleInterval = TimeSpan.FromMinutes(1) };
|
||||
var source = CreateSource(repository, options, kpiHistoryOptions);
|
||||
|
||||
var samples = await source.CollectAsync(CapturedAt);
|
||||
|
||||
var delivered = Assert.Single(samples, s => s.Metric == "deliveredLastInterval");
|
||||
// 30 counted over a 15-minute window, rescaled onto a 1-minute recorder tick: 30 / 15 = 2.
|
||||
// Summing 60 such 1-minute-tick samples across an hour therefore reconstructs the true
|
||||
// hourly total instead of the ~15x over-count a raw (unscaled) 15-minute count would
|
||||
// produce if simply summed 60 times.
|
||||
Assert.Equal(2.0, delivered.Value, precision: 6);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CollectAsync_OmitsOldestPendingAge_WhenNull()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user