using ZB.MOM.WW.MxGateway.Contracts.Proto;
namespace ZB.MOM.WW.MxGateway.Server.Dashboard;
///
/// One active-alarm row as shown on the dashboard Alarms tab. Projected
/// from an so the Razor component never
/// touches protobuf types directly.
///
public sealed record DashboardActiveAlarm(
string Reference,
string Provider,
string Area,
string Source,
string AlarmType,
int Severity,
AlarmConditionState State,
DateTimeOffset? LastTransition,
string OperatorUser,
string OperatorComment,
string Description)
{
/// Projects a worker active-alarm snapshot into a dashboard alarm row.
/// The snapshot returned by QueryActiveAlarms.
/// The projected dashboard alarm.
public static DashboardActiveAlarm FromSnapshot(ActiveAlarmSnapshot snapshot)
{
ArgumentNullException.ThrowIfNull(snapshot);
string provider = string.Empty;
string reference = snapshot.AlarmFullReference ?? string.Empty;
int bang = reference.IndexOf('!', StringComparison.Ordinal);
if (bang > 0)
{
provider = reference[..bang];
}
return new DashboardActiveAlarm(
Reference: reference,
Provider: provider,
Area: snapshot.Category ?? string.Empty,
Source: snapshot.SourceObjectReference ?? string.Empty,
AlarmType: snapshot.AlarmTypeName ?? string.Empty,
Severity: snapshot.Severity,
State: snapshot.CurrentState,
LastTransition: snapshot.LastTransitionTimestamp?.ToDateTimeOffset(),
OperatorUser: snapshot.OperatorUser ?? string.Empty,
OperatorComment: snapshot.OperatorComment ?? string.Empty,
Description: snapshot.Description ?? string.Empty);
}
/// True when this alarm is active and not yet acknowledged.
public bool IsUnacknowledged => State == AlarmConditionState.Active;
}
/// Result of a dashboard active-alarm query.
/// The active alarms, or an empty list on error.
/// A diagnostic message when the query failed; otherwise null.
/// The worker process id backing the dashboard session, when available.
public sealed record DashboardAlarmQueryResult(
IReadOnlyList Alarms,
string? Error,
int? WorkerProcessId);