From fa1269111883e22972502cd7411a648020a4175e Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Thu, 3 Dec 2026 09:43:22 -0500 Subject: [PATCH] perf(dashboard): Alarms page re-projects only when the alarm set changed (DSH-010, #256) --- .../Components/Pages/AlarmsPage.razor | 18 ++++++++--- .../Dashboard/DashboardAlarmView.cs | 16 ++++++++++ .../Dashboard/DashboardAlarmViewTests.cs | 31 +++++++++++++++++++ 3 files changed, 60 insertions(+), 5 deletions(-) diff --git a/src/ZB.MOM.WW.MxGateway.Server/Dashboard/Components/Pages/AlarmsPage.razor b/src/ZB.MOM.WW.MxGateway.Server/Dashboard/Components/Pages/AlarmsPage.razor index 690a6be2..86a847cd 100644 --- a/src/ZB.MOM.WW.MxGateway.Server/Dashboard/Components/Pages/AlarmsPage.razor +++ b/src/ZB.MOM.WW.MxGateway.Server/Dashboard/Components/Pages/AlarmsPage.razor @@ -414,11 +414,19 @@ _snapshotTruncated = result.SnapshotTruncated; _workerPid = result.WorkerProcessId; _lastRefresh = DateTimeOffset.UtcNow; - _alarms.Clear(); - _alarms.AddRange(result.Alarms); - _alarmGeneration++; - _unackedCount = _alarms.Count(alarm => alarm.State == AlarmConditionState.Active); - _ackedCount = _alarms.Count(alarm => alarm.State == AlarmConditionState.ActiveAcked); + + // Replace the list, and bump the generation that invalidates the memoized filter/sort and + // area views, only when the alarms actually differ: the poll runs every 3 s per viewer and + // the alarm set is usually static (DSH-010). + if (!DashboardAlarmView.AreSame(_alarms, result.Alarms)) + { + _alarms.Clear(); + _alarms.AddRange(result.Alarms); + _alarmGeneration++; + _unackedCount = _alarms.Count(alarm => alarm.State == AlarmConditionState.Active); + _ackedCount = _alarms.Count(alarm => alarm.State == AlarmConditionState.ActiveAcked); + } + StateHasChanged(); } diff --git a/src/ZB.MOM.WW.MxGateway.Server/Dashboard/DashboardAlarmView.cs b/src/ZB.MOM.WW.MxGateway.Server/Dashboard/DashboardAlarmView.cs index afa4aa69..75259b86 100644 --- a/src/ZB.MOM.WW.MxGateway.Server/Dashboard/DashboardAlarmView.cs +++ b/src/ZB.MOM.WW.MxGateway.Server/Dashboard/DashboardAlarmView.cs @@ -43,6 +43,22 @@ public sealed class DashboardAlarmView /// Gets how many times the area list was actually computed (diagnostic, for tests). internal int AreaComputations { get; private set; } + /// + /// Returns whether two poll results hold the same alarms in the same order, so the caller can keep + /// its list and generation (and with them the memoized filter and area views) when nothing changed. + /// + /// The list the page currently holds. + /// The latest poll result. + /// when every alarm row is value-equal, in order. + public static bool AreSame(IReadOnlyList current, IReadOnlyList incoming) + { + ArgumentNullException.ThrowIfNull(current); + ArgumentNullException.ThrowIfNull(incoming); + + // DashboardActiveAlarm is a record, so Equals compares every displayed field. + return current.SequenceEqual(incoming); + } + /// Returns the alarms matching , highest severity then newest first. /// /// Version of ; the caller bumps it whenever it replaces the list's contents. diff --git a/src/ZB.MOM.WW.MxGateway.Tests/Dashboard/DashboardAlarmViewTests.cs b/src/ZB.MOM.WW.MxGateway.Tests/Dashboard/DashboardAlarmViewTests.cs index d9a56452..864cc070 100644 --- a/src/ZB.MOM.WW.MxGateway.Tests/Dashboard/DashboardAlarmViewTests.cs +++ b/src/ZB.MOM.WW.MxGateway.Tests/Dashboard/DashboardAlarmViewTests.cs @@ -27,6 +27,37 @@ public sealed class DashboardAlarmViewTests Assert.Equal(["B", "A"], first.Select(alarm => alarm.Reference)); } + /// + /// DSH-010: a poll that returns an unchanged alarm set is detected as such, so the page keeps its + /// generation and the filter/sort stays at one computation; a real change is detected. + /// + [Fact] + public void AreSame_DetectsUnchangedPollsSoTheGenerationIsNotBumped() + { + DashboardAlarmView view = new(); + List held = [Alarm("A", 500), Alarm("B", 800)]; + long generation = 1; + view.Filtered(generation, DefaultCriteria, held); + + // Two further polls with value-equal (but freshly projected) rows. + for (int poll = 0; poll < 2; poll++) + { + DashboardActiveAlarm[] incoming = [Alarm("A", 500), Alarm("B", 800)]; + if (!DashboardAlarmView.AreSame(held, incoming)) + { + held = [.. incoming]; + generation++; + } + + view.Filtered(generation, DefaultCriteria, held); + } + + Assert.Equal(1, view.FilterComputations); + + Assert.False(DashboardAlarmView.AreSame(held, [Alarm("A", 500), Alarm("B", 900)])); + Assert.False(DashboardAlarmView.AreSame(held, [Alarm("A", 500)])); + } + /// A changed filter input or a new alarm generation recomputes. [Fact] public void Filtered_WhenCriteriaOrGenerationChange_Recomputes()