Polling chosen over event-driven for initial scope: simpler, matches Admin UI consumer cadence, avoids DriverHost lifecycle-event plumbing that doesn't exist today. Event-driven push for sub-heartbeat latency is a straightforward follow-up. Admin.Services.HostStatusService left-joins DriverHostStatus against ClusterNode on NodeId so rows persist even when the ClusterNode entry doesn't exist yet (first-boot bootstrap case). StaleThreshold = 30s — covers one missed publisher heartbeat plus a generous buffer for clock skew and GC pauses. Admin Components/Pages/Hosts.razor — FleetAdmin-visible page grouped by cluster (handles the '(unassigned)' case for rows without a matching ClusterNode). Four summary cards (Hosts / Running / Stale / Faulted); per-cluster table with Node / Driver / Host / State + Stale-badge / Last-transition / Last-seen / Detail columns; 10s auto-refresh via IServiceScopeFactory timer pattern matching FleetStatusPoller + Fleet dashboard (PR 27). Row-class highlighting: Faulted → table-danger, Stale → table-warning, else default. State badge maps DriverHostState enum to bootstrap color classes. Sidebar link added between 'Fleet status' and 'Clusters'. Server csproj adds Microsoft.EntityFrameworkCore.SqlServer 10.0.0 + registers OtOpcUaConfigDbContext in Program.cs scoped via NodeOptions.ConfigDbConnectionString (no Admin-style manual SQL raw — the DbContext is the only access path, keeps migrations owner-of-record). Tests — HostStatusPublisherTests (4 new Integration cases, uses per-run throwaway DB matching the FleetStatusPollerTests pattern): publisher upserts one row per host from each probe-capable driver and skips non-probe drivers; second tick advances LastSeenUtc without creating duplicate rows (upsert pattern verified end-to-end); state change between ticks updates State AND StateChangedUtc (datetime2(3) rounds to millisecond precision so comparison uses 1ms tolerance — documented inline); MapState translates every HostState enum member. Server.Tests Integration: 4 new tests pass. Admin build clean, Admin.Tests Unit still 23 / 0. docs/v2/lmx-followups.md item #7 marked DONE with three explicit deferred items (event-driven push, failure-count column, SignalR fan-out). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
64 lines
2.8 KiB
C#
64 lines
2.8 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using ZB.MOM.WW.OtOpcUa.Configuration;
|
|
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
|
|
using ZB.MOM.WW.OtOpcUa.Configuration.Enums;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Admin.Services;
|
|
|
|
/// <summary>
|
|
/// One row per <see cref="DriverHostStatus"/> record, enriched with the owning
|
|
/// <c>ClusterNode.ClusterId</c> when available (left-join). The Admin <c>/hosts</c> page
|
|
/// groups by cluster and renders a per-node → per-driver → per-host tree.
|
|
/// </summary>
|
|
public sealed record HostStatusRow(
|
|
string NodeId,
|
|
string? ClusterId,
|
|
string DriverInstanceId,
|
|
string HostName,
|
|
DriverHostState State,
|
|
DateTime StateChangedUtc,
|
|
DateTime LastSeenUtc,
|
|
string? Detail);
|
|
|
|
/// <summary>
|
|
/// Read-side service for the Admin UI's per-host drill-down. Loads
|
|
/// <see cref="DriverHostStatus"/> rows (written by the Server process's
|
|
/// <c>HostStatusPublisher</c>) and left-joins <c>ClusterNode</c> so each row knows which
|
|
/// cluster it belongs to — the Admin UI groups by cluster for the fleet-wide view.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// The publisher heartbeat is 10s (<c>HostStatusPublisher.HeartbeatInterval</c>). The
|
|
/// Admin page also polls every ~10s and treats rows with <c>LastSeenUtc</c> older than
|
|
/// <c>StaleThreshold</c> (30s) as stale — covers a missed heartbeat tolerance plus
|
|
/// a generous buffer for clock skew and publisher GC pauses.
|
|
/// </remarks>
|
|
public sealed class HostStatusService(OtOpcUaConfigDbContext db)
|
|
{
|
|
public static readonly TimeSpan StaleThreshold = TimeSpan.FromSeconds(30);
|
|
|
|
public async Task<IReadOnlyList<HostStatusRow>> ListAsync(CancellationToken ct = default)
|
|
{
|
|
// LEFT JOIN on NodeId so a row persists even when its owning ClusterNode row hasn't
|
|
// been created yet (first-boot bootstrap case — keeps the UI from losing sight of
|
|
// the reporting server).
|
|
var rows = await (from s in db.DriverHostStatuses.AsNoTracking()
|
|
join n in db.ClusterNodes.AsNoTracking()
|
|
on s.NodeId equals n.NodeId into nodeJoin
|
|
from n in nodeJoin.DefaultIfEmpty()
|
|
orderby s.NodeId, s.DriverInstanceId, s.HostName
|
|
select new HostStatusRow(
|
|
s.NodeId,
|
|
n != null ? n.ClusterId : null,
|
|
s.DriverInstanceId,
|
|
s.HostName,
|
|
s.State,
|
|
s.StateChangedUtc,
|
|
s.LastSeenUtc,
|
|
s.Detail)).ToListAsync(ct);
|
|
return rows;
|
|
}
|
|
|
|
public static bool IsStale(HostStatusRow row) =>
|
|
DateTime.UtcNow - row.LastSeenUtc > StaleThreshold;
|
|
}
|