Merge AdminUI follow-up: mapping Local parent becomes a cluster-scoped container tree picker; a realm switch clears the stale root/parent/remote root (#1090)

This commit is contained in:
Joseph Doherty
2026-12-04 14:32:19 -05:00
2 changed files with 76 additions and 35 deletions
@@ -115,7 +115,7 @@ else
<section class="panel mt-3"><h2 class="panel-head">Export draft</h2><div class="p-3">
<label class="form-label" for="export-id">Export ID</label><input id="export-id" class="form-control mb-2" @bind="e.DataExportId" disabled="@(e.DataExportRowId != Guid.Empty)" />
<label class="form-label" for="principal">Peer principal</label><input id="principal" class="form-control mb-2" @bind="e.PrincipalId" />
<label class="form-label" for="export-realm">Realm</label><select id="export-realm" class="form-select mb-2" @bind="e.Realm"><option value="@DataRealm.Raw">Raw</option><option value="@DataRealm.Uns">UNS</option></select>
<label class="form-label" for="export-realm">Realm</label><select id="export-realm" class="form-select mb-2" @bind="e.Realm" @bind:after="() => ClearExportRoot(e)"><option value="@DataRealm.Raw">Raw</option><option value="@DataRealm.Uns">UNS</option></select>
<label class="form-label" for="export-root">Local export root</label><div class="d-flex gap-2 align-items-center mb-2"><input id="export-root" class="form-control mono" readonly value="@(string.IsNullOrEmpty(e.RootKind) ? "No root chosen" : e.RootKind + " " + e.RootEntityId)" /><button type="button" class="btn btn-outline-secondary text-nowrap" id="export-root-browse" @onclick="() => OpenPicker(RootPicker.Export)">Browse…</button></div><div class="form-text">Or type the root as <span class="mono">Kind|EntityId</span> (e.g. <span class="mono">RawTag|tag-id</span>).</div><label class="form-label" for="export-root-id">Export root (Kind|EntityId)</label><input id="export-root-id" class="form-control mb-2" value="@(e.RootKind + "|" + e.RootEntityId)" @onchange="args => SetExportRoot(e, args.Value?.ToString())" />
@Operations(e.AllowedOperations, v => e.AllowedOperations = v)
<label class="form-label" for="export-history-reads">Concurrent history reads per peer</label><input id="export-history-reads" type="number" min="1" max="@DataConnectionValidator.MaxConcurrentHistoryReadsLimit" class="form-control mb-2" @bind="e.MaxConcurrentHistoryReads" />
@@ -132,8 +132,8 @@ else
<section class="panel mt-3"><h2 class="panel-head">Mapping draft</h2><div class="p-3">
<label class="form-label" for="mapping-id">Mapping ID</label><input id="mapping-id" class="form-control mb-2" @bind="m.RemoteNamespaceMappingId" disabled="@(m.RemoteNamespaceMappingRowId != Guid.Empty)" />
<label class="form-label" for="mapping-connection">Connection</label><select id="mapping-connection" class="form-select mb-2" @bind="m.DataConnectionId"><option value="">Choose a connection</option>@foreach (var c in Connections) { <option value="@c.DataConnectionId">@c.DataConnectionId</option> }</select>
<label class="form-label" for="mapping-realm">Realm</label><select id="mapping-realm" class="form-select mb-2" @bind="m.Realm" @bind:after="() => SetRealm(m)"><option value="@DataRealm.Raw">Raw</option><option value="@DataRealm.Uns">UNS</option></select>
<label class="form-label" for="mapping-parent">Local parent</label><select id="mapping-parent" class="form-select mb-2" value="@(m.LocalParentKind + "|" + m.LocalParentId)" @onchange="args => SetParent(m, args.Value?.ToString())"><option value="">Choose a parent</option>@foreach (var p in Parents(m.Realm)) { <option value="@p.Value">@p.Label</option> }</select>
<label class="form-label" for="mapping-realm">Realm</label><select id="mapping-realm" class="form-select mb-2" @bind="m.Realm" @bind:after="() => MappingRealmChanged(m)"><option value="@DataRealm.Raw">Raw</option><option value="@DataRealm.Uns">UNS</option></select>
<label class="form-label" for="mapping-parent">Local parent</label><div class="d-flex gap-2 align-items-center mb-2"><input id="mapping-parent" class="form-control mono" readonly value="@(string.IsNullOrEmpty(m.LocalParentKind) ? "No parent chosen" : m.LocalParentKind + " " + m.LocalParentId)" /><button type="button" class="btn btn-outline-secondary text-nowrap" id="mapping-parent-browse" @onclick="() => OpenPicker(RootPicker.Parent)">Browse…</button></div>
<label class="form-label" for="branch">Branch name</label><input id="branch" class="form-control mb-2" @bind="m.BranchName" />
<label class="form-label" for="remote-root">Remote root entity</label><div class="d-flex gap-2 align-items-center mb-2"><input id="remote-root" class="form-control mono" readonly value="@(string.IsNullOrEmpty(m.RemoteRootEntityId) ? "No root chosen" : m.RemoteRootEntityId)" /><button type="button" class="btn btn-outline-secondary text-nowrap" id="remote-root-browse" disabled="@string.IsNullOrEmpty(RemoteTargetCluster(m))" @onclick="() => OpenPicker(RootPicker.Remote)">Browse…</button></div><div class="form-text">The picker browses the configured target cluster. For a separately managed cluster, enter its root ID below.</div><label class="form-label" for="remote-root-id">Remote root ID</label><input id="remote-root-id" class="form-control mb-2" @bind="m.RemoteRootEntityId" />
<label class="form-label" for="identifier-kind">Remote canonical identifier kind</label><input id="identifier-kind" class="form-control mb-2" @bind="m.RemoteIdentifierKind" />
@@ -148,8 +148,8 @@ else
<div><label><input type="checkbox" @bind="m.WriteThrough" /> Allow write-through</label></div><label><input type="checkbox" @bind="m.Enabled" /> Enabled in next deployment</label>@Actions(m, m.RemoteNamespaceMappingRowId != Guid.Empty)
</div></section>
}
<DataRootPickerModal Visible="@(_picker != RootPicker.None && (_picker == RootPicker.Export ? _export is not null : _mapping is not null))" Title="@(_picker == RootPicker.Export ? "Choose export root" : "Choose remote root")"
Hint="@(_picker == RootPicker.Export ? "Pick the node this export publishes. The tree is scoped to this cluster." : "Pick the remote node to mirror. The tree is scoped to the connection's target cluster.")"
<DataRootPickerModal Visible="@(_picker != RootPicker.None && (_picker == RootPicker.Export ? _export is not null : _mapping is not null))" Title="@(_picker switch { RootPicker.Export => "Choose export root", RootPicker.Parent => "Choose local parent", _ => "Choose remote root" })"
Hint="@(_picker switch { RootPicker.Export => "Pick the node this export publishes. The tree is scoped to this cluster.", RootPicker.Parent => "Pick the local node the remote data is placed under. The tree is scoped to this cluster and shows containers only.", _ => "Pick the remote node to mirror. The tree is scoped to the connection's target cluster." })"
Realm="@PickerRealm" RawRoot="@PickerRaw" UnsRoot="@PickerUns" CurrentKind="@PickerKind" CurrentId="@PickerId"
OnPicked="PickRoot" OnCancel="() => _picker = RootPicker.None" />
</AuthorizeView>
@@ -201,36 +201,14 @@ else
private static void Toggle(List<string> ids, string id, bool on) { if (on && !ids.Contains(id)) ids.Add(id); else if (!on) ids.Remove(id); }
private static void TargetChanged(DataConnection c) { if (c.TargetMode == DataTargetMode.Cluster) c.ExpectedNodeId = null; else c.EligibleTargetNodeIds.Clear(); }
private static void SetRealm(RemoteNamespaceMapping m) { m.RemoteRealm = m.Realm; m.RemoteNamespaceUri = m.Realm == DataRealm.Raw ? V3NodeIds.RawNamespaceUri : V3NodeIds.UnsNamespaceUri; }
private static void SetParent(RemoteNamespaceMapping m, string? value) { var p = (value ?? "").Split('|', 2); m.LocalParentKind = p[0]; m.LocalParentId = p.Length > 1 ? p[1] : ""; }
private IEnumerable<(string Value, string Label)> Parents(DataRealm realm)
{
yield return ($"Cluster|{ClusterId}", "Cluster root");
if (realm == DataRealm.Raw)
{
foreach (var x in _snapshot!.RawFolders.Where(x => Same(x.ClusterId, ClusterId))) yield return ($"RawFolder|{x.RawFolderId}", $"Folder: {x.Name} ({x.RawFolderId})");
foreach (var x in _snapshot.DriverInstances.Where(x => Same(x.ClusterId, ClusterId))) yield return ($"RawDriver|{x.DriverInstanceId}", $"Driver: {x.Name} ({x.DriverInstanceId})");
var drivers = _snapshot.DriverInstances.Where(x => Same(x.ClusterId, ClusterId)).Select(x => x.DriverInstanceId).ToHashSet(StringComparer.OrdinalIgnoreCase);
var devices = _snapshot.Devices.Where(x => drivers.Contains(x.DriverInstanceId)).ToList();
foreach (var x in devices) yield return ($"RawDevice|{x.DeviceId}", $"Device: {x.Name} ({x.DeviceId})");
var ids = devices.Select(x => x.DeviceId).ToHashSet(StringComparer.OrdinalIgnoreCase);
foreach (var x in _snapshot.TagGroups.Where(x => ids.Contains(x.DeviceId))) yield return ($"RawTagGroup|{x.TagGroupId}", $"Tag group: {x.Name} ({x.TagGroupId})");
}
else if (realm == DataRealm.Uns)
{
var areas = _snapshot!.UnsAreas.Where(x => Same(x.ClusterId, ClusterId)).ToList();
foreach (var x in areas) yield return ($"UnsArea|{x.UnsAreaId}", $"Area: {x.Name} ({x.UnsAreaId})");
var areaIds = areas.Select(x => x.UnsAreaId).ToHashSet(StringComparer.OrdinalIgnoreCase);
var lines = _snapshot.UnsLines.Where(x => areaIds.Contains(x.UnsAreaId)).ToList();
foreach (var x in lines) yield return ($"UnsLine|{x.UnsLineId}", $"Line: {x.Name} ({x.UnsLineId})");
var lineIds = lines.Select(x => x.UnsLineId).ToHashSet(StringComparer.OrdinalIgnoreCase);
foreach (var x in _snapshot.Equipment.Where(x => lineIds.Contains(x.UnsLineId))) yield return ($"Equipment|{x.EquipmentId}", $"Equipment: {x.Name} ({x.EquipmentId})");
}
}
private enum RootPicker { None, Export, Remote }
// #1090: a realm change invalidates every realm-scoped id, so none may survive it.
private static void ClearExportRoot(DataExport e) { e.RootKind = ""; e.RootEntityId = ""; }
private static void MappingRealmChanged(RemoteNamespaceMapping m) { SetRealm(m); m.LocalParentKind = ""; m.LocalParentId = ""; m.RemoteRootEntityId = ""; }
private enum RootPicker { None, Export, Remote, Parent }
private RootPicker _picker;
private string? RemoteTargetCluster(RemoteNamespaceMapping m) => Connections.FirstOrDefault(x => Same(x.DataConnectionId, m.DataConnectionId))?.ExpectedClusterId;
private DataRealm PickerRealm => _picker == RootPicker.Export ? _export?.Realm ?? DataRealm.Raw : _mapping?.Value.Realm ?? DataRealm.Raw;
private string? PickerCluster => _picker == RootPicker.Export ? ClusterId : _mapping is { } m ? RemoteTargetCluster(m.Value) : null;
private string? PickerCluster => _picker is RootPicker.Export or RootPicker.Parent ? ClusterId : _mapping is { } m ? RemoteTargetCluster(m.Value) : null;
// The trees are built once per open, from the snapshot, for the one cluster in scope.
private RawNode? PickerRaw;
private UnsPickNode? PickerUns;
@@ -240,14 +218,15 @@ else
PickerRaw = null; PickerUns = null;
var cluster = PickerCluster;
if (string.IsNullOrEmpty(cluster)) return;
if (PickerRealm == DataRealm.Raw) PickerRaw = DataRootTrees.BuildRaw(_snapshot!, cluster);
if (PickerRealm == DataRealm.Raw) PickerRaw = DataRootTrees.BuildRaw(_snapshot!, cluster, includeTags: which != RootPicker.Parent);
else PickerUns = DataRootTrees.BuildUns(_snapshot!, cluster, equipmentLeaves: which == RootPicker.Export);
}
private string? PickerKind => _picker == RootPicker.Export ? _export?.RootKind : null;
private string? PickerId => _picker == RootPicker.Export ? _export?.RootEntityId : _mapping?.Value.RemoteRootEntityId;
private string? PickerKind => _picker == RootPicker.Export ? _export?.RootKind : _picker == RootPicker.Parent ? _mapping?.Value.LocalParentKind : null;
private string? PickerId => _picker switch { RootPicker.Export => _export?.RootEntityId, RootPicker.Parent => _mapping?.Value.LocalParentId, _ => _mapping?.Value.RemoteRootEntityId };
private void PickRoot((string Kind, string Id) picked)
{
if (_picker == RootPicker.Export && _export is not null) { _export.RootKind = picked.Kind; _export.RootEntityId = picked.Id; }
else if (_picker == RootPicker.Parent && _mapping is not null) { _mapping.Value.LocalParentKind = picked.Kind; _mapping.Value.LocalParentId = picked.Id; }
else if (_picker == RootPicker.Remote && _mapping is not null) _mapping.Value.RemoteRootEntityId = picked.Id;
_picker = RootPicker.None;
}
@@ -138,6 +138,68 @@ public sealed class DataConnectionRootPickerTests : BunitContext
cut.WaitForAssertion(() => cut.Find("#remote-root").GetAttribute("value").ShouldBe("AREA-OTHER"));
}
[Fact]
public void LocalParent_UnsTreePicker_IsLocalClusterScoped_AndStoresKindAndId()
{
var cut = RenderPage();
cut.WaitForElement("button:contains('New mapping')").Click();
cut.WaitForElement("#mapping-parent").GetAttribute("value").ShouldBe("No parent chosen");
cut.Find("#mapping-realm").Change("Uns");
cut.Find("#mapping-parent-browse").Click();
cut.WaitForAssertion(() => cut.Find("[role=dialog]"));
cut.WaitForAssertion(() => cut.FindAll(".uns-root-picker input[type=radio]").Count.ShouldBeGreaterThan(0));
// The parent is a LOCAL node: this page's cluster, never the connection's target cluster.
cut.Find("[role=dialog]").InnerHtml.ShouldContain("Packaging");
cut.Find("[role=dialog]").InnerHtml.ShouldNotContain("Foreign area");
cut.Find("input[title='UnsArea AREA-1']").Change(true);
cut.Find("#root-picker-confirm").Click();
cut.WaitForAssertion(() => cut.Find("#mapping-parent").GetAttribute("value").ShouldBe("UnsArea AREA-1"));
cut.FindAll("[role=dialog]").Count.ShouldBe(0);
}
[Fact]
public void LocalParent_RawTreePicker_PicksAContainer()
{
var cut = RenderPage();
cut.WaitForElement("button:contains('New mapping')").Click();
cut.WaitForElement("#mapping-parent-browse").Click();
cut.WaitForAssertion(() => cut.FindAll("[role=dialog] input[type=radio]").Count.ShouldBeGreaterThan(0));
cut.Find("button[aria-label='Expand PLCs']").Click();
cut.WaitForElement("button[aria-label='Expand modbus-1']").Click();
cut.WaitForElement("input[title='Device dev:" + RawTreeTestDb.FolderedDeviceId + "']").Change(true);
cut.Find("#root-picker-confirm").Click();
cut.WaitForAssertion(() => cut.Find("#mapping-parent").GetAttribute("value").ShouldBe("RawDevice " + RawTreeTestDb.FolderedDeviceId));
}
[Fact]
public void ChangingTheExportRealm_ClearsTheStaleRoot()
{
var cut = RenderPage();
cut.WaitForElement("button:contains('New export')").Click();
cut.WaitForElement("#export-root-id").Change("RawTag|tag-1");
cut.Find("#export-root").GetAttribute("value").ShouldBe("RawTag tag-1");
cut.Find("#export-realm").Change("Uns");
cut.WaitForAssertion(() => cut.Find("#export-root").GetAttribute("value").ShouldBe("No root chosen"));
}
[Fact]
public void ChangingTheMappingRealm_ClearsTheParentAndRemoteRoot()
{
var cut = RenderPage();
cut.WaitForElement("button:contains('New mapping')").Click();
cut.WaitForElement("#mapping-realm").Change("Uns");
cut.Find("#mapping-parent-browse").Click();
cut.WaitForAssertion(() => cut.FindAll(".uns-root-picker input[type=radio]").Count.ShouldBeGreaterThan(0));
cut.Find("input[title='UnsArea AREA-1']").Change(true);
cut.Find("#root-picker-confirm").Click();
cut.WaitForAssertion(() => cut.Find("#mapping-parent").GetAttribute("value").ShouldBe("UnsArea AREA-1"));
cut.Find("#remote-root-id").Change("AREA-OTHER");
cut.Find("#remote-root").GetAttribute("value").ShouldBe("AREA-OTHER");
cut.Find("#mapping-realm").Change("Raw");
cut.WaitForAssertion(() => cut.Find("#mapping-parent").GetAttribute("value").ShouldBe("No parent chosen"));
cut.Find("#remote-root").GetAttribute("value").ShouldBe("No root chosen");
}
[Fact]
public void Cancel_LeavesTheFieldUnchanged()
{