feat(adminui): driver-snapshot GetAll() + pure Hosts driver-view builder (#8)
This commit is contained in:
@@ -0,0 +1,124 @@
|
|||||||
|
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Hosts;
|
||||||
|
|
||||||
|
using ZB.MOM.WW.OtOpcUa.Commons.Messages.Drivers;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// One configured host node within a cluster, as the <c>/hosts</c> page needs it: the cluster
|
||||||
|
/// it belongs to, its node id, and the host/port its OPC UA endpoint binds.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="ClusterId">Cluster this node belongs to.</param>
|
||||||
|
/// <param name="NodeId">Stable node identifier within the cluster.</param>
|
||||||
|
/// <param name="Host">Host name or address the node's OPC UA server binds.</param>
|
||||||
|
/// <param name="OpcUaPort">OPC UA TCP port the node listens on.</param>
|
||||||
|
public sealed record HostsNodeInfo(string ClusterId, string NodeId, string Host, int OpcUaPort);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 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.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="DriverInstanceId">Globally-unique driver instance id (joins to a snapshot).</param>
|
||||||
|
/// <param name="ClusterId">Cluster the instance is configured under.</param>
|
||||||
|
/// <param name="Name">Authored display name.</param>
|
||||||
|
/// <param name="DriverType">Driver type string (Modbus, S7, Galaxy, ...).</param>
|
||||||
|
public sealed record HostsDriverInstanceInfo(string DriverInstanceId, string ClusterId, string Name, string DriverType);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// One row in the <c>/hosts</c> "Driver Instances" table: a live health snapshot enriched with
|
||||||
|
/// the configured name + type (both null when no matching instance is configured).
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="DriverInstanceId">Globally-unique driver instance id.</param>
|
||||||
|
/// <param name="Name">Configured display name; null when unknown.</param>
|
||||||
|
/// <param name="DriverType">Configured driver type; null when unknown.</param>
|
||||||
|
/// <param name="State">Last-known driver state string (Healthy, Faulted, Reconnecting, ...).</param>
|
||||||
|
/// <param name="LastSuccessfulReadUtc">Most recent successful equipment read; null if never.</param>
|
||||||
|
/// <param name="LastError">Latest error message; null when none.</param>
|
||||||
|
/// <param name="ErrorCount5Min">Faulted-transition count in the last 5 minutes.</param>
|
||||||
|
/// <param name="PublishedUtc">Timestamp the snapshot was published.</param>
|
||||||
|
public sealed record HostsDriverRow(
|
||||||
|
string DriverInstanceId, string? Name, string? DriverType, string State,
|
||||||
|
DateTime? LastSuccessfulReadUtc, string? LastError, int ErrorCount5Min, DateTime PublishedUtc);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// One cluster's section on the <c>/hosts</c> page: its configured nodes plus its enriched
|
||||||
|
/// driver-instance rows.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="ClusterId">Cluster identifier.</param>
|
||||||
|
/// <param name="Nodes">Configured nodes in this cluster, ordered by node id.</param>
|
||||||
|
/// <param name="Drivers">Driver rows in this cluster, ordered by name then instance id.</param>
|
||||||
|
public sealed record HostsClusterGroup(string ClusterId, IReadOnlyList<HostsNodeInfo> Nodes, IReadOnlyList<HostsDriverRow> Drivers);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Pure, DB-agnostic builder for the <c>/hosts</c> "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.
|
||||||
|
/// </summary>
|
||||||
|
public static class HostsDriverView
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Builds the per-cluster view model. Clusters are the case-insensitive union of cluster ids
|
||||||
|
/// seen across <paramref name="snapshots"/> and <paramref name="nodes"/>, ordered
|
||||||
|
/// case-insensitively. Each group carries its nodes (ordered by node id) and its driver rows
|
||||||
|
/// (snapshots enriched by joining <paramref name="instances"/> on driver instance id;
|
||||||
|
/// unmatched ids yield null name/type), ordered by name-then-instance-id.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="snapshots">Live health snapshots; null treated as empty.</param>
|
||||||
|
/// <param name="nodes">Configured cluster nodes; null treated as empty.</param>
|
||||||
|
/// <param name="instances">Configured driver instances for enrichment; null treated as empty.</param>
|
||||||
|
/// <returns>One <see cref="HostsClusterGroup"/> per cluster, ordered by cluster id.</returns>
|
||||||
|
public static IReadOnlyList<HostsClusterGroup> Build(
|
||||||
|
IEnumerable<DriverHealthChanged>? snapshots,
|
||||||
|
IEnumerable<HostsNodeInfo>? nodes,
|
||||||
|
IEnumerable<HostsDriverInstanceInfo>? instances)
|
||||||
|
{
|
||||||
|
var snapList = (snapshots ?? Enumerable.Empty<DriverHealthChanged>()).ToList();
|
||||||
|
var nodeList = (nodes ?? Enumerable.Empty<HostsNodeInfo>()).ToList();
|
||||||
|
var instanceList = instances ?? Enumerable.Empty<HostsDriverInstanceInfo>();
|
||||||
|
|
||||||
|
// First-wins lookup keyed by driver instance id.
|
||||||
|
var byInstance = new Dictionary<string, HostsDriverInstanceInfo>(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<HostsClusterGroup>(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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -14,6 +14,9 @@ public interface IDriverStatusSnapshotStore
|
|||||||
void Upsert(DriverHealthChanged snapshot);
|
void Upsert(DriverHealthChanged snapshot);
|
||||||
bool TryGet(string driverInstanceId, out DriverHealthChanged snapshot);
|
bool TryGet(string driverInstanceId, out DriverHealthChanged snapshot);
|
||||||
|
|
||||||
|
/// <summary>Returns a point-in-time snapshot of every driver instance's last-known health.</summary>
|
||||||
|
IReadOnlyCollection<DriverHealthChanged> GetAll();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Raised after every <see cref="Upsert"/> with the just-stored snapshot. Lets in-process
|
/// Raised after every <see cref="Upsert"/> with the just-stored snapshot. Lets in-process
|
||||||
/// consumers (the Blazor Server <c>DriverStatusPanel</c>) receive live updates by reading
|
/// consumers (the Blazor Server <c>DriverStatusPanel</c>) receive live updates by reading
|
||||||
|
|||||||
@@ -25,4 +25,7 @@ public sealed class InMemoryDriverStatusSnapshotStore : IDriverStatusSnapshotSto
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public bool TryGet(string driverInstanceId, out DriverHealthChanged snapshot)
|
public bool TryGet(string driverInstanceId, out DriverHealthChanged snapshot)
|
||||||
=> _byInstance.TryGetValue(driverInstanceId, out snapshot!);
|
=> _byInstance.TryGetValue(driverInstanceId, out snapshot!);
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public IReadOnlyCollection<DriverHealthChanged> GetAll() => _byInstance.Values.ToArray();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Covers <see cref="HostsDriverView.Build"/>: 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) <c>/hosts</c> Razor section renders deterministically.
|
||||||
|
/// </summary>
|
||||||
|
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<DriverHealthChanged>(),
|
||||||
|
Array.Empty<HostsNodeInfo>(),
|
||||||
|
Array.Empty<HostsDriverInstanceInfo>()).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" });
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user