Validate audit windows and isolate dashboard count reads
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
@inject ISiteRepository SiteRepository
|
||||
@inject IAuditLogQueryService AuditLogQueryService
|
||||
|
||||
<div class="card mb-3" data-test="audit-filter-bar">
|
||||
<form class="card mb-3" data-test="audit-filter-bar" aria-label="Audit filters" @onsubmit="Apply" @onsubmit:preventDefault>
|
||||
<div class="card-body py-2">
|
||||
@* All filters sit in one wrapped row. Kind / Status / Site use compact
|
||||
MultiSelectDropdown controls; Channel is a single-select because the
|
||||
@@ -91,17 +91,27 @@
|
||||
<div class="col-auto" data-test="filter-custom-range">
|
||||
@if (_model.TimeRange == AuditTimeRangePreset.Custom)
|
||||
{
|
||||
@* DateTimeRangeFilter is INPUT-ONLY: it surfaces the raw datetime-local
|
||||
picks (DateTimeKind.Unspecified) straight into the model's range fields.
|
||||
The local→UTC conversion still happens later in Apply()/LocalInputToUtc,
|
||||
exactly as before. IdPrefix="audit" keeps the audit-from/audit-to ids. *@
|
||||
<DateTimeRangeFilter From="_model.CustomFromUtc"
|
||||
FromChanged="@(v => _model.CustomFromUtc = v)"
|
||||
To="_model.CustomToUtc"
|
||||
ToChanged="@(v => _model.CustomToUtc = v)"
|
||||
IdPrefix="audit"
|
||||
FromLabel="From (UTC)"
|
||||
ToLabel="To (UTC)" />
|
||||
<div class="row g-2 align-items-end">
|
||||
<div class="col-auto">
|
||||
<label class="form-label small mb-1" for="audit-from">From (UTC)</label>
|
||||
<input id="audit-from" data-test="audit-from" type="datetime-local" step="any" class="form-control form-control-sm"
|
||||
value="@_customFromText" @onchange="e => _customFromText = e.Value?.ToString() ?? string.Empty"
|
||||
aria-invalid="@(_customRangeError is not null ? "true" : null)"
|
||||
aria-describedby="@(_customRangeError is not null ? "audit-range-error" : "audit-utc-hint")" />
|
||||
</div>
|
||||
<div class="col-auto">
|
||||
<label class="form-label small mb-1" for="audit-to">To (UTC)</label>
|
||||
<input id="audit-to" data-test="audit-to" type="datetime-local" step="any" class="form-control form-control-sm"
|
||||
value="@_customToText" @onchange="e => _customToText = e.Value?.ToString() ?? string.Empty"
|
||||
aria-invalid="@(_customRangeError is not null ? "true" : null)"
|
||||
aria-describedby="@(_customRangeError is not null ? "audit-range-error" : "audit-utc-hint")" />
|
||||
</div>
|
||||
</div>
|
||||
<div id="audit-utc-hint" class="form-text">Enter UTC values. Leave either bound empty for an open-ended range.</div>
|
||||
@if (_customRangeError is not null)
|
||||
{
|
||||
<div id="audit-range-error" class="field-error s-bad small" role="alert">@_customRangeError</div>
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -163,12 +173,11 @@
|
||||
|
||||
<div class="col-auto ms-auto">
|
||||
<div class="btn-group btn-group-sm" role="group">
|
||||
<button class="btn btn-outline-secondary"
|
||||
<button type="button" class="btn btn-outline-secondary"
|
||||
@onclick="ClearFilters" data-test="filter-clear">Clear</button>
|
||||
<button class="btn btn-primary"
|
||||
@onclick="Apply" data-test="filter-apply">Apply</button>
|
||||
<button type="submit" class="btn btn-primary" data-test="filter-apply">Apply</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
@@ -20,6 +20,9 @@ namespace ZB.MOM.WW.ScadaBridge.CentralUI.Components.Audit;
|
||||
public partial class AuditFilterBar
|
||||
{
|
||||
private readonly AuditQueryModel _model = new();
|
||||
private string _customFromText = string.Empty;
|
||||
private string _customToText = string.Empty;
|
||||
private string? _customRangeError;
|
||||
private List<Site> _sites = new();
|
||||
|
||||
/// <summary>Channel options — the full enum, fixed for the component's lifetime.</summary>
|
||||
@@ -154,6 +157,8 @@ public partial class AuditFilterBar
|
||||
_model.SiteIdentifiers.Clear();
|
||||
_model.SourceNodes.Clear();
|
||||
_model.TimeRange = AuditTimeRangePreset.LastHour;
|
||||
_customFromText = _customToText = string.Empty;
|
||||
_customRangeError = null;
|
||||
_model.CustomFromUtc = null;
|
||||
_model.CustomToUtc = null;
|
||||
_model.InstanceSearch = string.Empty;
|
||||
@@ -167,38 +172,35 @@ public partial class AuditFilterBar
|
||||
|
||||
private async Task Apply()
|
||||
{
|
||||
// <input type="datetime-local"> binds with DateTimeKind.Unspecified
|
||||
// — the value is the user's browser-local wall-clock. Tag it as Local then convert
|
||||
// to UTC before the model emits the filter, otherwise a non-UTC operator's window
|
||||
// is silently shifted by their UTC offset. Done on a swap-and-restore basis so the
|
||||
// bound inputs still show the user's local picks on the next render.
|
||||
var originalFrom = _model.CustomFromUtc;
|
||||
var originalTo = _model.CustomToUtc;
|
||||
try
|
||||
_customRangeError = null;
|
||||
if (_model.TimeRange == AuditTimeRangePreset.Custom)
|
||||
{
|
||||
_model.CustomFromUtc = LocalInputToUtc(originalFrom);
|
||||
_model.CustomToUtc = LocalInputToUtc(originalTo);
|
||||
var now = NowUtcProvider?.Invoke() ?? DateTime.UtcNow;
|
||||
var filter = _model.ToFilter(now);
|
||||
await OnFilterChanged.InvokeAsync(filter);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_model.CustomFromUtc = originalFrom;
|
||||
_model.CustomToUtc = originalTo;
|
||||
if (!TryUtcInput(_customFromText, out var from) || !TryUtcInput(_customToText, out var to))
|
||||
{
|
||||
_customRangeError = "Enter valid UTC date and time values, or clear an optional bound.";
|
||||
return;
|
||||
}
|
||||
if (from.HasValue && to.HasValue && from > to)
|
||||
{
|
||||
_customRangeError = "From (UTC) must be before or equal to To (UTC). Correct the range before applying.";
|
||||
return;
|
||||
}
|
||||
_model.CustomFromUtc = from;
|
||||
_model.CustomToUtc = to;
|
||||
}
|
||||
var now = NowUtcProvider?.Invoke() ?? DateTime.UtcNow;
|
||||
await OnFilterChanged.InvokeAsync(_model.ToFilter(now));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a value bound from <c><input type="datetime-local"></c> (which Blazor
|
||||
/// surfaces as <see cref="DateTimeKind.Unspecified"/>) into UTC. The input represents
|
||||
/// the operator's browser-local wall-clock, so we must tag it <see cref="DateTimeKind.Local"/>
|
||||
/// before <see cref="DateTime.ToUniversalTime"/> can do anything meaningful.
|
||||
/// </summary>
|
||||
private static DateTime? LocalInputToUtc(DateTime? value) =>
|
||||
value.HasValue
|
||||
? DateTime.SpecifyKind(value.Value, DateTimeKind.Local).ToUniversalTime()
|
||||
: (DateTime?)null;
|
||||
private static bool TryUtcInput(string raw, out DateTime? value)
|
||||
{
|
||||
value = null;
|
||||
if (string.IsNullOrWhiteSpace(raw)) return true;
|
||||
if (!DateTime.TryParseExact(raw, ["yyyy-MM-dd'T'HH:mm", "yyyy-MM-dd'T'HH:mm:ss", "yyyy-MM-dd'T'HH:mm:ss.FFFFFFF"],
|
||||
System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out var parsed)) return false;
|
||||
value = DateTime.SpecifyKind(parsed, DateTimeKind.Utc);
|
||||
return true;
|
||||
}
|
||||
|
||||
private static string TimeRangeLabel(AuditTimeRangePreset preset) => preset switch
|
||||
{
|
||||
|
||||
@@ -22,33 +22,49 @@
|
||||
@* KPI row *@
|
||||
<div class="row g-3 mb-4">
|
||||
<div class="col-lg-3 col-md-6 col-12">
|
||||
<div class="card h-100">
|
||||
<div class="card h-100" role="region" aria-label="Sites configured">
|
||||
<div class="card-body text-center">
|
||||
<div class="fs-2 fw-bold">@(_loaded ? _siteCount.ToString() : "—")</div>
|
||||
<div class="fs-2 fw-bold" role="status" aria-live="polite">@CountText(_sites)</div>
|
||||
@if (_sites.Failed)
|
||||
{
|
||||
<button type="button" class="btn btn-sm btn-outline-secondary" disabled="@_sites.Loading" @onclick="LoadSites">Retry Sites configured</button>
|
||||
}
|
||||
<div class="text-muted small">Sites configured</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-lg-3 col-md-6 col-12">
|
||||
<div class="card h-100">
|
||||
<div class="card h-100" role="region" aria-label="Data connections configured">
|
||||
<div class="card-body text-center">
|
||||
<div class="fs-2 fw-bold">@(_loaded ? _dataConnectionCount.ToString() : "—")</div>
|
||||
<div class="fs-2 fw-bold" role="status" aria-live="polite">@CountText(_connections)</div>
|
||||
@if (_connections.Failed)
|
||||
{
|
||||
<button type="button" class="btn btn-sm btn-outline-secondary" disabled="@_connections.Loading" @onclick="LoadConnections">Retry Data connections configured</button>
|
||||
}
|
||||
<div class="text-muted small">Data connections configured</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-lg-3 col-md-6 col-12">
|
||||
<div class="card h-100">
|
||||
<div class="card h-100" role="region" aria-label="Templates">
|
||||
<div class="card-body text-center">
|
||||
<div class="fs-2 fw-bold">@(_loaded ? _templateCount.ToString() : "—")</div>
|
||||
<div class="fs-2 fw-bold" role="status" aria-live="polite">@CountText(_templates)</div>
|
||||
@if (_templates.Failed)
|
||||
{
|
||||
<button type="button" class="btn btn-sm btn-outline-secondary" disabled="@_templates.Loading" @onclick="LoadTemplates">Retry Templates</button>
|
||||
}
|
||||
<div class="text-muted small">Templates</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-lg-3 col-md-6 col-12">
|
||||
<div class="card h-100">
|
||||
<div class="card h-100" role="region" aria-label="API keys">
|
||||
<div class="card-body text-center">
|
||||
<div class="fs-2 fw-bold">@(_loaded ? _apiKeyCount.ToString() : "—")</div>
|
||||
<div class="fs-2 fw-bold" role="status" aria-live="polite">@CountText(_apiKeys)</div>
|
||||
@if (_apiKeys.Failed)
|
||||
{
|
||||
<button type="button" class="btn btn-sm btn-outline-secondary" disabled="@_apiKeys.Loading" @onclick="LoadApiKeys">Retry API keys</button>
|
||||
}
|
||||
<div class="text-muted small">API keys</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -95,25 +111,58 @@
|
||||
</div>
|
||||
|
||||
@code {
|
||||
private bool _loaded;
|
||||
private int _siteCount;
|
||||
private int _dataConnectionCount;
|
||||
private int _templateCount;
|
||||
private int _apiKeyCount;
|
||||
private Task _countReadTail = Task.CompletedTask;
|
||||
private readonly CountState _sites = new();
|
||||
private readonly CountState _connections = new();
|
||||
private readonly CountState _templates = new();
|
||||
private readonly CountState _apiKeys = new();
|
||||
|
||||
private sealed class CountState
|
||||
{
|
||||
public int? Value { get; set; }
|
||||
public bool Loading { get; set; } = true;
|
||||
public bool Failed { get; set; }
|
||||
public bool Pending { get; set; }
|
||||
}
|
||||
private static string CountText(CountState state) => state.Failed ? "Unavailable" : state.Loading ? "Loading…" : state.Value?.ToString() ?? "Unavailable";
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
// Keep repository calls sequential: some implementations share a scoped
|
||||
// DbContext. Each fact still settles independently, including failures.
|
||||
await LoadSites();
|
||||
await LoadConnections();
|
||||
await LoadTemplates();
|
||||
await LoadApiKeys();
|
||||
}
|
||||
private Task LoadSites() => LoadCount(_sites, async () => (await SiteRepository.GetAllSitesAsync()).Count);
|
||||
private Task LoadConnections() => LoadCount(_connections, async () => (await SiteRepository.GetAllDataConnectionsAsync()).Count);
|
||||
private Task LoadTemplates() => LoadCount(_templates, async () => (await TemplateEngineRepository.GetTemplateStubsAsync()).Count);
|
||||
private Task LoadApiKeys() => LoadCount(_apiKeys, async () => (await ApiKeyAdmin.ListAsync()).Count);
|
||||
|
||||
private async Task LoadCount(CountState state, Func<Task<int>> read)
|
||||
{
|
||||
// Renderer callbacks enter serially. Claim this fact before the first
|
||||
// await, then queue every initial/retry read behind the same component gate.
|
||||
if (state.Pending) return;
|
||||
state.Pending = true;
|
||||
state.Loading = true;
|
||||
state.Failed = false;
|
||||
var previous = _countReadTail;
|
||||
var released = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
_countReadTail = released.Task;
|
||||
try
|
||||
{
|
||||
_siteCount = (await SiteRepository.GetAllSitesAsync()).Count;
|
||||
_dataConnectionCount = (await SiteRepository.GetAllDataConnectionsAsync()).Count;
|
||||
_templateCount = (await TemplateEngineRepository.GetTemplateStubsAsync()).Count;
|
||||
_apiKeyCount = (await ApiKeyAdmin.ListAsync()).Count;
|
||||
await previous;
|
||||
state.Value = await read();
|
||||
}
|
||||
catch
|
||||
catch { state.Failed = true; }
|
||||
finally
|
||||
{
|
||||
// Non-fatal — leave counts at zero with the placeholder rendering.
|
||||
state.Loading = false;
|
||||
state.Pending = false;
|
||||
released.TrySetResult();
|
||||
StateHasChanged();
|
||||
}
|
||||
_loaded = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
<div class="toast show mb-2" role="alert">
|
||||
<div class="toast-header @GetHeaderClass(toast.Type)">
|
||||
<strong class="me-auto">@toast.Title</strong>
|
||||
<button type="button" class="btn-close btn-close-white" @onclick="() => Dismiss(toast)"></button>
|
||||
<button type="button" aria-label="@($"Dismiss {toast.Title}")" class="btn-close btn-close-white" @onclick="() => Dismiss(toast)"></button>
|
||||
</div>
|
||||
<div class="toast-body">@toast.Message</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user