feat(runtime,adminui): real GetDiagnostics from child health + /fleet/diagnostics consumer (A6)

DriverHostActor wraps the health publisher it hands its children in a
ChildHealthLedger (last snapshot per driver id, forwarded to the real
publisher), so GetDiagnostics now answers with each child's row id,
authored name, last-published state, device counts (host statuses when
the driver probes hosts, else one device) and last-change time — instead
of Guid.Empty / "Spawned" / 0 / now. Entries are forgotten on StopChild
and operator Restart.

IFleetDiagnosticsClient gains its first consumer: the new
/fleet/diagnostics page (node selector + ?node= query, revision,
running-from-cache notice, per-driver table). FleetDiagnosticsClient
logs the degrade-to-empty path at Warning.

Review ref: 02/U1.

Refs #731

Claude-Session: https://claude.ai/code/session_01Ub9hGmYLQvn9XETzw7CMsT
This commit is contained in:
Joseph Doherty
2026-10-22 06:44:38 -04:00
parent c6d4b14c93
commit d1cc9e2cfc
6 changed files with 459 additions and 15 deletions
@@ -0,0 +1,79 @@
using Akka.Actor;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Commons.Interfaces;
using ZB.MOM.WW.OtOpcUa.Commons.Messages.Fleet;
using ZB.MOM.WW.OtOpcUa.Commons.Types;
using ZB.MOM.WW.OtOpcUa.Runtime.Tests.Harness;
namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.Drivers;
/// <summary>
/// Review 02 U1: <c>GetDiagnostics</c> answered a placeholder — <c>Guid.Empty</c>, "Spawned", zero devices and
/// "now" for every child, in every behaviour — so the snapshot looked live and said nothing. It now carries
/// each child's row id, authored name, last-published health state, device counts and last change time.
/// </summary>
public sealed class DriverHostActorDiagnosticsTests : RuntimeActorTestBase
{
private static readonly RevisionHash RevA = RevisionHash.Parse(new string('a', 64));
private static readonly NodeId TestNode = NodeId.Parse("node-1:4053");
private const string DriverId = "DI-1";
[Fact]
public async Task A_connected_child_reports_its_row_id_name_health_state_and_device_count()
{
var db = NewInMemoryDbFactory();
var rowId = Guid.NewGuid();
var before = DateTime.UtcNow.AddSeconds(-1);
var factory = new RecordingSubscribableDriverFactory("Modbus");
var deploymentId = V3ArtifactSeeder.SeedV3Deployment(
db, RevA, new[] { (DriverId, "Line1-Modbus", "dev1", "speed", "Float", false) },
Array.Empty<(string, string, string, string, string?)>(),
enabled: true, driverRowIds: new Dictionary<string, Guid> { [DriverId] = rowId });
var (host, _, _) = V3ArtifactSeeder.SpawnHostAndApply(this, db, TestNode, deploymentId, RevA, factory);
var snapshot = await AwaitDiagnosticsAsync(host, s => s.Drivers.Any(d => d.State == "Healthy"));
snapshot.NodeId.ShouldBe(TestNode);
snapshot.CurrentRevision.ShouldBe(RevA);
snapshot.RunningFromCache.ShouldBeFalse();
var driver = snapshot.Drivers.ShouldHaveSingleItem();
driver.DriverInstanceId.ShouldBe(rowId, "the real row id, not Guid.Empty");
driver.Name.ShouldBe("Line1-Modbus", "the authored name, not the instance id");
driver.State.ShouldBe("Healthy");
driver.ConnectedDevices.ShouldBe(1);
driver.FaultedDevices.ShouldBe(0);
driver.LastChangeUtc.ShouldBeGreaterThan(before);
driver.LastChangeUtc.ShouldBeLessThanOrEqualTo(DateTime.UtcNow);
}
[Fact]
public async Task A_stubbed_child_reports_Stubbed_with_no_devices()
{
var db = NewInMemoryDbFactory();
// The factory answers for a type the artifact does not use, so the host stubs the child.
var factory = new RecordingSubscribableDriverFactory("NotModbus");
var deploymentId = V3ArtifactSeeder.SeedV3Deployment(
db, RevA, new[] { (DriverId, "Modbus", "dev1", "speed", "Float", false) },
Array.Empty<(string, string, string, string, string?)>(), enabled: true);
var (host, _, _) = V3ArtifactSeeder.SpawnHostAndApply(this, db, TestNode, deploymentId, RevA, factory);
var snapshot = await AwaitDiagnosticsAsync(host, s => s.Drivers.Count == 1);
var driver = snapshot.Drivers.Single();
driver.State.ShouldBe("Stubbed");
driver.ConnectedDevices.ShouldBe(0);
driver.FaultedDevices.ShouldBe(0);
}
private async Task<NodeDiagnosticsSnapshot> AwaitDiagnosticsAsync(IActorRef host, Func<NodeDiagnosticsSnapshot, bool> ready)
{
NodeDiagnosticsSnapshot? snapshot = null;
await AwaitAssertAsync(async () =>
{
snapshot = await host.Ask<NodeDiagnosticsSnapshot>(new GetDiagnostics(CorrelationId.NewId()), PresenceBudget);
ready(snapshot).ShouldBeTrue();
}, PresenceBudget);
return snapshot!;
}
}
@@ -47,6 +47,8 @@ public static class V3ArtifactSeeder
/// is parsed out of <c>TagConfig.alarm</c> by <c>TagConfigIntent</c>), which the alarm-ref half of the
/// restart suite needs — an alarm-bearing raw tag is excluded from the VALUE subscription set and carried
/// in the alarm one instead, so the two travel through different child code paths.</param>
/// <param name="driverRowIds">Optional <c>DriverInstanceRowId</c> (the DB row Guid) per driver id; absent
/// ids get <see cref="Guid.Empty"/>, which is what an artifact without the property parses to.</param>
/// <param name="taglessDrivers">Extra <c>DriverInstance</c> ids carrying NO devices and NO tags. The
/// only way to express a driver that is a running child yet absent from both the value- and alarm-ref
/// maps — the <c>_children.Keys</c> arm of <c>ApplyDesiredSubscriptions</c>'s union, which must still
@@ -58,14 +60,16 @@ public static class V3ArtifactSeeder
(string Area, string Line, string Equip, string BackingTag, string? Effective)[] unsRefs,
bool enabled = false,
IReadOnlyDictionary<string, string>? tagConfigByTag = null,
string[]? taglessDrivers = null)
string[]? taglessDrivers = null,
IReadOnlyDictionary<string, Guid>? driverRowIds = null)
{
Guid RowId(string id) => driverRowIds is not null && driverRowIds.TryGetValue(id, out var g) ? g : Guid.Empty;
var drivers = rawTags
.Select(t => (t.Driver, t.DriverName))
.Distinct()
.Select(d => new { DriverInstanceId = d.Driver, RawFolderId = "rf-plant", Name = d.DriverName, DriverType = "Modbus", DriverConfig = "{}", ClusterId = "c1", Enabled = enabled })
.Select(d => new { DriverInstanceRowId = RowId(d.Driver), DriverInstanceId = d.Driver, RawFolderId = "rf-plant", Name = d.DriverName, DriverType = "Modbus", DriverConfig = "{}", ClusterId = "c1", Enabled = enabled })
.Concat((taglessDrivers ?? Array.Empty<string>())
.Select(id => new { DriverInstanceId = id, RawFolderId = "rf-plant", Name = id, DriverType = "Modbus", DriverConfig = "{}", ClusterId = "c1", Enabled = enabled }))
.Select(id => new { DriverInstanceRowId = RowId(id), DriverInstanceId = id, RawFolderId = "rf-plant", Name = id, DriverType = "Modbus", DriverConfig = "{}", ClusterId = "c1", Enabled = enabled }))
.ToArray();
var devices = rawTags
.Select(t => (t.Driver, t.Device))