perf(search): prefix-by-default Deployments and config-audit search; bounded Contains opt-in (ScadaBridge#50)

Deployments free-text search and the config-audit EntityName filter no longer
emit LIKE '%term%' by default. Prefix (LIKE 'term%') is the default; substring
is an explicit opt-in (DeploymentSearchMode.Contains / entityNameContains)
always bounded by a DeployedAt/Timestamp window (explicit, else 30 days).
Migration PrefixSearchIndexes adds DeployedBy, RevisionHash and EntityName
indexes. Deployments page gains a 'Match anywhere' checkbox.

Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
Joseph Doherty
2026-11-09 15:42:56 -05:00
parent 0ce6633270
commit 47f3a703bf
15 changed files with 2620 additions and 44 deletions
+1
View File
@@ -229,6 +229,7 @@ spec for each is `docs/requirements/Component-<Name>.md`, and `README.md` carrie
- Host readiness gating: `/health/ready` endpoint, no traffic until operational. - Host readiness gating: `/health/ready` endpoint, no traffic until operational.
- EF Core migrations: auto-apply in dev, manual SQL scripts for production. Gotcha: `dotnet ef migrations add … --no-build` scaffolds off the previously-built DLL and can emit an **empty** migration — build first (drop the `--no-build`), and delete the empty `Migrations/` files if it happens (`migrations remove` needs a live DB). - EF Core migrations: auto-apply in dev, manual SQL scripts for production. Gotcha: `dotnet ef migrations add … --no-build` scaffolds off the previously-built DLL and can emit an **empty** migration — build first (drop the `--no-build`), and delete the empty `Migrations/` files if it happens (`migrations remove` needs a live DB).
- Audit logging absorbed into Configuration Database component (IAuditService). The config `AuditLogEntries` table stores **summaries** (a script body is recorded as length + SHA-256), caps `AfterStateJson` at 256 K chars, pages deterministically on `(Timestamp DESC, Id DESC)`, and has an optional purge: `ScadaBridge:ConfigAudit:RetentionDays` (default `0` = retained indefinitely), `PurgeBatchSize` 5000, `MaxBatchesPerTick` 200, `PurgeInterval` (bound with `ValidateOnStart` on Central). - Audit logging absorbed into Configuration Database component (IAuditService). The config `AuditLogEntries` table stores **summaries** (a script body is recorded as length + SHA-256), caps `AfterStateJson` at 256 K chars, pages deterministically on `(Timestamp DESC, Id DESC)`, and has an optional purge: `ScadaBridge:ConfigAudit:RetentionDays` (default `0` = retained indefinitely), `PurgeBatchSize` 5000, `MaxBatchesPerTick` 200, `PurgeInterval` (bound with `ValidateOnStart` on Central).
- **Search is prefix by default (ScadaBridge#50, register row 36).** The Deployments page search (instance name, deployment id, revision hash, user) and the config-audit `EntityName` filter are `LIKE 'term%'` (index-seekable); a substring match is an explicit opt-in (`DeploymentListFilter.SearchMode = Contains` / the page's "Match anywhere" checkbox; `GetAuditLogEntriesAsync(entityNameContains: true)`) that is ALWAYS bounded to a `DeployedAt`/`Timestamp` window (explicit `DeployedFrom`/`from`, else the last 30 days). Migration `PrefixSearchIndexes` (script `docs/plans/sql/PrefixSearchIndexes.sql`; not applied to QA/prod) adds the `DeployedBy`, `RevisionHash` and `AuditLogEntries.EntityName` indexes. Tests: `SearchPrefixQueryTests` (SQL-shape via `ToQueryString`).
- **Review remediation 2026-10-21** (8-slice system review, ~107 findings, 4 High; record `docs/plans/2026-10-21-system-review-remediation.md`). Decisions: CLI uses bearer-per-invocation (see Security); the never-read `Database:MachineDataDb` option was **removed** (it was a required boot key); config audit stores summaries + retention (above); a **bound Test Run** (Test Run against a deployed instance) now needs Deployer + site scope and writes an audit row; persisted static overrides **seed** script state at actor start without firing scripts (alarms still evaluate). Also changed: site deploy gate rejects non-actor-safe (`ActorNameRules`) instance/script/alarm/native-source names and Data Connection actor names are ASCII-sanitised with collision rejection; site gRPC addresses are validated on every write path (management + Transport import); artifact applies are serialised per site with a 10-min stuck-apply watchdog; `IDataConnection.WriteBatchAndWaitAsync` is **gone** (the script-level handshake composes `WriteTagBatchRequest` + waiter); `instance list --search` is a **prefix** match paged in SQL; command-plane failback probes readiness with a real RPC (probation 5 min); `Transport:SchemaVersionMajor` removed; migration `ReviewRemediationIndexes` (script `docs/plans/sql/ReviewRemediationIndexes.sql`; **QA/prod not yet applied**). New register rows 128-135; row 117 closed. - **Review remediation 2026-10-21** (8-slice system review, ~107 findings, 4 High; record `docs/plans/2026-10-21-system-review-remediation.md`). Decisions: CLI uses bearer-per-invocation (see Security); the never-read `Database:MachineDataDb` option was **removed** (it was a required boot key); config audit stores summaries + retention (above); a **bound Test Run** (Test Run against a deployed instance) now needs Deployer + site scope and writes an audit row; persisted static overrides **seed** script state at actor start without firing scripts (alarms still evaluate). Also changed: site deploy gate rejects non-actor-safe (`ActorNameRules`) instance/script/alarm/native-source names and Data Connection actor names are ASCII-sanitised with collision rejection; site gRPC addresses are validated on every write path (management + Transport import); artifact applies are serialised per site with a 10-min stuck-apply watchdog; `IDataConnection.WriteBatchAndWaitAsync` is **gone** (the script-level handshake composes `WriteTagBatchRequest` + waiter); `instance list --search` is a **prefix** match paged in SQL; command-plane failback probes readiness with a real RPC (probation 5 min); `Transport:SchemaVersionMajor` removed; migration `ReviewRemediationIndexes` (script `docs/plans/sql/ReviewRemediationIndexes.sql`; **QA/prod not yet applied**). New register rows 128-135; row 117 closed.
### Akka.NET Conventions ### Akka.NET Conventions
+26
View File
@@ -0,0 +1,26 @@
-- PrefixSearchIndexes (2026-11-09, ScadaBridge#50). Indexes backing the prefix search on the
-- Deployments page (DeployedBy, RevisionHash) and the config-audit EntityName filter.
BEGIN TRANSACTION;
ALTER TABLE [TemplateNativeAlarmSources] ADD [IsMesReportable] bit NOT NULL DEFAULT CAST(0 AS bit);
ALTER TABLE [InstanceNativeAlarmSourceOverrides] ADD [IsMesReportableOverride] bit NULL;
INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'20261109200211_AddNativeAlarmSourceMesReportable', N'10.0.13');
COMMIT;
GO
BEGIN TRANSACTION;
CREATE INDEX [IX_DeploymentRecords_DeployedBy] ON [DeploymentRecords] ([DeployedBy]);
CREATE INDEX [IX_DeploymentRecords_RevisionHash] ON [DeploymentRecords] ([RevisionHash]);
CREATE INDEX [IX_AuditLogEntries_EntityName] ON [AuditLogEntries] ([EntityName]);
INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'20261109203753_PrefixSearchIndexes', N'10.0.13');
COMMIT;
GO
@@ -75,6 +75,15 @@
data-test="deploy-search" data-test="deploy-search"
value="@_searchInput" value="@_searchInput"
@oninput="OnSearchInput" /> @oninput="OnSearchInput" />
<div class="form-check mt-1">
<input id="dep-search-contains" type="checkbox" class="form-check-input"
data-test="deploy-search-contains"
checked="@_searchContains"
@onchange="SetSearchContainsAsync" />
<label class="form-check-label small text-muted" for="dep-search-contains">
Match anywhere in the text (last 30 days only; default matches the start)
</label>
</div>
</div> </div>
<div class="col-auto"> <div class="col-auto">
<button class="btn btn-outline-secondary btn-sm" data-test="deploy-clear-filters" <button class="btn btn-outline-secondary btn-sm" data-test="deploy-clear-filters"
@@ -522,6 +531,19 @@
} }
} }
/// <summary>
/// Opt-in substring search. Scans, so the repository bounds it to the last 30 days
/// (ScadaBridge#50); the default is an index-friendly prefix match.
/// </summary>
private bool _searchContains;
private async Task SetSearchContainsAsync(ChangeEventArgs e)
{
_searchContains = e.Value is true;
_currentPage = 1;
await LoadDataAsync();
}
private async Task SetStatusFilterAsync(DeploymentStatus? status) private async Task SetStatusFilterAsync(DeploymentStatus? status)
{ {
// Clicking the active tile clears it — a toggle, so the tiles are both the // Clicking the active tile clears it — a toggle, so the tiles are both the
@@ -536,6 +558,7 @@
_statusFilter = null; _statusFilter = null;
_searchInput = string.Empty; _searchInput = string.Empty;
_appliedSearch = string.Empty; _appliedSearch = string.Empty;
_searchContains = false;
_currentPage = 1; _currentPage = 1;
// A cleared box must not be re-queried by an armed keystroke timer. // A cleared box must not be re-queried by an armed keystroke timer.
@@ -587,7 +610,8 @@
var filter = new DeploymentListFilter( var filter = new DeploymentListFilter(
Status: _statusFilter, Status: _statusFilter,
Search: _appliedSearch, Search: _appliedSearch,
SiteIdScope: siteScope); SiteIdScope: siteScope,
SearchMode: _searchContains ? DeploymentSearchMode.Contains : DeploymentSearchMode.Prefix);
var page = await DeploymentManagerRepository var page = await DeploymentManagerRepository
.QueryDeploymentListPageAsync(filter, pageToQuery, PageSize); .QueryDeploymentListPageAsync(filter, pageToQuery, PageSize);
@@ -56,11 +56,18 @@ public interface ICentralUiRepository
/// <param name="from">Optional start of date range filter.</param> /// <param name="from">Optional start of date range filter.</param>
/// <param name="to">Optional end of date range filter.</param> /// <param name="to">Optional end of date range filter.</param>
/// <param name="entityId">Optional entity ID filter.</param> /// <param name="entityId">Optional entity ID filter.</param>
/// <param name="entityName">Optional entity name filter.</param> /// <param name="entityName">
/// Optional entity name filter. A case-insensitive PREFIX match by default (index-friendly);
/// see <paramref name="entityNameContains"/>.
/// </param>
/// <param name="bundleImportId">Optional bundle import correlation ID filter.</param> /// <param name="bundleImportId">Optional bundle import correlation ID filter.</param>
/// <param name="page">One-based page number.</param> /// <param name="page">One-based page number.</param>
/// <param name="pageSize">Number of entries per page.</param> /// <param name="pageSize">Number of entries per page.</param>
/// <param name="cancellationToken">Cancellation token.</param> /// <param name="cancellationToken">Cancellation token.</param>
/// <param name="entityNameContains">
/// Opt in to a substring match on <paramref name="entityName"/>. This scans, so it is always
/// bounded to a time window: <paramref name="from"/>, or the last 30 days when null.
/// </param>
/// <returns>A task that resolves to a tuple of the matching entries and the total count.</returns> /// <returns>A task that resolves to a tuple of the matching entries and the total count.</returns>
Task<(IReadOnlyList<AuditLogEntry> Entries, int TotalCount)> GetAuditLogEntriesAsync( Task<(IReadOnlyList<AuditLogEntry> Entries, int TotalCount)> GetAuditLogEntriesAsync(
string? user = null, string? user = null,
@@ -73,7 +80,8 @@ public interface ICentralUiRepository
Guid? bundleImportId = null, Guid? bundleImportId = null,
int page = 1, int page = 1,
int pageSize = 50, int pageSize = 50,
CancellationToken cancellationToken = default); CancellationToken cancellationToken = default,
bool entityNameContains = false);
/// <summary>Persists pending changes to the underlying store.</summary> /// <summary>Persists pending changes to the underlying store.</summary>
/// <param name="cancellationToken">Cancellation token.</param> /// <param name="cancellationToken">Cancellation token.</param>
@@ -21,9 +21,11 @@ namespace ZB.MOM.WW.ScadaBridge.Commons.Types.Deployment;
/// must keep showing its own total while that same status is the active filter. /// must keep showing its own total while that same status is the active filter.
/// </param> /// </param>
/// <param name="Search"> /// <param name="Search">
/// Free-text term matched (case-insensitively, as a substring) against the /// Free-text term matched (case-insensitively) against the instance unique name,
/// instance unique name, the deployment id, the revision hash and the initiating /// the deployment id, the revision hash and the initiating user. By default
/// user. Blank/whitespace is treated as unconstrained. /// (<see cref="DeploymentSearchMode.Prefix"/>) it is a PREFIX match, which can seek
/// the column indexes; <see cref="DeploymentSearchMode.Contains"/> opts back into the
/// substring match, bounded to a time window. Blank/whitespace is unconstrained.
/// </param> /// </param>
/// <param name="SiteIdScope"> /// <param name="SiteIdScope">
/// When non-null, restrict to deployments whose instance belongs to one of these /// When non-null, restrict to deployments whose instance belongs to one of these
@@ -33,12 +35,33 @@ namespace ZB.MOM.WW.ScadaBridge.Commons.Types.Deployment;
/// "no filter"; <see langword="null"/> is the system-wide case. /// "no filter"; <see langword="null"/> is the system-wide case.
/// </param> /// </param>
/// <param name="InstanceId">Restrict to one instance's deployment history, or <see langword="null"/> for all.</param> /// <param name="InstanceId">Restrict to one instance's deployment history, or <see langword="null"/> for all.</param>
/// <param name="SearchMode">Prefix (default, index-friendly) or bounded Contains. Only meaningful with <paramref name="Search"/>.</param>
/// <param name="DeployedFrom">
/// Lower bound on <c>DeployedAt</c>. Optional for Prefix searches; in Contains mode a
/// window is ALWAYS applied: this value, or the last
/// <see cref="DeploymentListFilter.ContainsDefaultWindow"/> when null.
/// </param>
public sealed record DeploymentListFilter( public sealed record DeploymentListFilter(
DeploymentStatus? Status = null, DeploymentStatus? Status = null,
string? Search = null, string? Search = null,
IReadOnlyCollection<int>? SiteIdScope = null, IReadOnlyCollection<int>? SiteIdScope = null,
int? InstanceId = null) int? InstanceId = null,
DeploymentSearchMode SearchMode = DeploymentSearchMode.Prefix,
DateTimeOffset? DeployedFrom = null)
{ {
/// <summary>Time window applied to a Contains search that supplies no explicit <see cref="DeployedFrom"/>.</summary>
public static readonly TimeSpan ContainsDefaultWindow = TimeSpan.FromDays(30);
/// <summary>
/// The effective <c>DeployedAt</c> lower bound at <paramref name="nowUtc"/>: the explicit
/// <see cref="DeployedFrom"/>, else (Contains search only) now minus
/// <see cref="ContainsDefaultWindow"/>, else <see langword="null"/>.
/// </summary>
public DateTimeOffset? EffectiveDeployedFrom(DateTimeOffset nowUtc) =>
DeployedFrom ?? (NormalizedSearch is not null && SearchMode == DeploymentSearchMode.Contains
? nowUtc - ContainsDefaultWindow
: null);
/// <summary> /// <summary>
/// The trimmed search term, or <see langword="null"/> when the term is absent /// The trimmed search term, or <see langword="null"/> when the term is absent
/// or whitespace-only. Repository implementations use this rather than /// or whitespace-only. Repository implementations use this rather than
@@ -48,3 +71,13 @@ public sealed record DeploymentListFilter(
public string? NormalizedSearch => public string? NormalizedSearch =>
string.IsNullOrWhiteSpace(Search) ? null : Search.Trim(); string.IsNullOrWhiteSpace(Search) ? null : Search.Trim();
} }
/// <summary>How <see cref="DeploymentListFilter.Search"/> is matched.</summary>
public enum DeploymentSearchMode
{
/// <summary>Case-insensitive prefix (<c>LIKE 'term%'</c>); can use the column indexes.</summary>
Prefix = 0,
/// <summary>Substring (<c>LIKE '%term%'</c>); scans, so it is always bounded by a DeployedAt window.</summary>
Contains = 1,
}
@@ -40,6 +40,8 @@ public class AuditLogEntryConfiguration : IEntityTypeConfiguration<AuditLogEntry
builder.HasIndex(a => a.EntityType); builder.HasIndex(a => a.EntityType);
builder.HasIndex(a => a.EntityId); builder.HasIndex(a => a.EntityId);
builder.HasIndex(a => a.Action); builder.HasIndex(a => a.Action);
// Prefix-search leg of the config-audit EntityName filter (ScadaBridge#50).
builder.HasIndex(a => a.EntityName).HasDatabaseName("IX_AuditLogEntries_EntityName");
// Review-remediation composite indexes (ReviewRemediationIndexes). The single-column // Review-remediation composite indexes (ReviewRemediationIndexes). The single-column
// Timestamp/User/EntityType indexes above are kept: (Timestamp DESC, Id DESC) only // Timestamp/User/EntityType indexes above are kept: (Timestamp DESC, Id DESC) only
// subsumes Timestamp conceptually, and dropping existing indexes is deliberately // subsumes Timestamp conceptually, and dropping existing indexes is deliberately
@@ -40,6 +40,11 @@ public class DeploymentRecordConfiguration : IEntityTypeConfiguration<Deployment
builder.HasIndex(d => d.DeploymentId).IsUnique(); builder.HasIndex(d => d.DeploymentId).IsUnique();
builder.HasIndex(d => d.InstanceId); builder.HasIndex(d => d.InstanceId);
// Prefix-search legs of the Deployments page free-text search (ScadaBridge#50):
// DeploymentId and Instance.UniqueName are already uniquely indexed.
builder.HasIndex(d => d.DeployedBy).HasDatabaseName("IX_DeploymentRecords_DeployedBy");
builder.HasIndex(d => d.RevisionHash).HasDatabaseName("IX_DeploymentRecords_RevisionHash");
// Deployment-list sort key. Every list read orders DeployedAt DESC, Id DESC // Deployment-list sort key. Every list read orders DeployedAt DESC, Id DESC
// (DeploymentManagerRepository.QueryDeploymentListPageAsync, which register // (DeploymentManagerRepository.QueryDeploymentListPageAsync, which register
// row 37 made the single paged read behind BOTH the Central UI page and the // row 37 made the single paged read behind BOTH the Central UI page and the
@@ -0,0 +1,45 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Migrations
{
/// <inheritdoc />
public partial class PrefixSearchIndexes : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateIndex(
name: "IX_DeploymentRecords_DeployedBy",
table: "DeploymentRecords",
column: "DeployedBy");
migrationBuilder.CreateIndex(
name: "IX_DeploymentRecords_RevisionHash",
table: "DeploymentRecords",
column: "RevisionHash");
migrationBuilder.CreateIndex(
name: "IX_AuditLogEntries_EntityName",
table: "AuditLogEntries",
column: "EntityName");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropIndex(
name: "IX_DeploymentRecords_DeployedBy",
table: "DeploymentRecords");
migrationBuilder.DropIndex(
name: "IX_DeploymentRecords_RevisionHash",
table: "DeploymentRecords");
migrationBuilder.DropIndex(
name: "IX_AuditLogEntries_EntityName",
table: "AuditLogEntries");
}
}
}
@@ -92,6 +92,9 @@ namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Migrations
b.HasIndex("EntityId"); b.HasIndex("EntityId");
b.HasIndex("EntityName")
.HasDatabaseName("IX_AuditLogEntries_EntityName");
b.HasIndex("EntityType"); b.HasIndex("EntityType");
b.HasIndex("Timestamp"); b.HasIndex("Timestamp");
@@ -283,11 +286,17 @@ namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Migrations
b.HasKey("Id"); b.HasKey("Id");
b.HasIndex("DeployedBy")
.HasDatabaseName("IX_DeploymentRecords_DeployedBy");
b.HasIndex("DeploymentId") b.HasIndex("DeploymentId")
.IsUnique(); .IsUnique();
b.HasIndex("InstanceId"); b.HasIndex("InstanceId");
b.HasIndex("RevisionHash")
.HasDatabaseName("IX_DeploymentRecords_RevisionHash");
b.HasIndex("DeployedAt", "Id") b.HasIndex("DeployedAt", "Id")
.IsDescending() .IsDescending()
.HasDatabaseName("IX_DeploymentRecords_DeployedAt_Id"); .HasDatabaseName("IX_DeploymentRecords_DeployedAt_Id");
@@ -127,33 +127,12 @@ public class CentralUiRepository : ICentralUiRepository
Guid? bundleImportId = null, Guid? bundleImportId = null,
int page = 1, int page = 1,
int pageSize = 50, int pageSize = 50,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default,
bool entityNameContains = false)
{ {
var query = _context.AuditLogEntries.AsNoTracking().AsQueryable(); var query = BuildAuditLogQuery(
user, entityType, action, from, to, entityId, entityName, bundleImportId,
if (!string.IsNullOrWhiteSpace(user)) entityNameContains, DateTimeOffset.UtcNow);
query = query.Where(a => a.User == user);
if (!string.IsNullOrWhiteSpace(entityType))
query = query.Where(a => a.EntityType == entityType);
if (!string.IsNullOrWhiteSpace(action))
query = query.Where(a => a.Action == action);
if (from.HasValue)
query = query.Where(a => a.Timestamp >= from.Value);
if (to.HasValue)
query = query.Where(a => a.Timestamp <= to.Value);
if (!string.IsNullOrWhiteSpace(entityId))
query = query.Where(a => a.EntityId == entityId);
if (!string.IsNullOrWhiteSpace(entityName))
query = query.Where(a => a.EntityName.Contains(entityName));
if (bundleImportId is Guid bundleId)
query = query.Where(a => a.BundleImportId == bundleId);
var totalCount = await query.CountAsync(cancellationToken); var totalCount = await query.CountAsync(cancellationToken);
@@ -169,6 +148,51 @@ public class CentralUiRepository : ICentralUiRepository
return (entries, totalCount); return (entries, totalCount);
} }
/// <summary>Default window for an opt-in entity-name substring search with no explicit <c>from</c>.</summary>
internal static readonly TimeSpan EntityNameContainsDefaultWindow = TimeSpan.FromDays(30);
internal IQueryable<AuditLogEntry> BuildAuditLogQuery(
string? user, string? entityType, string? action, DateTimeOffset? from, DateTimeOffset? to,
string? entityId, string? entityName, Guid? bundleImportId, bool entityNameContains, DateTimeOffset nowUtc)
{
var query = _context.AuditLogEntries.AsNoTracking().AsQueryable();
if (!string.IsNullOrWhiteSpace(user))
query = query.Where(a => a.User == user);
if (!string.IsNullOrWhiteSpace(entityType))
query = query.Where(a => a.EntityType == entityType);
if (!string.IsNullOrWhiteSpace(action))
query = query.Where(a => a.Action == action);
// A substring name search scans, so it is never unbounded: without an explicit
// `from` it is limited to the last EntityNameContainsDefaultWindow (ScadaBridge#50).
var contains = entityNameContains && !string.IsNullOrWhiteSpace(entityName);
var effectiveFrom = from ?? (contains ? nowUtc - EntityNameContainsDefaultWindow : (DateTimeOffset?)null);
if (effectiveFrom.HasValue)
{
var lowerBound = effectiveFrom.Value;
query = query.Where(a => a.Timestamp >= lowerBound);
}
if (to.HasValue)
query = query.Where(a => a.Timestamp <= to.Value);
if (!string.IsNullOrWhiteSpace(entityId))
query = query.Where(a => a.EntityId == entityId);
if (!string.IsNullOrWhiteSpace(entityName))
query = contains
? query.Where(a => a.EntityName.Contains(entityName))
: query.Where(a => a.EntityName.StartsWith(entityName));
if (bundleImportId is Guid bundleId)
query = query.Where(a => a.BundleImportId == bundleId);
return query;
}
/// <inheritdoc /> /// <inheritdoc />
public async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default) public async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
{ {
@@ -57,7 +57,7 @@ public class DeploymentManagerRepository : IDeploymentManagerRepository
/// status-count aggregation needs every status of the otherwise-identical set. /// status-count aggregation needs every status of the otherwise-identical set.
/// </param> /// </param>
/// <returns>The composed, unordered, untracked query.</returns> /// <returns>The composed, unordered, untracked query.</returns>
private IQueryable<DeploymentInstanceJoin> BuildListQuery(DeploymentListFilter filter, bool applyStatus) internal IQueryable<DeploymentInstanceJoin> BuildListQuery(DeploymentListFilter filter, bool applyStatus)
{ {
ArgumentNullException.ThrowIfNull(filter); ArgumentNullException.ThrowIfNull(filter);
@@ -93,16 +93,26 @@ public class DeploymentManagerRepository : IDeploymentManagerRepository
query = query.Where(r => scope.Contains(r.SiteId)); query = query.Where(r => scope.Contains(r.SiteId));
} }
if (filter.EffectiveDeployedFrom(DateTimeOffset.UtcNow) is { } from)
query = query.Where(r => r.DeployedAt >= from);
if (filter.NormalizedSearch is { } search) if (filter.NormalizedSearch is { } search)
{ {
// Substring match across the four identifiers a deployment is findable // Default: PREFIX match across the four identifiers a deployment is findable
// by. Translated to LIKE '%term%' by both providers; case-insensitivity // by (LIKE 'term%', index-seekable; case-insensitivity is the database
// is the database collation's (SQL Server default CI, SQLite ASCII LIKE). // collation's). Contains is the explicit opt-in scan (LIKE '%term%') and is
query = query.Where(r => // always bounded by the DeployedAt window applied above (ScadaBridge#50).
r.InstanceUniqueName.Contains(search) query = filter.SearchMode == DeploymentSearchMode.Contains
|| r.DeploymentId.Contains(search) ? query.Where(r =>
|| r.DeployedBy.Contains(search) r.InstanceUniqueName.Contains(search)
|| (r.RevisionHash != null && r.RevisionHash.Contains(search))); || r.DeploymentId.Contains(search)
|| r.DeployedBy.Contains(search)
|| (r.RevisionHash != null && r.RevisionHash.Contains(search)))
: query.Where(r =>
r.InstanceUniqueName.StartsWith(search)
|| r.DeploymentId.StartsWith(search)
|| r.DeployedBy.StartsWith(search)
|| (r.RevisionHash != null && r.RevisionHash.StartsWith(search)));
} }
return query; return query;
@@ -873,7 +883,7 @@ public class DeploymentManagerRepository : IDeploymentManagerRepository
/// lockstep. Not a public contract — <see cref="DeploymentListRow"/> is what /// lockstep. Not a public contract — <see cref="DeploymentListRow"/> is what
/// leaves this class. /// leaves this class.
/// </summary> /// </summary>
private sealed class DeploymentInstanceJoin internal sealed class DeploymentInstanceJoin
{ {
/// <summary>Deployment record primary key.</summary> /// <summary>Deployment record primary key.</summary>
public int Id { get; init; } public int Id { get; init; }
@@ -7,6 +7,10 @@
<TreatWarningsAsErrors>true</TreatWarningsAsErrors> <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<InternalsVisibleTo Include="ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" /> <PackageReference Include="Microsoft.EntityFrameworkCore" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" /> <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" />
@@ -258,7 +258,7 @@ public class DeploymentListQueryRepositoryTests : IDisposable
/// <param name="expected">The expected.</param> /// <param name="expected">The expected.</param>
/// <returns>A task that represents the asynchronous operation.</returns> /// <returns>A task that represents the asynchronous operation.</returns>
[Theory] [Theory]
[InlineData("Filler", "dep-a")] // instance unique name [InlineData("Line3", "dep-a")] // instance unique name (prefix)
[InlineData("dep-b", "dep-b")] // deployment id [InlineData("dep-b", "dep-b")] // deployment id
[InlineData("carol", "dep-b")] // initiating user [InlineData("carol", "dep-b")] // initiating user
[InlineData("rev-a", "dep-a")] // revision hash [InlineData("rev-a", "dep-a")] // revision hash
@@ -0,0 +1,201 @@
using Microsoft.EntityFrameworkCore;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Audit;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Deployment;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Instances;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Sites;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Deployment;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
using ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Repositories;
namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests;
/// <summary>
/// ScadaBridge#50: the Deployments and config-audit free-text searches must not use a
/// leading-wildcard LIKE by default. Query-shape tests render the SQL Server provider's SQL
/// (no connection needed); result tests run on SQLite.
/// </summary>
public class SearchPrefixQueryTests : IDisposable
{
private readonly ScadaBridgeDbContext _context = SqliteTestHelper.CreateInMemoryContext();
private readonly DeploymentManagerRepository _deployments;
private readonly CentralUiRepository _audit;
/// <summary>Initializes a new instance of the <c>SearchPrefixQueryTests</c> class.</summary>
public SearchPrefixQueryTests()
{
_deployments = new DeploymentManagerRepository(_context);
_audit = new CentralUiRepository(_context);
}
/// <summary>Releases the resources held by the test fixture.</summary>
public void Dispose()
{
_context.Database.CloseConnection();
_context.Dispose();
GC.SuppressFinalize(this);
}
private static ScadaBridgeDbContext SqlServerContext() =>
new(new DbContextOptionsBuilder<ScadaBridgeDbContext>()
.UseSqlServer("Server=unused;Database=unused;")
.Options);
// ── Query shape (SQL Server SQL) ─────────────────────────────────────────
/// <summary>Default deployment search renders a trailing-wildcard-only LIKE and no time window.</summary>
[Fact]
public void Deployments_DefaultSearch_HasNoLeadingWildcard()
{
using var ctx = SqlServerContext();
var sql = new DeploymentManagerRepository(ctx)
.BuildListQuery(new DeploymentListFilter(Search: "Line3"), applyStatus: true)
.ToQueryString();
// EF parameterises the LIKE pattern: assert on the declared pattern values.
Assert.Contains("LIKE @search_startswith ESCAPE", sql);
Assert.Contains("= N'Line3%';", sql);
Assert.DoesNotContain("= N'%", sql);
Assert.DoesNotContain("_contains", sql);
Assert.DoesNotContain("DeployedAt] >=", sql);
}
/// <summary>The Contains opt-in keeps the substring scan but is always time-bounded.</summary>
[Fact]
public void Deployments_ContainsSearch_HasLeadingWildcard_AndIsTimeBounded()
{
using var ctx = SqlServerContext();
var sql = new DeploymentManagerRepository(ctx)
.BuildListQuery(new DeploymentListFilter(Search: "Filler", SearchMode: DeploymentSearchMode.Contains), applyStatus: true)
.ToQueryString();
Assert.Contains("= N'%Filler%';", sql);
Assert.Contains("_contains", sql);
Assert.Contains("DeployedAt] >=", sql);
}
/// <summary>Default audit entity-name filter is a prefix match with no window.</summary>
[Fact]
public void AuditLog_DefaultEntityName_HasNoLeadingWildcard_NoWindow()
{
using var ctx = SqlServerContext();
var sql = new CentralUiRepository(ctx)
.BuildAuditLogQuery(null, null, null, null, null, null, "Pump", null, false, DateTimeOffset.UtcNow)
.ToQueryString();
Assert.Contains("LIKE @entityName_startswith ESCAPE", sql);
Assert.Contains("= N'Pump%';", sql);
Assert.DoesNotContain("= N'%", sql);
Assert.DoesNotContain("[Timestamp] >=", sql);
}
/// <summary>The audit Contains opt-in is bounded by the default window.</summary>
[Fact]
public void AuditLog_ContainsEntityName_IsBoundedByDefaultWindow()
{
using var ctx = SqlServerContext();
var sql = new CentralUiRepository(ctx)
.BuildAuditLogQuery(null, null, null, null, null, null, "Pump", null, true, DateTimeOffset.UtcNow)
.ToQueryString();
Assert.Contains("= N'%Pump%';", sql);
Assert.Contains("[Timestamp] >=", sql);
}
/// <summary>The effective window applies only to Contains searches or an explicit bound.</summary>
[Fact]
public void EffectiveDeployedFrom_OnlyForContains_OrExplicit()
{
var now = new DateTimeOffset(2026, 11, 9, 0, 0, 0, TimeSpan.Zero);
Assert.Null(new DeploymentListFilter(Search: "x").EffectiveDeployedFrom(now));
Assert.Null(new DeploymentListFilter(SearchMode: DeploymentSearchMode.Contains).EffectiveDeployedFrom(now));
Assert.Equal(now.AddDays(-30),
new DeploymentListFilter(Search: "x", SearchMode: DeploymentSearchMode.Contains).EffectiveDeployedFrom(now));
var explicitFrom = now.AddDays(-2);
Assert.Equal(explicitFrom,
new DeploymentListFilter(Search: "x", SearchMode: DeploymentSearchMode.Contains, DeployedFrom: explicitFrom).EffectiveDeployedFrom(now));
}
// ── Results (SQLite) ─────────────────────────────────────────────────────
private async Task SeedDeploymentsAsync()
{
var site = new Site("S1", "S1");
_context.Sites.Add(site);
var template = new Template("T");
_context.Templates.Add(template);
await _context.SaveChangesAsync();
var a = new Instance("Line3.Filler") { SiteId = site.Id, TemplateId = template.Id };
var b = new Instance("Line4.Filler") { SiteId = site.Id, TemplateId = template.Id };
_context.Instances.AddRange(a, b);
await _context.SaveChangesAsync();
_context.DeploymentRecords.AddRange(
new DeploymentRecord("dep-old", "alice") { InstanceId = a.Id, Status = DeploymentStatus.Success, DeployedAt = DateTimeOffset.UtcNow.AddDays(-90) },
new DeploymentRecord("dep-new", "alice") { InstanceId = b.Id, Status = DeploymentStatus.Success, DeployedAt = DateTimeOffset.UtcNow.AddDays(-1) });
await _context.SaveChangesAsync();
}
/// <summary>Prefix search does not match mid-string, is case-insensitive, finds the prefix.</summary>
[Fact]
public async Task Deployments_Prefix_DoesNotMatchInTheMiddle()
{
await SeedDeploymentsAsync();
var mid = await _deployments.QueryDeploymentListPageAsync(new DeploymentListFilter(Search: "Filler"), 1, 25);
Assert.Equal(0, mid.TotalCount);
var prefix = await _deployments.QueryDeploymentListPageAsync(new DeploymentListFilter(Search: "line3"), 1, 25);
Assert.Equal("dep-old", Assert.Single(prefix.Rows).DeploymentId);
}
/// <summary>Contains matches mid-string but only inside the window; counts agree.</summary>
[Fact]
public async Task Deployments_Contains_MatchesInTheMiddle_ButOnlyWithinTheWindow()
{
await SeedDeploymentsAsync();
var page = await _deployments.QueryDeploymentListPageAsync(
new DeploymentListFilter(Search: "Filler", SearchMode: DeploymentSearchMode.Contains), 1, 25);
Assert.Equal("dep-new", Assert.Single(page.Rows).DeploymentId);
var wide = await _deployments.QueryDeploymentListPageAsync(
new DeploymentListFilter(Search: "Filler", SearchMode: DeploymentSearchMode.Contains,
DeployedFrom: DateTimeOffset.UtcNow.AddDays(-365)), 1, 25);
Assert.Equal(2, wide.TotalCount);
var counts = await _deployments.GetDeploymentStatusCountsAsync(
new DeploymentListFilter(Search: "Filler", SearchMode: DeploymentSearchMode.Contains));
Assert.Equal(1, counts.Total);
}
/// <summary>A literal percent is not a wildcard in prefix mode.</summary>
[Fact]
public async Task Deployments_Prefix_TreatsPercentAsLiteral()
{
await SeedDeploymentsAsync();
var page = await _deployments.QueryDeploymentListPageAsync(new DeploymentListFilter(Search: "%"), 1, 25);
Assert.Equal(0, page.TotalCount);
}
/// <summary>Audit entity-name: prefix by default, bounded Contains on opt-in.</summary>
[Fact]
public async Task AuditLog_EntityName_PrefixByDefault_ContainsOptInWithinWindow()
{
_context.AuditLogEntries.AddRange(
new AuditLogEntry("a", "Create", "Template", "1", "PumpStation") { Timestamp = DateTimeOffset.UtcNow },
new AuditLogEntry("a", "Create", "Template", "2", "BigPump") { Timestamp = DateTimeOffset.UtcNow },
new AuditLogEntry("a", "Create", "Template", "3", "OldPump") { Timestamp = DateTimeOffset.UtcNow.AddDays(-90) });
await _context.SaveChangesAsync();
var (prefix, _) = await _audit.GetAuditLogEntriesAsync(entityName: "pump");
Assert.Equal("PumpStation", Assert.Single(prefix).EntityName);
var (contains, _) = await _audit.GetAuditLogEntriesAsync(entityName: "Pump", entityNameContains: true);
Assert.Equal(new[] { "BigPump", "PumpStation" }, contains.Select(e => e.EntityName).OrderBy(n => n));
var (wide, _) = await _audit.GetAuditLogEntriesAsync(
entityName: "Pump", entityNameContains: true, from: DateTimeOffset.UtcNow.AddDays(-365));
Assert.Equal(3, wide.Count);
}
}