using ZB.MOM.WW.MxGateway.Contracts.Proto; namespace ZB.MOM.WW.MxGateway.Server.Dashboard; /// The Alarms page's filter inputs, compared by value to decide whether a filtered view is stale. /// Include unacknowledged Active alarms. /// Include ActiveAcked alarms. /// Area to match (case-insensitive), or empty for all. /// Lowest severity shown. /// Highest severity shown. /// Reference/source/description text search; trimmed before matching. public sealed record DashboardAlarmFilterCriteria( bool ShowActive, bool ShowAcked, string Area, int MinSeverity, int MaxSeverity, string Search); /// /// Memoized projections of the Alarms page's alarm list: the filtered, sorted rows and /// the distinct area list. A render used to run the full filter-and-sort twice (the /// "Showing" card and the table) and the area projection once more, on every render — /// including the renders that typing in the search box triggers per keystroke. Each /// projection is now recomputed only when the alarm list (its generation) or, for the /// rows, the filter criteria change. /// /// /// Owned by one page instance and touched only from its render dispatcher, so it is not /// synchronized. /// public sealed class DashboardAlarmView { private long _filteredGeneration = -1; private DashboardAlarmFilterCriteria? _filteredCriteria; private IReadOnlyList _filtered = []; private long _areasGeneration = -1; private IReadOnlyList _areas = []; /// Gets how many times the filtered rows were actually computed (diagnostic, for tests). internal int FilterComputations { get; private set; } /// Gets how many times the area list was actually computed (diagnostic, for tests). internal int AreaComputations { get; private set; } /// Returns the alarms matching , highest severity then newest first. /// /// Version of ; the caller bumps it whenever it replaces the list's contents. /// /// The current filter inputs. /// The full active-alarm list. /// The filtered rows; the same instance while generation and criteria are unchanged. public IReadOnlyList Filtered( long generation, DashboardAlarmFilterCriteria criteria, IReadOnlyList alarms) { ArgumentNullException.ThrowIfNull(criteria); ArgumentNullException.ThrowIfNull(alarms); if (generation == _filteredGeneration && criteria == _filteredCriteria) { return _filtered; } _filtered = Apply(criteria, alarms); _filteredGeneration = generation; _filteredCriteria = criteria; FilterComputations++; return _filtered; } /// Returns the distinct non-blank areas of , sorted case-insensitively. /// Version of . /// The full active-alarm list. /// The area list; the same instance while the generation is unchanged. public IReadOnlyList Areas(long generation, IReadOnlyList alarms) { ArgumentNullException.ThrowIfNull(alarms); if (generation == _areasGeneration) { return _areas; } _areas = alarms .Select(alarm => alarm.Area) .Where(area => !string.IsNullOrWhiteSpace(area)) .Distinct(StringComparer.OrdinalIgnoreCase) .OrderBy(area => area, StringComparer.OrdinalIgnoreCase) .ToArray(); _areasGeneration = generation; AreaComputations++; return _areas; } private static DashboardActiveAlarm[] Apply( DashboardAlarmFilterCriteria criteria, IReadOnlyList alarms) { string query = criteria.Search.Trim(); return alarms .Where(alarm => MatchesState(criteria, alarm)) .Where(alarm => criteria.Area.Length == 0 || string.Equals(alarm.Area, criteria.Area, StringComparison.OrdinalIgnoreCase)) .Where(alarm => alarm.Severity >= criteria.MinSeverity && alarm.Severity <= criteria.MaxSeverity) .Where(alarm => query.Length == 0 || alarm.Reference.Contains(query, StringComparison.OrdinalIgnoreCase) || alarm.Source.Contains(query, StringComparison.OrdinalIgnoreCase) || alarm.Description.Contains(query, StringComparison.OrdinalIgnoreCase)) .OrderByDescending(alarm => alarm.Severity) .ThenByDescending(alarm => alarm.LastTransition ?? DateTimeOffset.MinValue) .ToArray(); } private static bool MatchesState(DashboardAlarmFilterCriteria criteria, DashboardActiveAlarm alarm) { return alarm.State switch { AlarmConditionState.Active => criteria.ShowActive, AlarmConditionState.ActiveAcked => criteria.ShowAcked, _ => true, }; } }