fix(admin-ui): associate alert actions and guard requests
This commit is contained in:
@@ -13,6 +13,7 @@
|
||||
@inject IAuthorizationService AuthorizationService
|
||||
@inject IAdminOperationsClient AdminOps
|
||||
@using ZB.MOM.WW.OtOpcUa.AdminUI.LiveTail
|
||||
@using System.Globalization
|
||||
@implements IAsyncDisposable
|
||||
|
||||
<PageErrorBoundary>
|
||||
@@ -36,6 +37,19 @@
|
||||
Shows the most-recent @Capacity entries since the page opened; reload for a fresh window.
|
||||
</section>
|
||||
|
||||
@if (OperationPending)
|
||||
{
|
||||
<p role="status" aria-live="polite">Request pending for <span class="mono text-break">@_busyAlarmId</span>.</p>
|
||||
}
|
||||
else if (_opResultMessage is not null)
|
||||
{
|
||||
<div class="mb-3">
|
||||
<p role="status" aria-live="polite" class="mb-1"><span class="chip @(_opResultOk ? "chip-ok" : "chip-bad")">@_opResultSummary</span></p>
|
||||
<p class="small text-muted mb-1">Last request for <span class="mono text-break">@_opResultAlarmId</span></p>
|
||||
<DiagnosticDetails Summary="Request details" Text="@_opResultMessage" />
|
||||
</div>
|
||||
}
|
||||
|
||||
@if (_displayed.Count == 0)
|
||||
{
|
||||
<section class="panel notice rise mt-3" style="animation-delay:.08s">
|
||||
@@ -71,11 +85,12 @@ else
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var e in _displayed)
|
||||
@foreach (var row in _displayed)
|
||||
{
|
||||
<tr>
|
||||
<td><span class="mono small">@e.TimestampUtc.ToString("HH:mm:ss.fff")</span></td>
|
||||
<td><span class="mono">@e.AlarmId</span><div class="text-muted small">@e.AlarmName</div></td>
|
||||
var e = row.Payload;
|
||||
<tr @key="row.Sequence">
|
||||
<td><time class="mono small" datetime="@(e.TimestampUtc.ToString("O"))">@e.TimestampUtc.ToString("yyyy-MM-dd HH:mm:ss.fff 'UTC'")</time></td>
|
||||
<th scope="row" id="@FieldId(row.Sequence, "alarm")"><span class="mono">@e.AlarmId</span><div class="text-muted small">@e.AlarmName</div></th>
|
||||
<td>
|
||||
<span class="mono small">@e.EquipmentPath</span>
|
||||
@* v3 Batch 4 (multi-notifier native alarms): one condition row carries the list
|
||||
@@ -95,47 +110,28 @@ else
|
||||
<td><span class="chip @KindChipClass(e.TransitionKind)">@e.TransitionKind</span></td>
|
||||
<td class="num">@e.Severity</td>
|
||||
<td>@e.User</td>
|
||||
<td title="@e.Message">@DisplayText.Abbreviate(e.Message, 80)</td>
|
||||
<td><DiagnosticDetails Summary="@(DisplayText.Abbreviate(e.Message, 80))" Text="@e.Message" /></td>
|
||||
@if (_canOperate)
|
||||
{
|
||||
@* DriverOperator-gated Acknowledge / Shelve / Unshelve. Each routes through
|
||||
the AdminOperationsActor singleton, which republishes onto the cluster
|
||||
'alarm-commands' topic; the owning node applies it (ownership filter). *@
|
||||
<td>
|
||||
<div class="d-flex gap-1 align-items-center">
|
||||
<div class="btn-group btn-group-sm" role="group">
|
||||
<button type="button"
|
||||
class="btn btn-sm btn-outline-secondary"
|
||||
disabled="@_busyAlarmId.Equals(e.AlarmId)"
|
||||
@onclick="() => AcknowledgeAsync(e.AlarmId)"
|
||||
title="Acknowledge this alarm">Ack</button>
|
||||
<button type="button"
|
||||
class="btn btn-sm btn-outline-secondary"
|
||||
disabled="@_busyAlarmId.Equals(e.AlarmId)"
|
||||
@onclick="() => ShelveAsync(e.AlarmId, ShelveKind.OneShot)"
|
||||
title="Shelve this alarm until it next clears">Shelve</button>
|
||||
</div>
|
||||
<input type="number" min="1" class="form-control form-control-sm" style="width:4.5rem"
|
||||
disabled="@_busyAlarmId.Equals(e.AlarmId)"
|
||||
@bind:get="GetShelveMinutes(e.AlarmId)" @bind:set="m => SetShelveMinutes(e.AlarmId, m)"
|
||||
title="Timed-shelve duration in minutes" />
|
||||
<div class="btn-group btn-group-sm" role="group">
|
||||
<button type="button"
|
||||
class="btn btn-sm btn-outline-secondary"
|
||||
disabled="@_busyAlarmId.Equals(e.AlarmId)"
|
||||
@onclick="() => ShelveTimedAsync(e.AlarmId)"
|
||||
title="Shelve this alarm for the chosen number of minutes">Shelve (timed)</button>
|
||||
<button type="button"
|
||||
class="btn btn-sm btn-outline-secondary"
|
||||
disabled="@_busyAlarmId.Equals(e.AlarmId)"
|
||||
@onclick="() => ShelveAsync(e.AlarmId, ShelveKind.Unshelve)"
|
||||
title="Remove an existing shelve">Unshelve</button>
|
||||
</div>
|
||||
<div class="ot-toolbar d-flex gap-2 align-items-end" role="group" aria-busy="@(OperationPending ? "true" : "false")" aria-labelledby="@FieldId(row.Sequence, "alarm")">
|
||||
<button type="button" class="btn btn-sm btn-outline-secondary" disabled="@OperationPending" @onclick="() => AcknowledgeAsync(e.AlarmId)" aria-label="@($"Acknowledge {e.AlarmId}")">Ack</button>
|
||||
<button type="button" class="btn btn-sm btn-outline-secondary" disabled="@OperationPending" @onclick="() => ShelveAsync(e.AlarmId, ShelveKind.OneShot)" aria-label="@($"Shelve once {e.AlarmId}")">Shelve</button>
|
||||
<form @onsubmit="() => ShelveTimedAsync(e.AlarmId)" @onsubmit:preventDefault>
|
||||
<label id="@FieldId(row.Sequence, "minutes-label")" for="@FieldId(row.Sequence, "minutes")" class="form-label small mb-1">Timed shelve minutes</label>
|
||||
<div class="d-flex gap-1 align-items-center">
|
||||
<input id="@FieldId(row.Sequence, "minutes")" type="number" min="1" step="1" required class="form-control form-control-sm" style="width:5.5rem" disabled="@OperationPending"
|
||||
aria-labelledby="@(FieldId(row.Sequence, "minutes-label") + " " + FieldId(row.Sequence, "alarm"))" aria-describedby="@FieldId(row.Sequence, "minutes-error")" aria-invalid="@(DurationError(e.AlarmId) is null ? null : "true")"
|
||||
@bind:get="GetShelveText(e.AlarmId)" @bind:set="value => SetShelveText(e.AlarmId, value)" @bind:event="oninput" />
|
||||
<button type="submit" class="btn btn-sm btn-outline-secondary" disabled="@OperationPending" aria-label="@($"Timed shelve {e.AlarmId}")">Shelve (timed)</button>
|
||||
</div>
|
||||
<div id="@FieldId(row.Sequence, "minutes-error")" class="small text-danger">@DurationError(e.AlarmId)</div>
|
||||
</form>
|
||||
<button type="button" class="btn btn-sm btn-outline-secondary" disabled="@OperationPending" @onclick="() => ShelveAsync(e.AlarmId, ShelveKind.Unshelve)" aria-label="@($"Unshelve {e.AlarmId}")">Unshelve</button>
|
||||
</div>
|
||||
@if (_opResultAlarmId.Equals(e.AlarmId) && _opResultMessage is not null)
|
||||
{
|
||||
<span class="chip @(_opResultOk ? "chip-ok" : "chip-bad")" style="font-size:0.8rem">@_opResultMessage</span>
|
||||
}
|
||||
</td>
|
||||
}
|
||||
</tr>
|
||||
@@ -149,13 +145,19 @@ else
|
||||
|
||||
@code {
|
||||
private const int Capacity = 200;
|
||||
private sealed record AlarmRow(long Sequence, AlarmTransitionEvent Payload);
|
||||
private long _nextRow;
|
||||
private readonly string _fieldScope = "alarm-" + Guid.NewGuid().ToString("N");
|
||||
private string FieldId(long row, string field) => $"{_fieldScope}-{row}-{field}";
|
||||
private volatile bool _disposed;
|
||||
private bool OperationPending => !string.IsNullOrEmpty(_busyAlarmId);
|
||||
|
||||
// Newest-first ring, filled from the bridge thread and drained once per coalescing window (R04 P7).
|
||||
private readonly LiveTailBuffer<AlarmTransitionEvent> _tail = new(Capacity);
|
||||
private readonly LiveTailBuffer<AlarmRow> _tail = new(Capacity);
|
||||
private CoalescingRenderScheduler? _render;
|
||||
private bool _connected;
|
||||
private bool _paused;
|
||||
private IReadOnlyList<AlarmTransitionEvent> _displayed = Array.Empty<AlarmTransitionEvent>();
|
||||
private IReadOnlyList<AlarmRow> _displayed = Array.Empty<AlarmRow>();
|
||||
|
||||
// Authorization — DriverOperator gates the per-row Ack/Shelve/Unshelve controls.
|
||||
private bool _canOperate;
|
||||
@@ -166,20 +168,26 @@ else
|
||||
private string _opResultAlarmId = "";
|
||||
private string? _opResultMessage;
|
||||
private bool _opResultOk;
|
||||
private string? _opResultSummary;
|
||||
|
||||
// Auto-clear timer for the per-row result chip (mirrors DriverStatusPanel): the chip is set in
|
||||
// Auto-clear timer for the single request outcome (mirrors DriverStatusPanel): the chip is set in
|
||||
// ShowOpResult and cleared 8 s later so it doesn't persist until the next action.
|
||||
private System.Threading.Timer? _opResultClearTimer;
|
||||
private object? _opResultClearToken;
|
||||
|
||||
// Per-row timed-shelve duration (minutes). Keyed by AlarmId so each row's number input is
|
||||
// independent — binding every row to one shared field would couple all the inputs together.
|
||||
// Timed-shelve drafts belong to an alarm condition. Repeated transitions retain that
|
||||
// existing shared choice; page-only row sequences give each rendered control its own ID.
|
||||
private const int DefaultShelveMinutes = 5;
|
||||
private readonly Dictionary<string, int> _shelveMinutes = new();
|
||||
private int GetShelveMinutes(string alarmId) =>
|
||||
_shelveMinutes.TryGetValue(alarmId, out var m) ? m : DefaultShelveMinutes;
|
||||
private void SetShelveMinutes(string alarmId, int minutes) =>
|
||||
_shelveMinutes[alarmId] = minutes < 1 ? 1 : minutes;
|
||||
private readonly Dictionary<string, string> _shelveText = new();
|
||||
private readonly Dictionary<string, string> _durationErrors = new();
|
||||
private string GetShelveText(string alarmId) => _shelveText.TryGetValue(alarmId, out var text) ? text : DefaultShelveMinutes.ToString(CultureInfo.InvariantCulture);
|
||||
private string? DurationError(string alarmId) => _durationErrors.GetValueOrDefault(alarmId);
|
||||
private void SetShelveText(string alarmId, string? value)
|
||||
{
|
||||
if (_disposed || OperationPending) return;
|
||||
_shelveText[alarmId] = value ?? "";
|
||||
_durationErrors.Remove(alarmId);
|
||||
}
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
@@ -200,17 +208,19 @@ else
|
||||
// mid-session token refresh lands in the published command + audit accurately.
|
||||
var auth = await AuthState.GetAuthenticationStateAsync();
|
||||
var authResult = await AuthorizationService.AuthorizeAsync(auth.User, null, AdminUiPolicies.DriverOperator);
|
||||
_canOperate = authResult.Succeeded;
|
||||
if (!_disposed) _canOperate = authResult.Succeeded;
|
||||
}
|
||||
|
||||
private async Task AcknowledgeAsync(string alarmId)
|
||||
{
|
||||
if (_disposed || OperationPending || !_canOperate) return;
|
||||
_busyAlarmId = alarmId;
|
||||
_opResultMessage = null;
|
||||
StateHasChanged();
|
||||
try
|
||||
{
|
||||
var user = await GetCurrentUserNameAsync();
|
||||
if (_disposed) return;
|
||||
using var cts = new System.Threading.CancellationTokenSource(TimeSpan.FromSeconds(15));
|
||||
var result = await AdminOps.AcknowledgeAlarmAsync(
|
||||
alarmId, user, comment: null, cts.Token);
|
||||
@@ -218,28 +228,38 @@ else
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ShowOpResult(alarmId, false, ex.Message.Length > 60 ? ex.Message[..60] + "…" : ex.Message);
|
||||
ShowOpResult(alarmId, false, ex.Message, "Request result unavailable");
|
||||
}
|
||||
finally
|
||||
{
|
||||
_busyAlarmId = "";
|
||||
StateHasChanged();
|
||||
if (!_disposed) StateHasChanged();
|
||||
}
|
||||
}
|
||||
|
||||
// Thin wrapper for the per-row "Shelve (timed)" button: shelves until now + the row's chosen
|
||||
// minutes. OneShot/Unshelve call ShelveAsync directly with the default null unshelve-at.
|
||||
private Task ShelveTimedAsync(string alarmId) =>
|
||||
ShelveAsync(alarmId, ShelveKind.Timed, DateTime.UtcNow.AddMinutes(GetShelveMinutes(alarmId)));
|
||||
private Task ShelveTimedAsync(string alarmId)
|
||||
{
|
||||
if (_disposed || OperationPending || !_canOperate) return Task.CompletedTask;
|
||||
if (!int.TryParse(GetShelveText(alarmId), NumberStyles.Integer, CultureInfo.InvariantCulture, out var minutes) || minutes < 1)
|
||||
{
|
||||
_durationErrors[alarmId] = "Enter a whole number of minutes of at least 1.";
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
return ShelveAsync(alarmId, ShelveKind.Timed, DateTime.UtcNow.AddMinutes(minutes));
|
||||
}
|
||||
|
||||
private async Task ShelveAsync(string alarmId, ShelveKind kind, DateTime? unshelveAtUtc = null)
|
||||
{
|
||||
if (_disposed || OperationPending || !_canOperate) return;
|
||||
_busyAlarmId = alarmId;
|
||||
_opResultMessage = null;
|
||||
StateHasChanged();
|
||||
try
|
||||
{
|
||||
var user = await GetCurrentUserNameAsync();
|
||||
if (_disposed) return;
|
||||
using var cts = new System.Threading.CancellationTokenSource(TimeSpan.FromSeconds(15));
|
||||
var result = await AdminOps.ShelveAlarmAsync(
|
||||
alarmId, user, kind, unshelveAtUtc: unshelveAtUtc, comment: null, cts.Token);
|
||||
@@ -248,12 +268,12 @@ else
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ShowOpResult(alarmId, false, ex.Message.Length > 60 ? ex.Message[..60] + "…" : ex.Message);
|
||||
ShowOpResult(alarmId, false, ex.Message, "Request result unavailable");
|
||||
}
|
||||
finally
|
||||
{
|
||||
_busyAlarmId = "";
|
||||
StateHasChanged();
|
||||
if (!_disposed) StateHasChanged();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -265,13 +285,23 @@ else
|
||||
private async Task<string> GetCurrentUserNameAsync()
|
||||
{
|
||||
var auth = await AuthState.GetAuthenticationStateAsync();
|
||||
if (_disposed) return "unknown";
|
||||
var permission = await AuthorizationService.AuthorizeAsync(auth.User, null, AdminUiPolicies.DriverOperator);
|
||||
if (_disposed) return "unknown";
|
||||
if (!permission.Succeeded)
|
||||
{
|
||||
_canOperate = false;
|
||||
throw new UnauthorizedAccessException("You are no longer authorized to operate alarms. No request was sent.");
|
||||
}
|
||||
return auth.User.Identity?.Name
|
||||
?? auth.User.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier)?.Value
|
||||
?? "unknown";
|
||||
}
|
||||
|
||||
private void ShowOpResult(string alarmId, bool ok, string message)
|
||||
private void ShowOpResult(string alarmId, bool ok, string message, string? summary = null)
|
||||
{
|
||||
if (_disposed) return;
|
||||
_opResultSummary = summary ?? (ok ? message : "Request rejected");
|
||||
_opResultAlarmId = alarmId;
|
||||
_opResultOk = ok;
|
||||
_opResultMessage = message;
|
||||
@@ -286,7 +316,7 @@ else
|
||||
// Ignore a stale fire: a newer action superseded this timer's chip. (Timer.Dispose does
|
||||
// not drain an already-queued callback, so without this a stale timer could clear a
|
||||
// freshly-set result from a different row.)
|
||||
if (!ReferenceEquals(_opResultClearToken, token)) return;
|
||||
if (_disposed || !ReferenceEquals(_opResultClearToken, token)) return;
|
||||
_opResultMessage = null;
|
||||
_opResultAlarmId = "";
|
||||
StateHasChanged();
|
||||
@@ -298,13 +328,13 @@ else
|
||||
// later, so an alarm storm costs one render per window per tab instead of one per transition.
|
||||
private void OnAlarm(AlarmTransitionEvent evt)
|
||||
{
|
||||
if (_tail.Add(evt)) _render?.Schedule();
|
||||
if (!_disposed && _tail.Add(new AlarmRow(Interlocked.Increment(ref _nextRow), evt))) _render?.Schedule();
|
||||
}
|
||||
|
||||
// Render context: drain the queue into the ring and render once.
|
||||
private void FlushTail()
|
||||
{
|
||||
if (!_tail.Flush()) return;
|
||||
if (_disposed || !_tail.Flush()) return;
|
||||
if (!_paused) _displayed = _tail.Items;
|
||||
StateHasChanged();
|
||||
}
|
||||
@@ -313,7 +343,7 @@ else
|
||||
// subscription's health flips. Marshal onto the circuit sync context like OnAlarm so the live
|
||||
// pill re-renders safely.
|
||||
private void OnConnectionStateChanged(bool connected) =>
|
||||
InvokeAsync(() => { _connected = connected; StateHasChanged(); });
|
||||
InvokeAsync(() => { if (!_disposed) { _connected = connected; StateHasChanged(); } });
|
||||
|
||||
private void TogglePause()
|
||||
{
|
||||
@@ -328,7 +358,7 @@ else
|
||||
private async Task ClearAsync()
|
||||
{
|
||||
_tail.Clear();
|
||||
_displayed = Array.Empty<AlarmTransitionEvent>();
|
||||
_displayed = Array.Empty<AlarmRow>();
|
||||
await InvokeAsync(StateHasChanged);
|
||||
}
|
||||
|
||||
@@ -343,6 +373,9 @@ else
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
if (_disposed) return;
|
||||
_disposed = true;
|
||||
_opResultClearToken = null;
|
||||
// Unsubscribe BOTH broadcaster events first so the singleton can't invoke a handler on a
|
||||
// disposed component, then drain the auto-clear timer (its async dispose awaits any in-flight
|
||||
// callback so it can't call StateHasChanged on a gone component).
|
||||
|
||||
Reference in New Issue
Block a user