feat(m9/T26b): TemplateEdit full multi-level inherited set + read-only staleness banner

This commit is contained in:
Joseph Doherty
2026-06-18 13:01:38 -04:00
parent ca6e5da34b
commit 6bc2bb5430
5 changed files with 579 additions and 0 deletions
@@ -14,6 +14,7 @@
@inject ICentralUiRepository CentralUiRepository
@inject TemplateService TemplateService
@inject ZB.MOM.WW.ScadaBridge.CentralUI.ScriptAnalysis.ScriptAnalysisService AnalysisService
@inject ZB.MOM.WW.ScadaBridge.CentralUI.Services.ITemplateInheritanceQueryService InheritanceQuery
@inject AuthenticationStateProvider AuthStateProvider
@inject NavigationManager NavigationManager
@inject IDialogService Dialog
@@ -62,6 +63,12 @@
private Template? _ownerTemplate;
private TemplateComposition? _ownerComposition;
// M9-T26b: the FULL transitively-resolved inherited member set (multi-level
// chain + post-creation base additions) + staleness, fetched read-only via
// GetResolvedTemplateMembersCommand. Populated only for derived templates;
// null when the read failed (editor falls back to the stored-row view).
private Commons.Messages.Management.ResolvedTemplateMembers? _resolved;
private bool _loading = true;
private string? _loadError;
private string _activeTab = "attributes";
@@ -195,6 +202,7 @@
_baseTemplate = null;
_ownerTemplate = null;
_ownerComposition = null;
_resolved = null;
if (_selectedTemplate.IsDerived && _selectedTemplate.ParentTemplateId.HasValue)
{
_baseTemplate = await TemplateEngineRepository.GetTemplateByIdAsync(_selectedTemplate.ParentTemplateId.Value);
@@ -218,6 +226,14 @@
}
}
}
// M9-T26b: resolve the FULL inherited member set (whole chain +
// post-creation base additions) read-only. The stored-row tables
// above only carry the IMMEDIATE base; this surfaces the rest with
// origin annotation + a base-changed staleness banner. Read-only:
// a failed read leaves _resolved null and the editor degrades to
// the stored-row view.
_resolved = await InheritanceQuery.ResolveAsync(Id);
}
// Editor metadata: child compositions + every parent that
@@ -270,6 +286,32 @@
</div>
</div>
}
@* M9-T26b: read-only base-changed banner. Informational only — surfaced
when the freshly-resolved inherited set differs from this template's
stored copy (a multi-level inherited member, or a base member added
after this template was created). No action button: a deploy already
resolves fresh, so this is purely an authoring heads-up. *@
@if (_resolved?.Staleness.IsStale == true)
{
<div class="alert alert-info py-2 mb-3" role="status">
<i class="bi bi-info-circle me-1"></i>
<strong>Base template changed</strong> —
@_resolved.Staleness.DifferingMemberCount inherited member(s) differ from this template's stored copy.
The effective set below reflects the live base; a deploy resolves fresh.
</div>
}
@* M9-T26b: the FULL transitively-resolved inherited member set — the whole
chain (grandparent + further ancestors) plus base members added after
this template was created, which the immediate-base tables below cannot
show. Read-only preview; each row carries its origin template + lock
state, and alarms surface the merged effective trigger configuration. *@
@if (_resolved != null)
{
@RenderInheritedSet()
}
<div class="d-flex justify-content-between align-items-center mb-3">
<div>
<h4 class="d-inline mb-0">@_selectedTemplate.Name</h4>
@@ -419,6 +461,115 @@
}
};
// ---- M9-T26b: full resolved inherited set (read-only preview) ----
/// <summary>
/// Renders the FULL transitively-resolved effective member set (attributes,
/// alarms, scripts, native alarm sources) for a derived template. Read-only:
/// no edit / override / deploy actions — purely an authoring view that shows
/// what the whole inheritance chain resolves to (multi-level + post-creation
/// base additions), which the immediate-base tables below cannot surface.
/// </summary>
private RenderFragment RenderInheritedSet() => __builder =>
{
var r = _resolved!;
<div class="card mb-3 border-info-subtle">
<div class="card-header bg-info-subtle d-flex align-items-center">
<i class="bi bi-diagram-3 me-2"></i>
<span class="fw-semibold">Effective inherited set</span>
<span class="text-muted small ms-2">
Resolved across the full inheritance chain (read-only). Edits stay on the per-tab tables below.
</span>
</div>
<div class="card-body">
@RenderInheritedMemberTable("Attributes", r.Attributes, showTrigger: false)
@RenderInheritedMemberTable("Alarms", r.Alarms, showTrigger: true)
@RenderInheritedMemberTable("Scripts", r.Scripts, showTrigger: false)
@RenderInheritedMemberTable("Native Alarm Sources", r.NativeAlarmSources, showTrigger: false)
@if (r.Attributes.Count == 0 && r.Alarms.Count == 0
&& r.Scripts.Count == 0 && r.NativeAlarmSources.Count == 0)
{
<p class="text-muted small mb-0">No resolved members.</p>
}
</div>
</div>
};
/// <summary>
/// Renders one section (attributes / alarms / scripts / native sources) of the
/// resolved inherited set. Each row shows the effective value (for alarms, the
/// merged <c>EffectiveTriggerConfiguration</c> when <paramref name="showTrigger"/>),
/// the origin template, and inherited / locked / base-locked annotations.
/// </summary>
private RenderFragment RenderInheritedMemberTable(
string title,
IReadOnlyList<Commons.Messages.Management.ResolvedTemplateMemberInfo> members,
bool showTrigger) => __builder =>
{
@if (members.Count > 0)
{
<h6 class="mt-2 mb-2">@title <span class="badge bg-secondary">@members.Count</span></h6>
<table class="table table-sm table-striped mb-3">
<thead class="table-light">
<tr>
<th>Name</th>
<th>Effective value</th>
@if (showTrigger)
{
<th>Trigger config (effective)</th>
}
<th>Source</th>
<th>Lock</th>
</tr>
</thead>
<tbody>
@foreach (var m in members)
{
<tr>
<td>@m.Name</td>
<td class="small">@(m.EffectiveValue ?? "—")</td>
@if (showTrigger)
{
<td class="small text-muted font-monospace text-truncate"
style="max-width: 220px;" title="@m.EffectiveTriggerConfiguration">
@(string.IsNullOrEmpty(m.EffectiveTriggerConfiguration) ? "—" : m.EffectiveTriggerConfiguration)
</td>
}
<td>
@if (m.IsInherited)
{
<span class="badge bg-secondary"
title="@($"Inherited from {m.OriginTemplateName}")">
Inherited from @m.OriginTemplateName
</span>
}
else
{
<span class="badge bg-light text-dark">Local</span>
}
</td>
<td>
@if (m.IsBaseLocked)
{
<span class="badge bg-warning text-dark" title="A base template forbids overriding this member.">🔒 Base-locked</span>
}
else if (m.IsLocked)
{
<span class="badge bg-danger" aria-label="Locked">Locked</span>
}
else
{
<span class="badge bg-light text-dark" aria-label="Unlocked">Unlocked</span>
}
</td>
</tr>
}
</tbody>
</table>
}
};
private async Task DeleteTemplate()
{
if (_selectedTemplate == null) return;
@@ -141,6 +141,13 @@ public static class ServiceCollectionExtensions
// DbContext). Read-only; no mutation goes through it.
services.AddScoped<ISchemaLibraryQueryService, SchemaLibraryQueryService>();
// Template inheritance preview (M9-T26b): a read-only facade that dispatches
// GetResolvedTemplateMembersCommand to the central ManagementActor through the
// in-process ManagementActorHolder seam (same Ask path as the HTTP /management
// endpoint). Powers the template editor's FULL transitively-inherited member set
// + base-changed staleness banner; never mutates rows, not on the deploy path.
services.AddScoped<ITemplateInheritanceQueryService, TemplateInheritanceQueryService>();
// Roslyn-backed C# analysis for the Monaco script editor.
// Scoped because SharedScriptCatalog wraps a scoped service.
services.AddMemoryCache(o => o.SizeLimit = 200);
@@ -0,0 +1,35 @@
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Management;
namespace ZB.MOM.WW.ScadaBridge.CentralUI.Services;
/// <summary>
/// Read-only CentralUI facade over the template-inheritance resolve query
/// (M9-T26b). Dispatches the <see cref="GetResolvedTemplateMembersCommand"/> to
/// the central <c>ManagementActor</c> through the in-process
/// <c>ManagementActorHolder</c> seam — the SAME Ask path the HTTP
/// <c>/management</c> endpoint uses — and returns the freshly-resolved
/// <see cref="ResolvedTemplateMembers"/> (the full transitively-inherited member
/// set + staleness summary) so the template editor can render the effective
/// inherited view and a base-changed banner.
/// </summary>
/// <remarks>
/// This is a pure authoring-preview read: it never mutates stored rows and is
/// not on the deploy path. A read failure (transport fault, actor not started)
/// yields <c>null</c> (logged) so the editor degrades to its stored-row view
/// rather than throwing. Mirrors <see cref="ISchemaLibraryService"/>'s read
/// path / the other <c>ManagementActorHolder</c>-backed query facades.
/// </remarks>
public interface ITemplateInheritanceQueryService
{
/// <summary>
/// Resolves the effective inherited member set for a template.
/// </summary>
/// <param name="templateId">The template to resolve.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>
/// The freshly-resolved <see cref="ResolvedTemplateMembers"/>, or <c>null</c>
/// when the read failed (the editor then falls back to its stored-row view).
/// </returns>
Task<ResolvedTemplateMembers?> ResolveAsync(
int templateId, CancellationToken cancellationToken = default);
}
@@ -0,0 +1,133 @@
using System.Security.Claims;
using System.Text.Json;
using Akka.Actor;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.Extensions.Logging;
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Management;
using ZB.MOM.WW.ScadaBridge.ManagementService;
using ZB.MOM.WW.ScadaBridge.Security;
namespace ZB.MOM.WW.ScadaBridge.CentralUI.Services;
/// <summary>
/// Default <see cref="ITemplateInheritanceQueryService"/> implementation — a thin
/// read-only facade that dispatches the <see cref="GetResolvedTemplateMembersCommand"/>
/// to the central <c>ManagementActor</c> through the in-process
/// <see cref="ManagementActorHolder"/> (the same Ask seam the HTTP <c>/management</c>
/// endpoint uses). The actor walks the full inheritance chain and returns the
/// effective member set + staleness; none of that is re-implemented here. Mirrors
/// <see cref="SchemaLibraryService"/>'s read path.
/// </summary>
public sealed class TemplateInheritanceQueryService : ITemplateInheritanceQueryService
{
/// <summary>
/// camelCase + case-insensitive, matching <c>ManagementActor.SerializeResult</c>'s
/// options. <see cref="ManagementSuccess.JsonData"/> is produced with those settings,
/// so the deserializer must mirror them to bind every property.
/// </summary>
private static readonly JsonSerializerOptions ResultDeserializerOptions = new()
{
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
};
private static readonly TimeSpan AskTimeout = TimeSpan.FromSeconds(30);
private readonly ManagementActorHolder _holder;
private readonly AuthenticationStateProvider _auth;
private readonly ILogger<TemplateInheritanceQueryService> _logger;
/// <summary>
/// Initializes a new instance of the <see cref="TemplateInheritanceQueryService"/>.
/// </summary>
/// <param name="holder">Holder for the central <c>ManagementActor</c> reference.</param>
/// <param name="auth">Authentication state provider used to project the current principal.</param>
/// <param name="logger">Logger instance.</param>
public TemplateInheritanceQueryService(
ManagementActorHolder holder,
AuthenticationStateProvider auth,
ILogger<TemplateInheritanceQueryService> logger)
{
_holder = holder ?? throw new ArgumentNullException(nameof(holder));
_auth = auth ?? throw new ArgumentNullException(nameof(auth));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
/// <inheritdoc/>
public async Task<ResolvedTemplateMembers?> ResolveAsync(
int templateId, CancellationToken cancellationToken = default)
{
var response = await SendAsync(new GetResolvedTemplateMembersCommand(templateId), cancellationToken);
if (response is ManagementSuccess success)
{
return JsonSerializer.Deserialize<ResolvedTemplateMembers>(
success.JsonData, ResultDeserializerOptions);
}
// Read path: log + return null so the editor falls back to its stored-row view.
_logger.LogWarning(
"GetResolvedTemplateMembers failed for template {TemplateId}: {Response}",
templateId, DescribeFailure(response));
return null;
}
/// <summary>
/// Wraps <paramref name="command"/> in a <see cref="ManagementEnvelope"/> for the
/// current principal and Asks the <c>ManagementActor</c>. Transport faults (timeout,
/// actor not yet started) become a synthetic <see cref="ManagementError"/> so the
/// caller handles one response shape.
/// </summary>
private async Task<object> SendAsync(object command, CancellationToken cancellationToken)
{
var actor = _holder.ActorRef;
if (actor is null)
{
return new ManagementError(
string.Empty, "Management service is not ready.", "SERVICE_UNAVAILABLE");
}
var user = await BuildAuthenticatedUserAsync();
var envelope = new ManagementEnvelope(user, command, Guid.NewGuid().ToString("N"));
try
{
return await actor.Ask<object>(envelope, AskTimeout, cancellationToken);
}
catch (OperationCanceledException)
{
// Caller-initiated cancel (e.g. circuit teardown) — propagate cleanly.
throw;
}
catch (Exception ex)
{
_logger.LogError(ex, "ManagementActor Ask failed for {Command}", command.GetType().Name);
return new ManagementError(string.Empty, ex.Message, "TRANSPORT_ERROR");
}
}
/// <summary>
/// Projects the current Blazor <see cref="ClaimsPrincipal"/> to the
/// <see cref="AuthenticatedUser"/> the actor authorizes against (mirrors the
/// claim set the HTTP endpoint constructs).
/// </summary>
private async Task<AuthenticatedUser> BuildAuthenticatedUserAsync()
{
var state = await _auth.GetAuthenticationStateAsync();
var principal = state.User;
var username = principal.FindFirst(JwtTokenService.UsernameClaimType)?.Value ?? "unknown";
var displayName = principal.FindFirst(JwtTokenService.DisplayNameClaimType)?.Value ?? username;
var roles = principal.FindAll(JwtTokenService.RoleClaimType).Select(c => c.Value).ToArray();
var permittedSiteIds = principal.FindAll(JwtTokenService.SiteIdClaimType).Select(c => c.Value).ToArray();
return new AuthenticatedUser(username, displayName, roles, permittedSiteIds);
}
/// <summary>Renders a fallback description for an unexpected/failure response.</summary>
private static string DescribeFailure(object response) => response switch
{
ManagementUnauthorized unauthorized => unauthorized.Message,
ManagementError error => error.Error,
_ => "Unexpected response from the management service.",
};
}