perf(dashboard): Alarms page re-projects only when the alarm set changed (DSH-010, #256)

This commit is contained in:
Joseph Doherty
2026-12-03 09:43:22 -05:00
parent 449b483046
commit fa12691118
3 changed files with 60 additions and 5 deletions
@@ -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();
}
@@ -43,6 +43,22 @@ public sealed class DashboardAlarmView
/// <summary>Gets how many times the area list was actually computed (diagnostic, for tests).</summary>
internal int AreaComputations { get; private set; }
/// <summary>
/// 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.
/// </summary>
/// <param name="current">The list the page currently holds.</param>
/// <param name="incoming">The latest poll result.</param>
/// <returns><see langword="true"/> when every alarm row is value-equal, in order.</returns>
public static bool AreSame(IReadOnlyList<DashboardActiveAlarm> current, IReadOnlyList<DashboardActiveAlarm> incoming)
{
ArgumentNullException.ThrowIfNull(current);
ArgumentNullException.ThrowIfNull(incoming);
// DashboardActiveAlarm is a record, so Equals compares every displayed field.
return current.SequenceEqual(incoming);
}
/// <summary>Returns the alarms matching <paramref name="criteria"/>, highest severity then newest first.</summary>
/// <param name="generation">
/// Version of <paramref name="alarms"/>; the caller bumps it whenever it replaces the list's contents.
@@ -27,6 +27,37 @@ public sealed class DashboardAlarmViewTests
Assert.Equal(["B", "A"], first.Select(alarm => alarm.Reference));
}
/// <summary>
/// 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.
/// </summary>
[Fact]
public void AreSame_DetectsUnchangedPollsSoTheGenerationIsNotBumped()
{
DashboardAlarmView view = new();
List<DashboardActiveAlarm> 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)]));
}
/// <summary>A changed filter input or a new alarm generation recomputes.</summary>
[Fact]
public void Filtered_WhenCriteriaOrGenerationChange_Recomputes()