diff --git a/src/Server/ZB.MOM.WW.OtOpcUa.AdminUI/Hosts/HostsDriverView.cs b/src/Server/ZB.MOM.WW.OtOpcUa.AdminUI/Hosts/HostsDriverView.cs new file mode 100644 index 00000000..385a6838 --- /dev/null +++ b/src/Server/ZB.MOM.WW.OtOpcUa.AdminUI/Hosts/HostsDriverView.cs @@ -0,0 +1,124 @@ +namespace ZB.MOM.WW.OtOpcUa.AdminUI.Hosts; + +using ZB.MOM.WW.OtOpcUa.Commons.Messages.Drivers; + +/// +/// 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, ...). +public sealed record HostsDriverInstanceInfo(string DriverInstanceId, string ClusterId, string Name, string DriverType); + +/// +/// 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. +public sealed record HostsDriverRow( + string DriverInstanceId, string? Name, string? DriverType, string State, + DateTime? LastSuccessfulReadUtc, string? LastError, int ErrorCount5Min, DateTime PublishedUtc); + +/// +/// 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), 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); + }) + .OrderBy(d => d.Name ?? d.DriverInstanceId, StringComparer.OrdinalIgnoreCase) + .ThenBy(d => d.DriverInstanceId, StringComparer.OrdinalIgnoreCase) + .ToList(); + + groups.Add(new HostsClusterGroup(clusterId, clusterNodes, clusterDrivers)); + } + + return groups; + } +} diff --git a/src/Server/ZB.MOM.WW.OtOpcUa.AdminUI/Hubs/IDriverStatusSnapshotStore.cs b/src/Server/ZB.MOM.WW.OtOpcUa.AdminUI/Hubs/IDriverStatusSnapshotStore.cs index 0527d555..a4d0da15 100644 --- a/src/Server/ZB.MOM.WW.OtOpcUa.AdminUI/Hubs/IDriverStatusSnapshotStore.cs +++ b/src/Server/ZB.MOM.WW.OtOpcUa.AdminUI/Hubs/IDriverStatusSnapshotStore.cs @@ -14,6 +14,9 @@ public interface IDriverStatusSnapshotStore void Upsert(DriverHealthChanged snapshot); bool TryGet(string driverInstanceId, out DriverHealthChanged snapshot); + /// Returns a point-in-time snapshot of every driver instance's last-known health. + IReadOnlyCollection GetAll(); + /// /// Raised after every with the just-stored snapshot. Lets in-process /// consumers (the Blazor Server DriverStatusPanel) receive live updates by reading diff --git a/src/Server/ZB.MOM.WW.OtOpcUa.AdminUI/Hubs/InMemoryDriverStatusSnapshotStore.cs b/src/Server/ZB.MOM.WW.OtOpcUa.AdminUI/Hubs/InMemoryDriverStatusSnapshotStore.cs index 353fe6de..8dc990cb 100644 --- a/src/Server/ZB.MOM.WW.OtOpcUa.AdminUI/Hubs/InMemoryDriverStatusSnapshotStore.cs +++ b/src/Server/ZB.MOM.WW.OtOpcUa.AdminUI/Hubs/InMemoryDriverStatusSnapshotStore.cs @@ -25,4 +25,7 @@ public sealed class InMemoryDriverStatusSnapshotStore : IDriverStatusSnapshotSto /// public bool TryGet(string driverInstanceId, out DriverHealthChanged snapshot) => _byInstance.TryGetValue(driverInstanceId, out snapshot!); + + /// + public IReadOnlyCollection GetAll() => _byInstance.Values.ToArray(); } diff --git a/tests/Server/ZB.MOM.WW.OtOpcUa.AdminUI.Tests/Hosts/HostsDriverViewTests.cs b/tests/Server/ZB.MOM.WW.OtOpcUa.AdminUI.Tests/Hosts/HostsDriverViewTests.cs new file mode 100644 index 00000000..e72c6bf5 --- /dev/null +++ b/tests/Server/ZB.MOM.WW.OtOpcUa.AdminUI.Tests/Hosts/HostsDriverViewTests.cs @@ -0,0 +1,157 @@ +using Shouldly; +using Xunit; +using ZB.MOM.WW.OtOpcUa.AdminUI.Hosts; +using ZB.MOM.WW.OtOpcUa.Commons.Messages.Drivers; + +namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Hosts; + +/// +/// Covers : the pure, DB-agnostic view-model builder that +/// groups driver-health snapshots by cluster, attaches each cluster's nodes, and enriches each +/// driver row with the configured name + type. The builder is null-safe and stable-ordered so +/// the (later) /hosts Razor section renders deterministically. +/// +public sealed class HostsDriverViewTests +{ + private static readonly DateTime When = new(2026, 6, 18, 0, 0, 0, DateTimeKind.Utc); + + private static DriverHealthChanged Snap(string cluster, string instance, string state = "Healthy") => + new(cluster, instance, state, null, null, 0, When); + + private static HostsNodeInfo Node(string cluster, string node, string host = "h", int port = 4840) => + new(cluster, node, host, port); + + private static HostsDriverInstanceInfo Instance(string instance, string cluster, string name, string type) => + new(instance, cluster, name, type); + + [Fact] + public void Build_groups_two_clusters_with_their_nodes_and_snapshots() + { + var snaps = new[] + { + Snap("MAIN", "drv-a"), + Snap("EDGE", "drv-b"), + }; + var nodes = new[] + { + Node("MAIN", "MAIN-1"), + Node("EDGE", "EDGE-1"), + }; + + var groups = HostsDriverView.Build(snaps, nodes, instances: null); + + groups.Count.ShouldBe(2); + var main = groups.Single(g => g.ClusterId == "MAIN"); + var edge = groups.Single(g => g.ClusterId == "EDGE"); + + main.Nodes.Select(n => n.NodeId).ShouldBe(new[] { "MAIN-1" }); + main.Drivers.Select(d => d.DriverInstanceId).ShouldBe(new[] { "drv-a" }); + edge.Nodes.Select(n => n.NodeId).ShouldBe(new[] { "EDGE-1" }); + edge.Drivers.Select(d => d.DriverInstanceId).ShouldBe(new[] { "drv-b" }); + } + + [Fact] + public void Build_enriches_driver_row_from_matching_instance() + { + var groups = HostsDriverView.Build( + new[] { Snap("MAIN", "drv-a", "Faulted") }, + nodes: null, + new[] { Instance("drv-a", "MAIN", "Pump House Modbus", "Modbus") }); + + var row = groups.Single().Drivers.Single(); + row.DriverInstanceId.ShouldBe("drv-a"); + row.Name.ShouldBe("Pump House Modbus"); + row.DriverType.ShouldBe("Modbus"); + row.State.ShouldBe("Faulted"); + } + + [Fact] + public void Build_leaves_name_and_type_null_for_unknown_driver() + { + var groups = HostsDriverView.Build( + new[] { Snap("MAIN", "orphan") }, + nodes: null, + new[] { Instance("drv-a", "MAIN", "Known", "Modbus") }); + + var row = groups.Single().Drivers.Single(d => d.DriverInstanceId == "orphan"); + row.Name.ShouldBeNull(); + row.DriverType.ShouldBeNull(); + } + + [Fact] + public void Build_includes_cluster_with_nodes_but_no_snapshots() + { + var groups = HostsDriverView.Build( + snapshots: null, + new[] { Node("MAIN", "MAIN-1") }, + instances: null); + + var group = groups.Single(); + group.ClusterId.ShouldBe("MAIN"); + group.Nodes.Count.ShouldBe(1); + group.Drivers.ShouldBeEmpty(); + } + + [Fact] + public void Build_with_empty_inputs_returns_empty_list() + { + HostsDriverView.Build( + Array.Empty(), + Array.Empty(), + Array.Empty()).ShouldBeEmpty(); + } + + [Fact] + public void Build_with_all_null_inputs_returns_empty_list_without_throwing() + { + HostsDriverView.Build(snapshots: null, nodes: null, instances: null).ShouldBeEmpty(); + } + + [Fact] + public void Build_orders_drivers_by_name_then_instance_id() + { + var snaps = new[] + { + Snap("MAIN", "drv-z"), // no instance -> null name -> sorts by id "drv-z" + Snap("MAIN", "drv-a"), // no instance -> null name -> sorts by id "drv-a" + Snap("MAIN", "drv-m"), // named "Alpha" + }; + var instances = new[] + { + Instance("drv-m", "MAIN", "Alpha", "Modbus"), + }; + + var drivers = HostsDriverView.Build(snaps, nodes: null, instances).Single().Drivers; + + // Sort key is (Name ?? DriverInstanceId): "Alpha"(drv-m), "drv-a", "drv-z". + drivers.Select(d => d.DriverInstanceId).ShouldBe(new[] { "drv-m", "drv-a", "drv-z" }); + } + + [Fact] + public void Build_takes_first_when_instance_id_appears_twice() + { + var groups = HostsDriverView.Build( + new[] { Snap("MAIN", "drv-a") }, + nodes: null, + new[] + { + Instance("drv-a", "MAIN", "First", "Modbus"), + Instance("drv-a", "MAIN", "Second", "S7"), + }); + + var row = groups.Single().Drivers.Single(); + row.Name.ShouldBe("First"); + row.DriverType.ShouldBe("Modbus"); + } + + [Fact] + public void Build_orders_clusters_case_insensitively() + { + var groups = HostsDriverView.Build( + new[] { Snap("zeta", "d1"), Snap("Alpha", "d2") }, + new[] { Node("Beta", "b1") }, + instances: null); + + groups.Select(g => g.ClusterId).ShouldBe(new[] { "Alpha", "Beta", "zeta" }); + } +}