perf(dashboard): memoize AlarmsPage filtered rows and area list
FilteredAlarms() ran the full filter+sort twice per render (Showing card and table) and Areas() once more, including every search keystroke render. Both now go through DashboardAlarmView, recomputed only when the alarm generation (bumped per poll) or the filter criteria change.
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
using ZB.MOM.WW.MxGateway.Contracts.Proto;
|
||||
using ZB.MOM.WW.MxGateway.Server.Dashboard;
|
||||
|
||||
namespace ZB.MOM.WW.MxGateway.Tests.Dashboard;
|
||||
|
||||
/// <summary>
|
||||
/// Verifies the Alarms page's memoized projections: one computation per change of
|
||||
/// the alarm generation or filter inputs, however many times a render asks.
|
||||
/// </summary>
|
||||
public sealed class DashboardAlarmViewTests
|
||||
{
|
||||
private static readonly DashboardAlarmFilterCriteria DefaultCriteria =
|
||||
new(ShowActive: true, ShowAcked: false, Area: string.Empty, MinSeverity: 0, MaxSeverity: 1000, Search: string.Empty);
|
||||
|
||||
/// <summary>Repeated reads at the same generation and criteria reuse one computation.</summary>
|
||||
[Fact]
|
||||
public void Filtered_WithUnchangedInputs_ComputesOnce()
|
||||
{
|
||||
DashboardAlarmView view = new();
|
||||
DashboardActiveAlarm[] alarms = [Alarm("A", 500), Alarm("B", 800)];
|
||||
|
||||
IReadOnlyList<DashboardActiveAlarm> first = view.Filtered(1, DefaultCriteria, alarms);
|
||||
IReadOnlyList<DashboardActiveAlarm> second = view.Filtered(1, DefaultCriteria with { }, alarms);
|
||||
|
||||
Assert.Same(first, second);
|
||||
Assert.Equal(1, view.FilterComputations);
|
||||
Assert.Equal(["B", "A"], first.Select(alarm => alarm.Reference));
|
||||
}
|
||||
|
||||
/// <summary>A changed filter input or a new alarm generation recomputes.</summary>
|
||||
[Fact]
|
||||
public void Filtered_WhenCriteriaOrGenerationChange_Recomputes()
|
||||
{
|
||||
DashboardAlarmView view = new();
|
||||
DashboardActiveAlarm[] alarms = [Alarm("Tank01.Hi", 500), Alarm("Pump02.Lo", 800)];
|
||||
|
||||
view.Filtered(1, DefaultCriteria, alarms);
|
||||
IReadOnlyList<DashboardActiveAlarm> searched = view.Filtered(1, DefaultCriteria with { Search = "tank" }, alarms);
|
||||
|
||||
Assert.Equal(2, view.FilterComputations);
|
||||
Assert.Equal(["Tank01.Hi"], searched.Select(alarm => alarm.Reference));
|
||||
|
||||
DashboardActiveAlarm[] refreshed = [Alarm("Tank03.Hi", 900)];
|
||||
IReadOnlyList<DashboardActiveAlarm> next = view.Filtered(2, DefaultCriteria with { Search = "tank" }, refreshed);
|
||||
|
||||
Assert.Equal(3, view.FilterComputations);
|
||||
Assert.Equal(["Tank03.Hi"], next.Select(alarm => alarm.Reference));
|
||||
}
|
||||
|
||||
/// <summary>The area list is computed once per alarm generation, independent of filter inputs.</summary>
|
||||
[Fact]
|
||||
public void Areas_ComputesOncePerGeneration()
|
||||
{
|
||||
DashboardAlarmView view = new();
|
||||
DashboardActiveAlarm[] alarms = [Alarm("A", 1, "north"), Alarm("B", 1, "North"), Alarm("C", 1, "east"), Alarm("D", 1, " ")];
|
||||
|
||||
IReadOnlyList<string> first = view.Areas(1, alarms);
|
||||
IReadOnlyList<string> second = view.Areas(1, alarms);
|
||||
|
||||
Assert.Same(first, second);
|
||||
Assert.Equal(1, view.AreaComputations);
|
||||
Assert.Equal(["east", "north"], first);
|
||||
|
||||
view.Areas(2, alarms);
|
||||
Assert.Equal(2, view.AreaComputations);
|
||||
}
|
||||
|
||||
private static DashboardActiveAlarm Alarm(string reference, int severity, string area = "")
|
||||
{
|
||||
return new DashboardActiveAlarm(
|
||||
Reference: reference,
|
||||
Provider: string.Empty,
|
||||
Area: area,
|
||||
Source: string.Empty,
|
||||
AlarmType: string.Empty,
|
||||
Severity: severity,
|
||||
State: AlarmConditionState.Active,
|
||||
LastTransition: null,
|
||||
OperatorUser: string.Empty,
|
||||
OperatorComment: string.Empty,
|
||||
Description: string.Empty);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user