feat(adminui): driver Controls status panel shows one entry per reporting node (#851)

DriverStatusPanel (the /hosts Controls dialog) showed one snapshot per driver - the most
recently published across both nodes of the redundant pair - so a driver Faulted on one node
and Healthy on its peer flipped on every publish. It now primes from the store's GetAll() and
keeps one entry per ReportingNodeId (DriverStatusByNode): per-node state chip in the header,
per-node last-success / 5-min errors / last error, per-node stale dimming. A snapshot with no
node id (pre-#847 publisher) renders unlabelled, sorted last. A primed copy older than a racing
live publish for the same node is dropped.
This commit is contained in:
Joseph Doherty
2026-11-11 06:16:06 -05:00
parent d1b6cad299
commit b40f08e187
3 changed files with 294 additions and 50 deletions
@@ -0,0 +1,86 @@
using ZB.MOM.WW.OtOpcUa.Commons.Messages.Drivers;
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Hosts;
/// <summary>
/// One reporting node's last-known health for a driver, as the Controls dialog's
/// <c>DriverStatusPanel</c> renders it (lmxopcua#851).
/// </summary>
/// <param name="NodeId">The reporting <c>ClusterNode.NodeId</c>; null when the snapshot came from a node that
/// predates <see cref="DriverHealthChanged.ReportingNodeId"/> (#847) — it still renders, unlabelled.</param>
/// <param name="Snapshot">The node's most recent snapshot.</param>
/// <param name="ReceivedUtc">When THIS AdminUI node received it (local clock — drives the stale dimming, so a
/// peer's clock skew cannot make a live row look stale).</param>
public sealed record DriverNodeStatus(string? NodeId, DriverHealthChanged Snapshot, DateTime ReceivedUtc)
{
/// <summary>Render key, unique per reporting node (the empty string stands for "no node id").</summary>
public string Key => NodeId ?? "";
/// <summary>The label the panel prints for the node.</summary>
public string Label => NodeId ?? "—";
/// <summary>True once no snapshot has arrived from this node for longer than <paramref name="staleAfter"/>.</summary>
public bool IsStale(DateTime nowUtc, TimeSpan staleAfter) => nowUtc - ReceivedUtc > staleAfter;
}
/// <summary>
/// The per-node health view of ONE driver instance behind the Controls dialog's status panel (lmxopcua#851).
/// Both nodes of a redundant pair run the same driver instance, so the panel shows one entry per reporting
/// node instead of the single most-recently-published snapshot (which flipped between a Faulted node and a
/// Healthy peer on every publish). Thread-safe: <see cref="Upsert"/> is called on the snapshot store's bridge
/// thread while the render reads <see cref="Entries"/>.
/// </summary>
public sealed class DriverStatusByNode
{
private readonly object _gate = new();
private readonly Dictionary<string, DriverNodeStatus> _byNode = new(StringComparer.Ordinal);
/// <summary>Initializes a new view for one driver instance.</summary>
/// <param name="driverInstanceId">The driver instance whose snapshots this view keeps; others are ignored.</param>
public DriverStatusByNode(string driverInstanceId) => DriverInstanceId = driverInstanceId;
/// <summary>The driver instance this view tracks.</summary>
public string DriverInstanceId { get; }
/// <summary>
/// Records a snapshot as its node's latest. Snapshots for other driver instances are ignored, and so is one
/// published EARLIER than the node's current entry (both stamped by the same node's clock): the panel primes
/// from <c>GetAll()</c> after subscribing, so a live publish can land before the primed copy of an older
/// one. Returns true when the view changed.
/// </summary>
/// <param name="snapshot">The published snapshot.</param>
/// <param name="receivedUtc">Local receive time.</param>
public bool Upsert(DriverHealthChanged snapshot, DateTime receivedUtc)
{
if (!string.Equals(snapshot.DriverInstanceId, DriverInstanceId, StringComparison.Ordinal))
return false;
var nodeId = string.IsNullOrWhiteSpace(snapshot.ReportingNodeId) ? null : snapshot.ReportingNodeId;
var entry = new DriverNodeStatus(nodeId, snapshot, receivedUtc);
lock (_gate)
{
if (_byNode.TryGetValue(entry.Key, out var current) && snapshot.PublishedUtc < current.Snapshot.PublishedUtc)
return false;
_byNode[entry.Key] = entry;
}
return true;
}
/// <summary>
/// A point-in-time copy of every node's entry, ordered by node id (ordinal, case-insensitive) with the
/// unlabelled (pre-#847) entry last.
/// </summary>
public IReadOnlyList<DriverNodeStatus> Entries
{
get
{
DriverNodeStatus[] copy;
lock (_gate) copy = _byNode.Values.ToArray();
return copy
.OrderBy(e => e.NodeId is null ? 1 : 0)
.ThenBy(e => e.NodeId ?? "", StringComparer.OrdinalIgnoreCase)
.ToList();
}
}
}