namespace ZB.MOM.WW.OtOpcUa.AdminUI.Hosts; using ZB.MOM.WW.OtOpcUa.Commons.Messages.Drivers; using ZB.MOM.WW.OtOpcUa.Core.Abstractions; /// /// One configured host node within a cluster, as the /hosts page needs it: the cluster /// it belongs to, its node id, and the host/port its OPC UA endpoint binds. /// /// Cluster this node belongs to. /// Stable node identifier within the cluster. /// Host name or address the node's OPC UA server binds. /// OPC UA TCP port the node listens on. public sealed record HostsNodeInfo(string ClusterId, string NodeId, string Host, int OpcUaPort); /// /// A configured driver instance's identity, used to enrich a live health snapshot with the /// authored display name and driver type. Sourced from the config DB by the caller; this type /// is deliberately DB-agnostic so the builder stays pure and unit-testable. /// /// Globally-unique driver instance id (joins to a snapshot). /// Cluster the instance is configured under. /// Authored display name. /// Driver type string (Modbus, S7, Galaxy, ...). /// Whether the instance is authored enabled — forwarded to the row's /// DriverStatusPanel, which renders a "Disabled — not deployed" notice and opens no /// subscription when false. public sealed record HostsDriverInstanceInfo( string DriverInstanceId, string ClusterId, string Name, string DriverType, bool Enabled = true); /// /// One row in the /hosts "Driver Instances" table: a live health snapshot enriched with /// the configured name + type (both null when no matching instance is configured). /// /// Globally-unique driver instance id. /// Configured display name; null when unknown. /// Configured driver type; null when unknown. /// Last-known driver state string (Healthy, Faulted, Reconnecting, ...). /// Most recent successful equipment read; null if never. /// Latest error message; null when none. /// Faulted-transition count in the last 5 minutes. /// Timestamp the snapshot was published. /// When the driver last reported that the remote's tag set may have /// changed; null when never (or when the driver cannot report it). Advisory — the served address space /// is unchanged and an operator must re-browse the device via /raw to pick anything up. /// The driver-supplied reason for that report; null when /// is null. /// Per-host connectivity for a multi-device driver, ordered by host name; null /// when the driver reports no per-host detail. Lets one unreachable device show even while the driver /// row itself is Healthy. /// Authored tags the driver could not build into its poll table; null when there /// is no answer — the driver cannot report skips, or it faulted before building a tag table. Lets /// "ignoring 20 of 22 tags" show even while every other column on the row — state, last read, error /// count, last error — reads perfectly healthy (Gitea #541). /// Whether the matching configured instance is authored enabled; /// when no instance matched. Fed to the row's expandable DriverStatusPanel (Gitea #546). An /// unmatched row defaults to enabled on purpose: the row only exists because a live snapshot arrived, /// so telling the operator it is "not deployed" would contradict the evidence in front of them. public sealed record HostsDriverRow( string DriverInstanceId, string? Name, string? DriverType, string State, DateTime? LastSuccessfulReadUtc, string? LastError, int ErrorCount5Min, DateTime PublishedUtc, DateTime? RediscoveryNeededUtc = null, string? RediscoveryReason = null, IReadOnlyList? HostStatuses = null, SkippedTagReport? SkippedTags = null, bool Enabled = true) { /// Whether this driver is ignoring at least one authored tag — the condition the whole /// column exists to make visible. public bool HasSkippedTags => SkippedTags is { SkippedCount: > 0 }; /// /// Operator-facing detail for the skip chip: the sample of skipped RawPaths with reasons, plus an /// explicit note when the sample is shorter than the count. The truncation note is not optional — /// without it a capped list reads as the complete one, and an operator who fixes those twenty /// believes they are done. /// /// The tooltip text; empty when nothing was skipped. public string SkippedTagsTitle() { if (SkippedTags is not { SkippedCount: > 0 } r) return ""; var lead = r.SkippedEverything ? $"This driver is polling NONE of its {r.AuthoredCount} authored tag(s)." : $"{r.SkippedCount} of {r.AuthoredCount} authored tag(s) are not being polled."; var detail = string.Join(" · ", r.Sample.Select(s => $"{s.RawPath}: {s.Reason}")); var more = r.SkippedCount > r.Sample.Count ? $" (+{r.SkippedCount - r.Sample.Count} more not shown)" : ""; return $"{lead} They report BadNodeIdUnknown and the driver's state does not reflect them. {detail}{more}"; } /// Hosts that are not , ordered by name — the ones worth an /// operator's attention. Empty when every host is fine or none are reported. /// counts as degraded on purpose: a probe that has not yet /// completed its first tick, or one a driver failed to start (AbCip logs exactly this case), reports /// Unknown — and silently rendering that as healthy is how the gap got missed the first time. public IReadOnlyList DegradedHosts { get; } = (HostStatuses ?? []) .Where(h => h.State != HostState.Running) .OrderBy(h => h.HostName, StringComparer.OrdinalIgnoreCase) .ToList(); } /// /// One cluster's section on the /hosts page: its configured nodes plus its enriched /// driver-instance rows. /// /// Cluster identifier. /// Configured nodes in this cluster, ordered by node id. /// Driver rows in this cluster, ordered by name then instance id. public sealed record HostsClusterGroup(string ClusterId, IReadOnlyList Nodes, IReadOnlyList Drivers); /// /// Pure, DB-agnostic builder for the /hosts "Driver Instances" section. Groups live /// driver-health snapshots by cluster, attaches each cluster's configured nodes, and enriches /// every driver row with its configured name + type. Null-safe on all inputs and stable-ordered /// so the Razor render is deterministic. /// public static class HostsDriverView { /// /// Builds the per-cluster view model. Clusters are the case-insensitive union of cluster ids /// seen across and , ordered /// case-insensitively. Each group carries its nodes (ordered by node id) and its driver rows /// (snapshots enriched by joining on driver instance id; /// unmatched ids yield null name/type and an enabled row), ordered by name-then-instance-id. /// /// Live health snapshots; null treated as empty. /// Configured cluster nodes; null treated as empty. /// Configured driver instances for enrichment; null treated as empty. /// One per cluster, ordered by cluster id. public static IReadOnlyList Build( IEnumerable? snapshots, IEnumerable? nodes, IEnumerable? instances) { var snapList = (snapshots ?? Enumerable.Empty()).ToList(); var nodeList = (nodes ?? Enumerable.Empty()).ToList(); var instanceList = instances ?? Enumerable.Empty(); // First-wins lookup keyed by driver instance id. var byInstance = new Dictionary(StringComparer.OrdinalIgnoreCase); foreach (var instance in instanceList) { byInstance.TryAdd(instance.DriverInstanceId, instance); } var clusterIds = snapList.Select(s => s.ClusterId) .Concat(nodeList.Select(n => n.ClusterId)) .Distinct(StringComparer.OrdinalIgnoreCase) .OrderBy(id => id, StringComparer.OrdinalIgnoreCase) .ToList(); var groups = new List(clusterIds.Count); foreach (var clusterId in clusterIds) { var clusterNodes = nodeList .Where(n => string.Equals(n.ClusterId, clusterId, StringComparison.OrdinalIgnoreCase)) .OrderBy(n => n.NodeId, StringComparer.OrdinalIgnoreCase) .ToList(); var clusterDrivers = snapList .Where(s => string.Equals(s.ClusterId, clusterId, StringComparison.OrdinalIgnoreCase)) .Select(s => { byInstance.TryGetValue(s.DriverInstanceId, out var instance); return new HostsDriverRow( s.DriverInstanceId, instance?.Name, instance?.DriverType, s.State, s.LastSuccessfulReadUtc, s.LastError, s.ErrorCount5Min, s.PublishedUtc, s.RediscoveryNeededUtc, s.RediscoveryReason, s.HostStatuses, s.SkippedTags, instance?.Enabled ?? true); }) .OrderBy(d => d.Name ?? d.DriverInstanceId, StringComparer.OrdinalIgnoreCase) .ThenBy(d => d.DriverInstanceId, StringComparer.OrdinalIgnoreCase) .ToList(); groups.Add(new HostsClusterGroup(clusterId, clusterNodes, clusterDrivers)); } return groups; } }