Merge branch 'deployments-server-paging' — Deployments page server-side paging + status counts (residual #4 / R3)

This commit is contained in:
Joseph Doherty
2026-08-15 03:34:22 -04:00
12 changed files with 1436 additions and 139 deletions
@@ -1,12 +1,10 @@
@page "/deployment/deployments"
@using ZB.MOM.WW.ScadaBridge.Security
@using ZB.MOM.WW.ScadaBridge.Commons.Entities.Deployment
@using ZB.MOM.WW.ScadaBridge.Commons.Entities.Instances
@using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories
@using ZB.MOM.WW.ScadaBridge.Commons.Types.Deployment
@using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums
@attribute [Authorize(Policy = AuthorizationPolicies.RequireDeployment)]
@inject IDeploymentManagerRepository DeploymentManagerRepository
@inject ITemplateEngineRepository TemplateEngineRepository
@inject ZB.MOM.WW.ScadaBridge.CentralUI.Auth.SiteScopeService SiteScope
@inject ZB.MOM.WW.ScadaBridge.DeploymentManager.IDeploymentStatusNotifier DeploymentStatusNotifier
@implements IDisposable
@@ -23,6 +21,76 @@
</div>
</div>
@* ── Status tiles ──────────────────────────────────────────────────────────
Counts come from ONE server-side grouped aggregation over the same filtered
set the table is drawn from — not from the rows on screen — so they stay
whole-fleet totals while the table shows a single page. Each tile doubles as
the status filter; the active one is pressed and clicking it again clears. *@
<div class="row g-2 mb-3">
@* Each tile is a real <button> so it is keyboard-reachable and announces its
pressed state. Its contents are PHRASING content only (span/small, not
div/h4): a button's content model forbids flow content, and the .card /
.card-body classes are carried by spans made block-level with d-block so
the Bootstrap card look is unchanged. *@
@foreach (var tile in StatusTiles)
{
var isActive = _statusFilter == tile.Status;
<div class="col-lg col-md-4 col-6">
<button type="button"
class="card w-100 h-100 p-0 border-@tile.Variant @(isActive ? "border-2 shadow-sm" : "")"
data-test="@tile.TestId"
aria-pressed="@(isActive ? "true" : "false")"
aria-label="@($"Filter by {tile.Label}: {CountFor(tile.Status)}")"
@onclick="() => SetStatusFilterAsync(tile.Status)">
<span class="card-body text-center py-2 d-block">
<span class="h4 mb-0 d-block text-@tile.Variant">@CountFor(tile.Status)</span>
<small class="text-muted">@tile.Label</small>
</span>
</button>
</div>
}
<div class="col-lg col-md-4 col-6">
<button type="button"
class="card w-100 h-100 p-0 border-secondary @(_statusFilter is null ? "border-2 shadow-sm" : "")"
data-test="deploy-tile-all"
aria-pressed="@(_statusFilter is null ? "true" : "false")"
aria-label="@($"Show all statuses: {_counts.Total}")"
@onclick="() => SetStatusFilterAsync(null)">
<span class="card-body text-center py-2 d-block">
<span class="h4 mb-0 d-block">@_counts.Total</span>
<small class="text-muted">All</small>
</span>
</button>
</div>
</div>
@* ── Filters ── *@
<div class="card mb-3">
<div class="card-body py-2">
<div class="row g-2 align-items-end">
<div class="col-md-5 col-12">
<label class="form-label small mb-1" for="dep-search">Search</label>
<input id="dep-search" type="search" class="form-control form-control-sm"
placeholder="Instance, deployment id, revision or user"
data-test="deploy-search"
value="@_searchInput"
@oninput="OnSearchInput" />
</div>
<div class="col-auto">
<button class="btn btn-outline-secondary btn-sm" data-test="deploy-clear-filters"
disabled="@(!CanClearFilters)" @onclick="ClearFiltersAsync">
Clear filters
</button>
</div>
<div class="col text-end">
<span class="small text-muted" data-test="deploy-result-count">
@_totalCount matching @(_totalCount == 1 ? "deployment" : "deployments")
</span>
</div>
</div>
</div>
</div>
@if (_loading)
{
<LoadingSpinner IsLoading="true" />
@@ -33,46 +101,10 @@
}
else
{
@* Summary cards *@
<div class="row mb-3">
<div class="col-lg-3 col-md-6 col-12">
<div class="card border-warning">
<div class="card-body text-center py-2">
<h4 class="mb-0 text-warning">@_records.Count(r => r.Status == DeploymentStatus.Pending)</h4>
<small class="text-muted">Pending</small>
</div>
</div>
</div>
<div class="col-lg-3 col-md-6 col-12">
<div class="card border-info">
<div class="card-body text-center py-2">
<h4 class="mb-0 text-info">@_records.Count(r => r.Status == DeploymentStatus.InProgress)</h4>
<small class="text-muted">In Progress</small>
</div>
</div>
</div>
<div class="col-lg-3 col-md-6 col-12">
<div class="card border-success">
<div class="card-body text-center py-2">
<h4 class="mb-0 text-success">@_records.Count(r => r.Status == DeploymentStatus.Success)</h4>
<small class="text-muted">Successful</small>
</div>
</div>
</div>
<div class="col-lg-3 col-md-6 col-12">
<div class="card border-danger">
<div class="card-body text-center py-2">
<h4 class="mb-0 text-danger">@_records.Count(r => r.Status == DeploymentStatus.Failed)</h4>
<small class="text-muted">Failed</small>
</div>
</div>
</div>
</div>
@if (_records.Count == 0)
@if (_rows.Count == 0)
{
<div class="text-center py-5 text-muted">
<p class="mb-0">No deployments recorded.</p>
<p class="mb-0">@(HasActiveFilter ? "No deployments match the current filters." : "No deployments recorded.")</p>
</div>
}
else
@@ -90,7 +122,7 @@
</tr>
</thead>
<tbody>
@foreach (var record in _pagedRecords)
@foreach (var record in _rows)
{
var rowId = $"deploy-row-{record.DeploymentId}";
var errorCollapseId = $"deploy-err-{record.DeploymentId}";
@@ -108,7 +140,7 @@
<div class="font-monospace small text-muted" title="@record.RevisionHash">@revShort</div>
}
</td>
<td>@GetInstanceName(record.InstanceId)</td>
<td>@record.InstanceUniqueName</td>
<td>
@if (isFailed)
{
@@ -171,7 +203,7 @@
<nav>
<ul class="pagination pagination-sm justify-content-end">
<li class="page-item @(_currentPage <= 1 ? "disabled" : "")">
<button class="page-link" @onclick="() => GoToPage(_currentPage - 1)">Previous</button>
<button class="page-link" data-test="deploy-page-prev" @onclick="() => GoToPageAsync(_currentPage - 1)">Previous</button>
</li>
@foreach (var page in ZB.MOM.WW.ScadaBridge.CentralUI.Components.Shared.PagerWindow.Build(_currentPage, _totalPages))
{
@@ -185,12 +217,12 @@
{
var p = page;
<li class="page-item @(p == _currentPage ? "active" : "")">
<button class="page-link" @onclick="() => GoToPage(p)">@(p)</button>
<button class="page-link" @onclick="() => GoToPageAsync(p)">@(p)</button>
</li>
}
}
<li class="page-item @(_currentPage >= _totalPages ? "disabled" : "")">
<button class="page-link" @onclick="() => GoToPage(_currentPage + 1)">Next</button>
<button class="page-link" data-test="deploy-page-next" @onclick="() => GoToPageAsync(_currentPage + 1)">Next</button>
</li>
</ul>
</nav>
@@ -199,9 +231,25 @@
</div>
@code {
private List<DeploymentRecord> _records = new();
private List<DeploymentRecord> _pagedRecords = new();
private Dictionary<int, string> _instanceNames = new();
// ── Server-side paging (residual R3) ──────────────────────────────────────
// The page used to read EVERY deployment record and EVERY instance, then
// site-scope, sort, count the tiles and slice a 25-row page in the circuit's
// memory — on first render AND on every deployment-status push. The cost
// scaled with the age of the system (DeploymentRecords is insert-only, one row
// per deploy attempt) rather than the size of the page. All four of those jobs
// are now SQL: QueryDeploymentListPageAsync returns one page joined to its
// instances plus the filtered total, and GetDeploymentStatusCountsAsync returns
// the tile counts from one grouped aggregation.
//
// Paging is OFFSET, not the Audit Log's keyset cursor, because this page's
// pager is numbered and jump-to-any-page: it needs a page count, which only a
// total can give it, and a keyset cursor cannot express either. The total is
// required for the tiles regardless. See QueryDeploymentListPageAsync's
// remarks for the full rationale.
private IReadOnlyList<DeploymentListRow> _rows = Array.Empty<DeploymentListRow>();
private DeploymentStatusCounts _counts = DeploymentStatusCounts.Empty;
private int _totalCount;
private bool _loading = true;
private string? _errorMessage;
private bool _autoRefresh = true;
@@ -211,6 +259,44 @@
private int _totalPages;
private const int PageSize = 25;
// ── Filters ───────────────────────────────────────────────────────────────
private DeploymentStatus? _statusFilter;
/// <summary>Raw contents of the search box, updated on every keystroke.</summary>
private string _searchInput = string.Empty;
/// <summary>
/// The search term the last query actually ran with. Distinct from
/// <see cref="_searchInput"/> so the debounce window can absorb keystrokes
/// without the rendered result count implying a query that has not run.
/// </summary>
private string _appliedSearch = string.Empty;
/// <summary>
/// Whether the CURRENTLY RENDERED result set is constrained. Keyed on the
/// applied search, not the box contents, so the empty-state message describes
/// the query that actually ran.
/// </summary>
private bool HasActiveFilter => _statusFilter is not null || !string.IsNullOrWhiteSpace(_appliedSearch);
/// <summary>
/// Whether "Clear filters" has anything to clear. Keyed on the box CONTENTS as
/// well, so the escape hatch is live the moment the operator types — it must not
/// be dead for the 500 ms the debounce window is still open.
/// </summary>
private bool CanClearFilters => HasActiveFilter || !string.IsNullOrWhiteSpace(_searchInput);
/// <summary>Tile definitions, in display order. The "All" tile is rendered separately.</summary>
private static readonly (DeploymentStatus Status, string Label, string Variant, string TestId)[] StatusTiles =
{
(DeploymentStatus.Pending, "Pending", "warning", "deploy-tile-pending"),
(DeploymentStatus.InProgress, "In Progress", "info", "deploy-tile-inprogress"),
(DeploymentStatus.Success, "Successful", "success", "deploy-tile-success"),
(DeploymentStatus.Failed, "Failed", "danger", "deploy-tile-failed"),
};
private int CountFor(DeploymentStatus status) => _counts.For(status);
// CentralUI-022: IDeploymentStatusNotifier is a process singleton that
// raises StatusChanged on the DeploymentManager service thread. Dispose()
// unsubscribes, but the notifier can read its subscriber list and begin
@@ -237,12 +323,13 @@
}
// ── Push coalescing (arch-review WP2.4) ───────────────────────────────────
// One reload = every deployment record + every instance, re-read and re-filtered.
// The notifier fires per status WRITE, so a site deploy of N instances produced
// 2N+ of those full reloads back to back, per circuit. The reload is now
// leading-edge debounced: the first push after an idle gap still reloads
// immediately (a single deployment stays as responsive as before), and every
// push inside the window is absorbed into ONE trailing reload.
// 2N+ back-to-back reloads per circuit. The reload is leading-edge debounced:
// the first push after an idle gap still reloads immediately (a single
// deployment stays as responsive as before), and every push inside the window
// is absorbed into ONE trailing reload. Server-side paging shrank what a reload
// costs, but it did not change how MANY arrive — the coalescing still earns its
// keep, and now bounds round-trips to the database rather than table scans.
private const int ReloadDebounceMs = 500;
private DateTimeOffset _lastReloadAt = DateTimeOffset.MinValue;
@@ -296,6 +383,52 @@
private readonly object _coalesceLock = new();
// ── Filter debounce ───────────────────────────────────────────────────────
// Same Timer + lock + _disposed idiom as the push coalescer above, but TRAILING
// ONLY: a search box must not query on the first keystroke, so there is no
// leading edge. Each keystroke re-arms the window, and the query runs once the
// operator stops typing for FilterDebounceMs. Without this, every character of
// a 20-character instance name would be one paged query plus one count
// aggregation against central MS SQL.
private const int FilterDebounceMs = 500;
/// <summary>Trailing-edge timer for search-box input. Disposed with the component.</summary>
private Timer? _filterDebounceTimer;
private readonly object _filterLock = new();
private void OnSearchInput(ChangeEventArgs e)
{
_searchInput = e.Value?.ToString() ?? string.Empty;
lock (_filterLock)
{
if (_disposed) return;
// Re-arm rather than stack: one pending query per idle gap, not one per
// keystroke.
_filterDebounceTimer?.Dispose();
_filterDebounceTimer = new Timer(
_ => OnFilterDebounceElapsed(), null,
TimeSpan.FromMilliseconds(FilterDebounceMs), Timeout.InfiniteTimeSpan);
}
}
private void OnFilterDebounceElapsed()
{
lock (_filterLock)
{
_filterDebounceTimer?.Dispose();
_filterDebounceTimer = null;
if (_disposed) return;
}
// A changed search term redefines the result set, so the operator's current
// page number no longer refers to anything — restart at page 1.
_currentPage = 1;
_ = DispatchReloadAsync();
}
/// <summary>
/// Reloads the deployment table on the renderer's dispatcher, guarded
/// against the component being disposed mid-flight (CentralUI-022):
@@ -338,33 +471,78 @@
}
}
private async Task SetStatusFilterAsync(DeploymentStatus? status)
{
// Clicking the active tile clears it — a toggle, so the tiles are both the
// readout and the control without needing a separate "clear" affordance.
_statusFilter = _statusFilter == status ? null : status;
_currentPage = 1;
await LoadDataAsync();
}
private async Task ClearFiltersAsync()
{
_statusFilter = null;
_searchInput = string.Empty;
_appliedSearch = string.Empty;
_currentPage = 1;
// A cleared box must not be re-queried by an armed keystroke timer.
lock (_filterLock)
{
_filterDebounceTimer?.Dispose();
_filterDebounceTimer = null;
}
await LoadDataAsync();
}
private async Task LoadDataAsync()
{
_loading = _records.Count == 0; // Only show loading on first load
_loading = _rows.Count == 0; // Only show loading on first load
_errorMessage = null;
try
{
// Build instance lookups first — site scoping (CentralUI-002) filters
// deployment records by the site of their instance.
var instances = await TemplateEngineRepository.GetAllInstancesAsync();
_instanceNames = instances.ToDictionary(i => i.Id, i => i.UniqueName);
var instanceSiteIds = instances.ToDictionary(i => i.Id, i => i.SiteId);
// Site scoping (CentralUI-002) is pushed into the query as the set of
// permitted site ids and resolved through the deployment record's
// instance, rather than by loading every instance to build an
// InstanceId → SiteId map. A system-wide user passes null (no filter);
// a scoped user's EMPTY grant is a real filter that matches nothing.
var systemWide = await SiteScope.IsSystemWideAsync();
var permittedSiteIds = systemWide
IReadOnlyCollection<int>? siteScope = systemWide
? null
: await SiteScope.PermittedSiteIdsAsync();
: (await SiteScope.PermittedSiteIdsAsync()).ToArray();
_records = (await DeploymentManagerRepository.GetAllDeploymentRecordsAsync())
.Where(r => permittedSiteIds == null
|| (instanceSiteIds.TryGetValue(r.InstanceId, out var sid)
&& permittedSiteIds.Contains(sid)))
.OrderByDescending(r => r.DeployedAt)
.ToList();
_appliedSearch = _searchInput.Trim();
var filter = new DeploymentListFilter(
Status: _statusFilter,
Search: _appliedSearch,
SiteIdScope: siteScope);
_totalPages = Math.Max(1, (int)Math.Ceiling(_records.Count / (double)PageSize));
if (_currentPage > _totalPages) _currentPage = 1;
UpdatePage();
var page = await DeploymentManagerRepository
.QueryDeploymentListPageAsync(filter, _currentPage, PageSize);
// The tile counts deliberately ignore the status filter (see
// GetDeploymentStatusCountsAsync) so each tile keeps showing its own
// total while it is the one selected.
_counts = await DeploymentManagerRepository.GetDeploymentStatusCountsAsync(filter);
_rows = page.Rows;
_totalCount = page.TotalCount;
_totalPages = page.PageCount(PageSize);
// A concurrent retention purge, or a filter that narrowed the set, can
// leave the operator past the end. Fall back to the last real page and
// re-query rather than rendering a blank table with a live pager.
if (_currentPage > _totalPages && _totalPages >= 1)
{
_currentPage = _totalPages;
var reread = await DeploymentManagerRepository
.QueryDeploymentListPageAsync(filter, _currentPage, PageSize);
_rows = reread.Rows;
_totalCount = reread.TotalCount;
_totalPages = reread.PageCount(PageSize);
}
}
catch (Exception ex)
{
@@ -373,24 +551,13 @@
_loading = false;
}
private void GoToPage(int page)
private async Task GoToPageAsync(int page)
{
if (page < 1 || page > _totalPages) return;
if (page < 1 || page > _totalPages || page == _currentPage) return;
_currentPage = page;
UpdatePage();
await LoadDataAsync();
}
private void UpdatePage()
{
_pagedRecords = _records
.Skip((_currentPage - 1) * PageSize)
.Take(PageSize)
.ToList();
}
private string GetInstanceName(int instanceId) =>
_instanceNames.GetValueOrDefault(instanceId, $"#{instanceId}");
private static string GetStatusBadge(DeploymentStatus status) => status switch
{
DeploymentStatus.Pending => "bg-warning text-dark",
@@ -419,5 +586,10 @@
_coalesceTimer?.Dispose();
_coalesceTimer = null;
}
lock (_filterLock)
{
_filterDebounceTimer?.Dispose();
_filterDebounceTimer = null;
}
}
}
@@ -16,11 +16,69 @@ public interface IDeploymentManagerRepository
/// <returns>The deployment record, or null if not found.</returns>
Task<DeploymentRecord?> GetDeploymentRecordByIdAsync(int id, CancellationToken cancellationToken = default);
/// <summary>
/// Gets all deployment records.
/// Database-side paged, filtered and instance-joined deployment listing —
/// the read path behind the Central UI deployment-status page.
///
/// <para>
/// Replaces a <c>GetAllDeploymentRecords</c> whole-table read. That method's only
/// caller loaded EVERY deployment record (the table is insert-only, one row per
/// deploy attempt for the retention window) plus EVERY instance, then filtered by
/// site scope, sorted, counted the status tiles and sliced a 25-row page — all in
/// the Blazor circuit's memory, on every render AND on every deployment-status
/// push. Cost scaled with the age of the system rather than the size of the page.
/// </para>
///
/// <para>
/// <b>OFFSET paging, not keyset — deliberately.</b> The sibling Audit Log and Site
/// Calls read paths are keyset-cursored, but this one is offset-paged like the
/// Notification Outbox, and for the same reason: its contract surfaces a PAGE
/// NUMBER and a TOTAL COUNT (the page renders a numbered, jump-to-any-page pager),
/// and a keyset cursor can express neither. The total is needed regardless, since
/// the status tiles are an aggregate over the same filtered set. The deep-offset
/// cost that pushes high-volume tables to keyset is bounded here by
/// <see cref="PurgeTerminalDeploymentRecordsAsync"/>, which caps the table at the
/// terminal-record retention window — unlike the 365-day central
/// <c>AuditLog</c>.
/// </para>
///
/// <para>
/// Ordering is <c>DeployedAt DESC, Id DESC</c>. The <c>Id</c> tie-break is not
/// cosmetic: <c>DeployedAt</c> ties on rapid redeploys, and an unstable sort key
/// makes offset paging non-deterministic — rows repeat across pages or vanish
/// between them.
/// </para>
/// </summary>
/// <param name="filter">Status / search / site-scope / instance constraints; all applied DB-side.</param>
/// <param name="pageNumber">1-based page number; values below 1 are floored at 1.</param>
/// <param name="pageSize">Rows per page; clamped to <c>[1, DeploymentRecordSummary.MaxPageSize]</c>.</param>
/// <param name="cancellationToken">A cancellation token that can be used to cancel the operation.</param>
/// <returns>A read-only list of all deployment records.</returns>
Task<IReadOnlyList<DeploymentRecord>> GetAllDeploymentRecordsAsync(CancellationToken cancellationToken = default);
/// <returns>The requested page of rows plus the total count of the filtered set.</returns>
Task<Types.Deployment.DeploymentListPage> QueryDeploymentListPageAsync(
Types.Deployment.DeploymentListFilter filter,
int pageNumber,
int pageSize,
CancellationToken cancellationToken = default);
/// <summary>
/// One grouped aggregation returning the per-status row counts of the filtered
/// set — the server-side replacement for the deployment page's four
/// <c>records.Count(r =&gt; r.Status == X)</c> tile computations over a
/// client-materialized table.
///
/// <para>
/// <b><see cref="Types.Deployment.DeploymentListFilter.Status"/> is ignored here.</b>
/// The tiles are the status BREAKDOWN of the otherwise-identically-filtered set,
/// so constraining them by the active status filter would collapse three of the
/// four tiles to zero the moment an operator clicked one. Every other dimension
/// (search, site scope, instance) IS applied, so the tiles always describe the
/// same population the list is drawn from.
/// </para>
/// </summary>
/// <param name="filter">The active list filter; its <c>Status</c> member is deliberately not applied.</param>
/// <param name="cancellationToken">A cancellation token that can be used to cancel the operation.</param>
/// <returns>Per-status counts, with absent statuses reported as 0.</returns>
Task<Types.Deployment.DeploymentStatusCounts> GetDeploymentStatusCountsAsync(
Types.Deployment.DeploymentListFilter filter,
CancellationToken cancellationToken = default);
/// <summary>
/// Database-side paged and filtered deployment query returning row summaries.
/// Backs the <c>QueryDeployments</c> management command, which previously
@@ -0,0 +1,50 @@
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
namespace ZB.MOM.WW.ScadaBridge.Commons.Types.Deployment;
/// <summary>
/// Filter contract for the Central UI deployment-status list. Every dimension is
/// applied IN THE DATABASE — the page that consumes it previously loaded the whole
/// <c>DeploymentRecords</c> table plus every <c>Instance</c> and filtered, sorted,
/// counted and paged the result in the Blazor circuit's memory.
///
/// <para>
/// Message-contract evolution rule: additive-only. New dimensions go on the end
/// with a default of <see langword="null"/> meaning "unconstrained".
/// </para>
/// </summary>
/// <param name="Status">
/// Restrict to one deployment status, or <see langword="null"/> for all.
/// Deliberately IGNORED by the status-count aggregation
/// (<c>IDeploymentManagerRepository.GetDeploymentStatusCountsAsync</c>): the tiles
/// are the status breakdown of the otherwise-identically-filtered set, so a tile
/// must keep showing its own total while that same status is the active filter.
/// </param>
/// <param name="Search">
/// Free-text term matched (case-insensitively, as a substring) against the
/// instance unique name, the deployment id, the revision hash and the initiating
/// user. Blank/whitespace is treated as unconstrained.
/// </param>
/// <param name="SiteIdScope">
/// When non-null, restrict to deployments whose instance belongs to one of these
/// sites — the site-scoped user's permitted-site grant, pushed into the query
/// rather than intersected in memory. An EMPTY collection is a real, meaningful
/// filter (a user permitted no sites sees nothing) and must not be treated as
/// "no filter"; <see langword="null"/> is the system-wide case.
/// </param>
/// <param name="InstanceId">Restrict to one instance's deployment history, or <see langword="null"/> for all.</param>
public sealed record DeploymentListFilter(
DeploymentStatus? Status = null,
string? Search = null,
IReadOnlyCollection<int>? SiteIdScope = null,
int? InstanceId = null)
{
/// <summary>
/// The trimmed search term, or <see langword="null"/> when the term is absent
/// or whitespace-only. Repository implementations use this rather than
/// <see cref="Search"/> so a stray space never becomes a LIKE pattern that
/// matches nothing.
/// </summary>
public string? NormalizedSearch =>
string.IsNullOrWhiteSpace(Search) ? null : Search.Trim();
}
@@ -0,0 +1,34 @@
namespace ZB.MOM.WW.ScadaBridge.Commons.Types.Deployment;
/// <summary>
/// One page of the Central UI deployment-status list plus the total row count of
/// the filtered set.
///
/// <para>
/// The count travels WITH the page because the page's pager is a numbered,
/// jump-to-any-page control: it needs a page count, which only a total can give
/// it. That is also the reason this read path is OFFSET-paged rather than keyset
/// -paged like the Audit Log — see
/// <c>IDeploymentManagerRepository.QueryDeploymentListPageAsync</c> for the full
/// rationale.
/// </para>
/// </summary>
/// <param name="Rows">The requested page of rows, newest deployment first.</param>
/// <param name="TotalCount">Total rows matching the filter across all pages.</param>
public sealed record DeploymentListPage(
IReadOnlyList<DeploymentListRow> Rows,
int TotalCount)
{
/// <summary>An empty page — no rows, no matches.</summary>
public static DeploymentListPage Empty { get; } = new(Array.Empty<DeploymentListRow>(), 0);
/// <summary>
/// Number of pages the filtered set spans at the given page size; at least 1
/// so a pager always has a page to sit on. Returns 1 for a non-positive
/// <paramref name="pageSize"/> rather than dividing by zero.
/// </summary>
/// <param name="pageSize">Rows per page.</param>
/// <returns>The total page count, never less than 1.</returns>
public int PageCount(int pageSize) =>
pageSize <= 0 ? 1 : Math.Max(1, (int)Math.Ceiling(TotalCount / (double)pageSize));
}
@@ -0,0 +1,50 @@
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
namespace ZB.MOM.WW.ScadaBridge.Commons.Types.Deployment;
/// <summary>
/// One rendered row of the Central UI deployment-status list: a
/// <c>DeploymentRecord</c> projection already joined to its <c>Instance</c>.
///
/// <para>
/// The join is what removes the page's SECOND full-table read. Rendering a row
/// needs the instance's display name and site scoping needs its site, and the page
/// used to obtain both by materializing every instance into two dictionaries. Both
/// values now come back with the page of rows that actually needs them.
/// </para>
///
/// <para>
/// Like <see cref="DeploymentRecordSummary"/>, the <c>RowVersion</c>
/// optimistic-concurrency token is deliberately absent — this is a READ projection
/// and must never be mistaken for something that can be written back. Mutating
/// paths keep loading the tracked entity.
/// </para>
///
/// <para>
/// Message-contract evolution rule: additive-only. New fields go on the end with a
/// default.
/// </para>
/// </summary>
/// <param name="Id">Deployment record row id.</param>
/// <param name="DeploymentId">The logical deployment id (GUID, "N" format).</param>
/// <param name="InstanceId">Instance the deployment targeted.</param>
/// <param name="InstanceUniqueName">The target instance's unique name, for display.</param>
/// <param name="SiteId">The target instance's site, the value site scoping is evaluated against.</param>
/// <param name="Status">Terminal or in-flight deployment status.</param>
/// <param name="RevisionHash">Revision hash of the deployed configuration.</param>
/// <param name="DeployedBy">User who initiated the deployment.</param>
/// <param name="DeployedAt">When the deployment was initiated.</param>
/// <param name="CompletedAt">When the deployment reached a terminal status, if it has.</param>
/// <param name="ErrorMessage">Failure detail when the deployment did not succeed.</param>
public sealed record DeploymentListRow(
int Id,
string DeploymentId,
int InstanceId,
string InstanceUniqueName,
int SiteId,
DeploymentStatus Status,
string? RevisionHash,
string DeployedBy,
DateTimeOffset DeployedAt,
DateTimeOffset? CompletedAt,
string? ErrorMessage);
@@ -0,0 +1,78 @@
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
namespace ZB.MOM.WW.ScadaBridge.Commons.Types.Deployment;
/// <summary>
/// Server-computed deployment counts per <see cref="DeploymentStatus"/>, backing the
/// Central UI deployment-status tiles.
///
/// <para>
/// The tiles used to be <c>_records.Count(r =&gt; r.Status == X)</c> over the
/// client-materialized full table — which meant the tiles were only correct because
/// the whole table was in memory, and paging the list would silently have reduced
/// them to "counts of the visible page". Computing them DB-side with one grouped
/// query decouples the tiles from the page.
/// </para>
/// </summary>
/// <param name="Pending">Rows in <see cref="DeploymentStatus.Pending"/>.</param>
/// <param name="InProgress">Rows in <see cref="DeploymentStatus.InProgress"/>.</param>
/// <param name="Success">Rows in <see cref="DeploymentStatus.Success"/>.</param>
/// <param name="Failed">Rows in <see cref="DeploymentStatus.Failed"/>.</param>
public sealed record DeploymentStatusCounts(
int Pending,
int InProgress,
int Success,
int Failed)
{
/// <summary>All-zero counts — the shape returned for an empty or fully-excluded set.</summary>
public static DeploymentStatusCounts Empty { get; } = new(0, 0, 0, 0);
/// <summary>
/// Total across every status. Summing is exact rather than approximate because
/// <see cref="DeploymentStatus"/> is a closed set and every row carries one of
/// its members.
/// </summary>
public int Total => Pending + InProgress + Success + Failed;
/// <summary>Count for a single status.</summary>
/// <param name="status">The status to read.</param>
/// <returns>The count for <paramref name="status"/>; 0 for an unrecognised value.</returns>
public int For(DeploymentStatus status) => status switch
{
DeploymentStatus.Pending => Pending,
DeploymentStatus.InProgress => InProgress,
DeploymentStatus.Success => Success,
DeploymentStatus.Failed => Failed,
_ => 0
};
/// <summary>
/// Folds the <c>(status, count)</c> pairs a grouped database query returns into
/// the fixed-shape record. Statuses absent from the grouping — no rows in that
/// state — land as 0 rather than being missing.
/// </summary>
/// <param name="grouped">Per-status counts as returned by the aggregation query.</param>
/// <returns>The folded counts.</returns>
public static DeploymentStatusCounts FromGrouped(IEnumerable<(DeploymentStatus Status, int Count)> grouped)
{
ArgumentNullException.ThrowIfNull(grouped);
var pending = 0;
var inProgress = 0;
var success = 0;
var failed = 0;
foreach (var (status, count) in grouped)
{
switch (status)
{
case DeploymentStatus.Pending: pending += count; break;
case DeploymentStatus.InProgress: inProgress += count; break;
case DeploymentStatus.Success: success += count; break;
case DeploymentStatus.Failed: failed += count; break;
}
}
return new DeploymentStatusCounts(pending, inProgress, success, failed);
}
}
@@ -36,12 +36,131 @@ public class DeploymentManagerRepository : IDeploymentManagerRepository
return await _dbContext.DeploymentRecords.FindAsync([id], cancellationToken);
}
/// <inheritdoc />
public async Task<IReadOnlyList<DeploymentRecord>> GetAllDeploymentRecordsAsync(CancellationToken cancellationToken = default)
/// <summary>
/// Shared filtered join behind <see cref="QueryDeploymentListPageAsync"/> and
/// <see cref="GetDeploymentStatusCountsAsync"/>, so the two reads that back one
/// screen can never disagree about which rows they describe.
///
/// <para>
/// The join to <c>Instance</c> is an INNER join, which is exact rather than
/// merely convenient: <c>DeploymentRecord</c> has a <c>Restrict</c> FK to
/// <c>Instance</c> and <see cref="DeleteInstanceAsync"/> removes the records
/// before the instance, so an orphaned deployment record cannot exist. The join
/// is what lets site scoping run in SQL — <c>DeploymentRecord</c> itself has no
/// <c>SiteId</c>.
/// </para>
/// </summary>
/// <param name="filter">Filter to apply.</param>
/// <param name="applyStatus">
/// When <see langword="false"/>, the status constraint is skipped — the
/// status-count aggregation needs every status of the otherwise-identical set.
/// </param>
/// <returns>The composed, unordered, untracked query.</returns>
private IQueryable<DeploymentInstanceJoin> BuildListQuery(DeploymentListFilter filter, bool applyStatus)
{
return await _dbContext.DeploymentRecords
.OrderByDescending(d => d.DeployedAt)
ArgumentNullException.ThrowIfNull(filter);
var query =
from d in _dbContext.DeploymentRecords.AsNoTracking()
join i in _dbContext.Set<Instance>().AsNoTracking() on d.InstanceId equals i.Id
select new DeploymentInstanceJoin
{
Id = d.Id,
DeploymentId = d.DeploymentId,
InstanceId = d.InstanceId,
InstanceUniqueName = i.UniqueName,
SiteId = i.SiteId,
Status = d.Status,
RevisionHash = d.RevisionHash,
DeployedBy = d.DeployedBy,
DeployedAt = d.DeployedAt,
CompletedAt = d.CompletedAt,
ErrorMessage = d.ErrorMessage
};
if (filter.InstanceId.HasValue)
query = query.Where(r => r.InstanceId == filter.InstanceId.Value);
if (applyStatus && filter.Status.HasValue)
query = query.Where(r => r.Status == filter.Status.Value);
if (filter.SiteIdScope != null)
{
// An EMPTY scope is a real filter — a user permitted no sites sees
// nothing — so it must not be short-circuited into "no filter".
var scope = filter.SiteIdScope as int[] ?? filter.SiteIdScope.ToArray();
query = query.Where(r => scope.Contains(r.SiteId));
}
if (filter.NormalizedSearch is { } search)
{
// Substring match across the four identifiers a deployment is findable
// by. Translated to LIKE '%term%' by both providers; case-insensitivity
// is the database collation's (SQL Server default CI, SQLite ASCII LIKE).
query = query.Where(r =>
r.InstanceUniqueName.Contains(search)
|| r.DeploymentId.Contains(search)
|| r.DeployedBy.Contains(search)
|| (r.RevisionHash != null && r.RevisionHash.Contains(search)));
}
return query;
}
/// <inheritdoc />
public async Task<DeploymentListPage> QueryDeploymentListPageAsync(
DeploymentListFilter filter,
int pageNumber,
int pageSize,
CancellationToken cancellationToken = default)
{
var page = Math.Max(1, pageNumber);
var size = Math.Clamp(pageSize, 1, DeploymentRecordSummary.MaxPageSize);
var query = BuildListQuery(filter, applyStatus: true);
// Count first: the pager needs a page count, and the caller must be able to
// tell "page 7 is now past the end" (a concurrent purge or a narrowed filter)
// from "page 7 is empty".
var totalCount = await query.CountAsync(cancellationToken);
// DeployedAt ties on rapid redeploys; without the Id tie-break the sort key
// is unstable and offset paging repeats or drops rows between pages.
var rows = await query
.OrderByDescending(r => r.DeployedAt)
.ThenByDescending(r => r.Id)
.Skip((page - 1) * size)
.Take(size)
.Select(r => new DeploymentListRow(
r.Id,
r.DeploymentId,
r.InstanceId,
r.InstanceUniqueName,
r.SiteId,
r.Status,
r.RevisionHash,
r.DeployedBy,
r.DeployedAt,
r.CompletedAt,
r.ErrorMessage))
.ToListAsync(cancellationToken);
return new DeploymentListPage(rows, totalCount);
}
/// <inheritdoc />
public async Task<DeploymentStatusCounts> GetDeploymentStatusCountsAsync(
DeploymentListFilter filter,
CancellationToken cancellationToken = default)
{
// applyStatus: false — the tiles are the status breakdown of the filtered
// set, so the active status filter is the one dimension they must NOT honour.
var grouped = await BuildListQuery(filter, applyStatus: false)
.GroupBy(r => r.Status)
.Select(g => new { Status = g.Key, Count = g.Count() })
.ToListAsync(cancellationToken);
return DeploymentStatusCounts.FromGrouped(grouped.Select(g => (g.Status, g.Count)));
}
/// <inheritdoc />
@@ -483,4 +602,27 @@ public class DeploymentManagerRepository : IDeploymentManagerRepository
{
return await _dbContext.SaveChangesAsync(cancellationToken);
}
/// <summary>
/// Flat shape of the deployment-record ⋈ instance join used to compose the list
/// query. A named type rather than an anonymous one so
/// <see cref="BuildListQuery"/> can hand the composed-but-unexecuted query back
/// to both the paging read and the count aggregation, keeping the two in
/// lockstep. Not a public contract — <see cref="DeploymentListRow"/> is what
/// leaves this class.
/// </summary>
private sealed class DeploymentInstanceJoin
{
public int Id { get; init; }
public string DeploymentId { get; init; } = string.Empty;
public int InstanceId { get; init; }
public string InstanceUniqueName { get; init; } = string.Empty;
public int SiteId { get; init; }
public DeploymentStatus Status { get; init; }
public string? RevisionHash { get; init; }
public string DeployedBy { get; init; } = string.Empty;
public DateTimeOffset DeployedAt { get; init; }
public DateTimeOffset? CompletedAt { get; init; }
public string? ErrorMessage { get; init; }
}
}