feat(uns): IUnsTreeService structural load + DI registration
This commit is contained in:
@@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Components.Web;
|
||||
using Microsoft.AspNetCore.Routing;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using ZB.MOM.WW.OtOpcUa.AdminUI.Hubs;
|
||||
using ZB.MOM.WW.OtOpcUa.AdminUI.Uns;
|
||||
using ZB.MOM.WW.OtOpcUa.Commons.Browsing;
|
||||
using ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Browser;
|
||||
using ZB.MOM.WW.OtOpcUa.Driver.OpcUaClient.Browser;
|
||||
@@ -42,6 +43,7 @@ public static class EndpointRouteBuilderExtensions
|
||||
services.AddSingleton<Browsing.BrowseSessionRegistry>();
|
||||
services.AddHostedService<Browsing.BrowseSessionReaper>();
|
||||
services.AddScoped<Browsing.IBrowserSessionService, Browsing.BrowserSessionService>();
|
||||
services.AddScoped<IUnsTreeService, UnsTreeService>();
|
||||
services.AddSingleton<IDriverBrowser, OpcUaClientDriverBrowser>();
|
||||
services.AddSingleton<IDriverBrowser, GalaxyDriverBrowser>();
|
||||
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Uns;
|
||||
|
||||
/// <summary>
|
||||
/// Loads the structural portion of the unified-namespace (UNS) browse tree —
|
||||
/// Enterprise → Cluster → Area → Line → Equipment — from the config database.
|
||||
/// Equipment children (tags/virtual tags) are summarised by count only and loaded
|
||||
/// lazily by the renderer; this service never returns Tag/VirtualTag leaf nodes.
|
||||
/// </summary>
|
||||
public interface IUnsTreeService
|
||||
{
|
||||
/// <summary>
|
||||
/// Loads the full structural tree. Empty clusters are retained so they remain
|
||||
/// visible and editable. The returned nodes are detached view-models, safe to
|
||||
/// hold and mutate UI state on after the underlying context is disposed.
|
||||
/// </summary>
|
||||
/// <param name="ct">A token to cancel the load.</param>
|
||||
/// <returns>The enterprise root nodes, each populated down to equipment.</returns>
|
||||
Task<IReadOnlyList<UnsNode>> LoadStructureAsync(CancellationToken ct = default);
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Uns;
|
||||
|
||||
/// <summary>
|
||||
/// Default <see cref="IUnsTreeService"/>. Reads the structural rows with a handful of
|
||||
/// untracked queries, computes per-equipment tag/virtual-tag counts, and hands the flat
|
||||
/// rows to the pure <see cref="UnsTreeAssembly.Build"/> to nest into the browse tree.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// A new context is created per call via the pooled factory — the same pattern every
|
||||
/// AdminUI page uses — so the service is safe to register as a scoped singleton and call
|
||||
/// concurrently from independent Blazor circuits.
|
||||
/// </remarks>
|
||||
public sealed class UnsTreeService(IDbContextFactory<OtOpcUaConfigDbContext> dbFactory) : IUnsTreeService
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public async Task<IReadOnlyList<UnsNode>> LoadStructureAsync(CancellationToken ct = default)
|
||||
{
|
||||
await using var db = await dbFactory.CreateDbContextAsync(ct);
|
||||
|
||||
var clusters = await db.ServerClusters
|
||||
.AsNoTracking()
|
||||
.Select(c => new ClusterRow(c.ClusterId, c.Enterprise, c.Site, c.Name))
|
||||
.ToListAsync(ct);
|
||||
|
||||
var areas = await db.UnsAreas
|
||||
.AsNoTracking()
|
||||
.Select(a => new AreaRow(a.UnsAreaId, a.ClusterId, a.Name))
|
||||
.ToListAsync(ct);
|
||||
|
||||
var lines = await db.UnsLines
|
||||
.AsNoTracking()
|
||||
.Select(l => new LineRow(l.UnsLineId, l.UnsAreaId, l.Name))
|
||||
.ToListAsync(ct);
|
||||
|
||||
var equipmentRows = await db.Equipment
|
||||
.AsNoTracking()
|
||||
.Select(e => new
|
||||
{
|
||||
e.EquipmentId,
|
||||
e.UnsLineId,
|
||||
e.MachineCode,
|
||||
e.Name,
|
||||
})
|
||||
.ToListAsync(ct);
|
||||
|
||||
// Per-equipment driver-tag counts (tags with no equipment are excluded).
|
||||
var tagCounts = (await db.Tags
|
||||
.AsNoTracking()
|
||||
.Where(t => t.EquipmentId != null)
|
||||
.GroupBy(t => t.EquipmentId)
|
||||
.Select(g => new { EquipmentId = g.Key!, Count = g.Count() })
|
||||
.ToListAsync(ct))
|
||||
.ToDictionary(x => x.EquipmentId, x => x.Count, StringComparer.Ordinal);
|
||||
|
||||
// Per-equipment virtual-tag counts (EquipmentId is always set on virtual tags).
|
||||
var vtagCounts = (await db.VirtualTags
|
||||
.AsNoTracking()
|
||||
.GroupBy(v => v.EquipmentId)
|
||||
.Select(g => new { EquipmentId = g.Key, Count = g.Count() })
|
||||
.ToListAsync(ct))
|
||||
.ToDictionary(x => x.EquipmentId, x => x.Count, StringComparer.Ordinal);
|
||||
|
||||
var equipment = equipmentRows
|
||||
.Select(e => new EquipmentRow(
|
||||
e.EquipmentId,
|
||||
e.UnsLineId,
|
||||
e.MachineCode,
|
||||
e.Name,
|
||||
tagCounts.GetValueOrDefault(e.EquipmentId),
|
||||
vtagCounts.GetValueOrDefault(e.EquipmentId)))
|
||||
.ToList();
|
||||
|
||||
return UnsTreeAssembly.Build(clusters, areas, lines, equipment);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using ZB.MOM.WW.OtOpcUa.AdminUI.Uns;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Uns;
|
||||
|
||||
/// <summary>
|
||||
/// Verifies <see cref="UnsTreeService.LoadStructureAsync"/> builds the Enterprise→Cluster→
|
||||
/// Area→Line→Equipment structure from the config database, including per-equipment tag/
|
||||
/// virtual-tag counts and the retention of empty clusters.
|
||||
/// </summary>
|
||||
[Trait("Category", "Unit")]
|
||||
public sealed class UnsTreeServiceStructureTests
|
||||
{
|
||||
private static UnsTreeService SeededService()
|
||||
{
|
||||
var dbName = $"uns-{Guid.NewGuid():N}";
|
||||
UnsTreeTestDb.SeedNamed(dbName);
|
||||
return new UnsTreeService(UnsTreeTestDb.Factory(dbName));
|
||||
}
|
||||
|
||||
/// <summary>Root enterprise "zb" carries both clusters, and MAIN exposes the full
|
||||
/// area→line→equipment path with the expected kinds and keys.</summary>
|
||||
[Fact]
|
||||
public async Task LoadStructure_builds_full_hierarchy()
|
||||
{
|
||||
var service = SeededService();
|
||||
|
||||
var roots = await service.LoadStructureAsync();
|
||||
|
||||
var enterprise = roots.ShouldHaveSingleItem();
|
||||
enterprise.Kind.ShouldBe(UnsNodeKind.Enterprise);
|
||||
enterprise.DisplayName.ShouldBe("zb");
|
||||
enterprise.Children.Count.ShouldBe(2);
|
||||
enterprise.Children.ShouldAllBe(c => c.Kind == UnsNodeKind.Cluster);
|
||||
|
||||
var main = enterprise.Children.Single(c => c.ClusterId == UnsTreeTestDb.PopulatedClusterId);
|
||||
main.Kind.ShouldBe(UnsNodeKind.Cluster);
|
||||
|
||||
var area = main.Children.ShouldHaveSingleItem();
|
||||
area.Kind.ShouldBe(UnsNodeKind.Area);
|
||||
area.Key.ShouldBe("area:AREA-1");
|
||||
area.EntityId.ShouldBe("AREA-1");
|
||||
|
||||
var line = area.Children.ShouldHaveSingleItem();
|
||||
line.Kind.ShouldBe(UnsNodeKind.Line);
|
||||
line.Key.ShouldBe("line:LINE-1");
|
||||
line.EntityId.ShouldBe("LINE-1");
|
||||
|
||||
var equipment = line.Children.ShouldHaveSingleItem();
|
||||
equipment.Kind.ShouldBe(UnsNodeKind.Equipment);
|
||||
equipment.Key.ShouldBe($"eq:{UnsTreeTestDb.SeededEquipmentId}");
|
||||
equipment.EntityId.ShouldBe(UnsTreeTestDb.SeededEquipmentId);
|
||||
equipment.ClusterId.ShouldBe(UnsTreeTestDb.PopulatedClusterId);
|
||||
}
|
||||
|
||||
/// <summary>The seeded equipment node's badge count equals tags + virtual tags and it is
|
||||
/// flagged as lazily expandable.</summary>
|
||||
[Fact]
|
||||
public async Task LoadStructure_counts_tags_and_vtags_per_equipment()
|
||||
{
|
||||
var service = SeededService();
|
||||
|
||||
var roots = await service.LoadStructureAsync();
|
||||
|
||||
var equipment = roots
|
||||
.Single()
|
||||
.Children.Single(c => c.ClusterId == UnsTreeTestDb.PopulatedClusterId)
|
||||
.Children.Single()
|
||||
.Children.Single()
|
||||
.Children.Single();
|
||||
|
||||
// Seed: 2 driver tags + 1 virtual tag (the orphan tag has no equipment and is excluded).
|
||||
equipment.ChildCount.ShouldBe(3);
|
||||
equipment.HasLazyChildren.ShouldBeTrue();
|
||||
}
|
||||
|
||||
/// <summary>An empty cluster (no areas) is still rendered as a Cluster node with no children.</summary>
|
||||
[Fact]
|
||||
public async Task LoadStructure_includes_empty_clusters()
|
||||
{
|
||||
var service = SeededService();
|
||||
|
||||
var roots = await service.LoadStructureAsync();
|
||||
|
||||
var siteA = roots
|
||||
.Single()
|
||||
.Children.Single(c => c.ClusterId == UnsTreeTestDb.EmptyClusterId);
|
||||
|
||||
siteA.Kind.ShouldBe(UnsNodeKind.Cluster);
|
||||
siteA.Children.ShouldBeEmpty();
|
||||
siteA.ChildCount.ShouldBe(0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
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.AdminUI.Tests.Uns;
|
||||
|
||||
/// <summary>
|
||||
/// Shared in-memory fixture for <c>UnsTreeService</c> structural tests. Builds an
|
||||
/// <see cref="OtOpcUaConfigDbContext"/> over a named InMemory database and seeds a small,
|
||||
/// deterministic UNS hierarchy: enterprise "zb" across two clusters (MAIN populated,
|
||||
/// SITE-A intentionally empty), plus tags and a virtual tag on one equipment node so
|
||||
/// the per-equipment count joins can be exercised.
|
||||
/// </summary>
|
||||
internal static class UnsTreeTestDb
|
||||
{
|
||||
/// <summary>The equipment that carries the seeded tags and virtual tag.</summary>
|
||||
public const string SeededEquipmentId = "EQ-000000000001";
|
||||
|
||||
/// <summary>The cluster that has no areas, used to cover the empty-cluster case.</summary>
|
||||
public const string EmptyClusterId = "SITE-A";
|
||||
|
||||
/// <summary>The populated cluster with the area→line→equipment path.</summary>
|
||||
public const string PopulatedClusterId = "MAIN";
|
||||
|
||||
/// <summary>Creates a context over a fresh, uniquely-named InMemory database.</summary>
|
||||
public static OtOpcUaConfigDbContext Create() => CreateNamed($"uns-{Guid.NewGuid():N}");
|
||||
|
||||
/// <summary>Creates a context bound to the supplied InMemory database name.</summary>
|
||||
public static OtOpcUaConfigDbContext CreateNamed(string name) =>
|
||||
new(new DbContextOptionsBuilder<OtOpcUaConfigDbContext>()
|
||||
.UseInMemoryDatabase(name)
|
||||
.Options);
|
||||
|
||||
/// <summary>
|
||||
/// Returns an <see cref="IDbContextFactory{TContext}"/> whose contexts all share the
|
||||
/// supplied InMemory database name, so data seeded by <see cref="SeedNamed"/> is visible
|
||||
/// to the service under test.
|
||||
/// </summary>
|
||||
public static IDbContextFactory<OtOpcUaConfigDbContext> Factory(string name) => new NamedFactory(name);
|
||||
|
||||
/// <summary>Seeds the fixture into the supplied (already-bound) context and saves.</summary>
|
||||
public static void Seed(OtOpcUaConfigDbContext db)
|
||||
{
|
||||
// Two clusters under the same enterprise; only MAIN gets a hierarchy.
|
||||
db.ServerClusters.Add(new ServerCluster
|
||||
{
|
||||
ClusterId = PopulatedClusterId,
|
||||
Name = "Main",
|
||||
Enterprise = "zb",
|
||||
Site = "warsaw-west",
|
||||
RedundancyMode = RedundancyMode.None,
|
||||
CreatedBy = "test",
|
||||
});
|
||||
db.ServerClusters.Add(new ServerCluster
|
||||
{
|
||||
ClusterId = EmptyClusterId,
|
||||
Name = "Site A",
|
||||
Enterprise = "zb",
|
||||
Site = "site-a",
|
||||
RedundancyMode = RedundancyMode.None,
|
||||
CreatedBy = "test",
|
||||
});
|
||||
|
||||
// MAIN: one area → one line → one equipment.
|
||||
db.UnsAreas.Add(new UnsArea
|
||||
{
|
||||
UnsAreaId = "AREA-1",
|
||||
ClusterId = PopulatedClusterId,
|
||||
Name = "assembly",
|
||||
});
|
||||
db.UnsLines.Add(new UnsLine
|
||||
{
|
||||
UnsLineId = "LINE-1",
|
||||
UnsAreaId = "AREA-1",
|
||||
Name = "line-a",
|
||||
});
|
||||
db.Equipment.Add(new Equipment
|
||||
{
|
||||
EquipmentId = SeededEquipmentId,
|
||||
EquipmentUuid = Guid.NewGuid(),
|
||||
UnsLineId = "LINE-1",
|
||||
Name = "machine-1",
|
||||
MachineCode = "machine_001",
|
||||
});
|
||||
|
||||
// Two driver tags + one virtual tag on the seeded equipment → ChildCount 3.
|
||||
db.Tags.Add(new Tag
|
||||
{
|
||||
TagId = "TAG-1",
|
||||
DriverInstanceId = "DRV-1",
|
||||
EquipmentId = SeededEquipmentId,
|
||||
Name = "speed",
|
||||
DataType = "Float",
|
||||
AccessLevel = TagAccessLevel.Read,
|
||||
TagConfig = "{}",
|
||||
});
|
||||
db.Tags.Add(new Tag
|
||||
{
|
||||
TagId = "TAG-2",
|
||||
DriverInstanceId = "DRV-1",
|
||||
EquipmentId = SeededEquipmentId,
|
||||
Name = "running",
|
||||
DataType = "Boolean",
|
||||
AccessLevel = TagAccessLevel.Read,
|
||||
TagConfig = "{}",
|
||||
});
|
||||
// A tag with no equipment must be ignored by the count query.
|
||||
db.Tags.Add(new Tag
|
||||
{
|
||||
TagId = "TAG-ORPHAN",
|
||||
DriverInstanceId = "DRV-1",
|
||||
EquipmentId = null,
|
||||
Name = "orphan",
|
||||
DataType = "Int32",
|
||||
AccessLevel = TagAccessLevel.Read,
|
||||
TagConfig = "{}",
|
||||
});
|
||||
db.VirtualTags.Add(new VirtualTag
|
||||
{
|
||||
VirtualTagId = "VTAG-1",
|
||||
EquipmentId = SeededEquipmentId,
|
||||
Name = "computed",
|
||||
DataType = "Double",
|
||||
ScriptId = "SCRIPT-1",
|
||||
});
|
||||
|
||||
db.SaveChanges();
|
||||
}
|
||||
|
||||
/// <summary>Seeds the fixture into a context bound to the supplied InMemory database name.</summary>
|
||||
public static void SeedNamed(string name)
|
||||
{
|
||||
using var db = CreateNamed(name);
|
||||
Seed(db);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Minimal <see cref="IDbContextFactory{TContext}"/> that hands back contexts sharing a
|
||||
/// single InMemory database name — the test-side stand-in for the runtime pooled factory.
|
||||
/// </summary>
|
||||
private sealed class NamedFactory(string name) : IDbContextFactory<OtOpcUaConfigDbContext>
|
||||
{
|
||||
public OtOpcUaConfigDbContext CreateDbContext() => CreateNamed(name);
|
||||
|
||||
public Task<OtOpcUaConfigDbContext> CreateDbContextAsync(CancellationToken cancellationToken = default) =>
|
||||
Task.FromResult(CreateNamed(name));
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="xunit"/>
|
||||
<PackageReference Include="Shouldly"/>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory"/>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk"/>
|
||||
<PackageReference Include="xunit.runner.visualstudio">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
|
||||
Reference in New Issue
Block a user