abbbcd1a8d
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).
93 lines
3.9 KiB
C#
93 lines
3.9 KiB
C#
using Microsoft.Extensions.Options;
|
|
using ZB.MOM.WW.ScadaBridge.KpiHistory;
|
|
using ZB.MOM.WW.ScadaBridge.NotificationOutbox;
|
|
using ZB.MOM.WW.ScadaBridge.SiteCallAudit;
|
|
|
|
namespace ZB.MOM.WW.ScadaBridge.Host.Tests;
|
|
|
|
/// <summary>
|
|
/// P5-5 (arch-review Low): <see cref="KpiRateFoldIntervalCrossValidator"/> is the
|
|
/// boot-time guardrail for the "rate-fold interval triple" —
|
|
/// <see cref="KpiHistoryOptions.SampleInterval"/>,
|
|
/// <see cref="NotificationOutboxOptions.DeliveredKpiWindow"/>, and
|
|
/// <see cref="SiteCallAuditOptions.KpiInterval"/>. The sample sources now rescale
|
|
/// their Rate-classified samples onto <c>SampleInterval</c>, so the hourly rollup
|
|
/// fold's correctness no longer depends on this equality — but the
|
|
/// recorded/charted sample no longer equals the live dashboard tile unless it
|
|
/// holds, so this validator still fails the host at boot on any divergence,
|
|
/// naming all three configuration keys.
|
|
/// </summary>
|
|
public class KpiRateFoldIntervalCrossValidatorTests
|
|
{
|
|
private static ValidateOptionsResult Validate(
|
|
KpiHistoryOptions kpiHistoryOptions,
|
|
NotificationOutboxOptions notificationOutboxOptions,
|
|
SiteCallAuditOptions siteCallAuditOptions)
|
|
{
|
|
var validator = new KpiRateFoldIntervalCrossValidator(
|
|
Options.Create(notificationOutboxOptions),
|
|
Options.Create(siteCallAuditOptions));
|
|
return validator.Validate(Options.DefaultName, kpiHistoryOptions);
|
|
}
|
|
|
|
[Fact]
|
|
public void AllThreeEqual_AtDocumentedDefaults_IsValid()
|
|
{
|
|
var result = Validate(
|
|
new KpiHistoryOptions(), new NotificationOutboxOptions(), new SiteCallAuditOptions());
|
|
|
|
Assert.True(result.Succeeded, result.FailureMessage);
|
|
}
|
|
|
|
[Fact]
|
|
public void AllThreeEqual_AtANonDefaultCommonValue_IsValid()
|
|
{
|
|
var common = TimeSpan.FromMinutes(5);
|
|
var result = Validate(
|
|
new KpiHistoryOptions { SampleInterval = common },
|
|
new NotificationOutboxOptions { DeliveredKpiWindow = common },
|
|
new SiteCallAuditOptions { KpiInterval = common });
|
|
|
|
Assert.True(result.Succeeded, result.FailureMessage);
|
|
}
|
|
|
|
[Fact]
|
|
public void DeliveredKpiWindowDiverges_FailsAndNamesAllThreeKeys()
|
|
{
|
|
var result = Validate(
|
|
new KpiHistoryOptions { SampleInterval = TimeSpan.FromMinutes(1) },
|
|
new NotificationOutboxOptions { DeliveredKpiWindow = TimeSpan.FromMinutes(15) },
|
|
new SiteCallAuditOptions { KpiInterval = TimeSpan.FromMinutes(1) });
|
|
|
|
Assert.True(result.Failed);
|
|
Assert.Contains("ScadaBridge:KpiHistory:SampleInterval", result.FailureMessage);
|
|
Assert.Contains("ScadaBridge:NotificationOutbox:DeliveredKpiWindow", result.FailureMessage);
|
|
Assert.Contains("ScadaBridge:SiteCallAudit:KpiInterval", result.FailureMessage);
|
|
}
|
|
|
|
[Fact]
|
|
public void KpiIntervalDiverges_FailsAndNamesAllThreeKeys()
|
|
{
|
|
var result = Validate(
|
|
new KpiHistoryOptions { SampleInterval = TimeSpan.FromMinutes(1) },
|
|
new NotificationOutboxOptions { DeliveredKpiWindow = TimeSpan.FromMinutes(1) },
|
|
new SiteCallAuditOptions { KpiInterval = TimeSpan.FromMinutes(20) });
|
|
|
|
Assert.True(result.Failed);
|
|
Assert.Contains("ScadaBridge:KpiHistory:SampleInterval", result.FailureMessage);
|
|
Assert.Contains("ScadaBridge:NotificationOutbox:DeliveredKpiWindow", result.FailureMessage);
|
|
Assert.Contains("ScadaBridge:SiteCallAudit:KpiInterval", result.FailureMessage);
|
|
}
|
|
|
|
[Fact]
|
|
public void BothDeliveredKpiWindowAndKpiIntervalDiverge_Fails()
|
|
{
|
|
var result = Validate(
|
|
new KpiHistoryOptions { SampleInterval = TimeSpan.FromMinutes(1) },
|
|
new NotificationOutboxOptions { DeliveredKpiWindow = TimeSpan.FromMinutes(15) },
|
|
new SiteCallAuditOptions { KpiInterval = TimeSpan.FromMinutes(20) });
|
|
|
|
Assert.True(result.Failed);
|
|
}
|
|
}
|