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
@@ -40,6 +40,8 @@ public class AuditLogEntryConfiguration : IEntityTypeConfiguration<AuditLogEntry
builder.HasIndex(a => a.EntityType);
builder.HasIndex(a => a.EntityId);
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
// Timestamp/User/EntityType indexes above are kept: (Timestamp DESC, Id DESC) only
// 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.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
// (DeploymentManagerRepository.QueryDeploymentListPageAsync, which register
// 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("EntityName")
.HasDatabaseName("IX_AuditLogEntries_EntityName");
b.HasIndex("EntityType");
b.HasIndex("Timestamp");
@@ -283,11 +286,17 @@ namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Migrations
b.HasKey("Id");
b.HasIndex("DeployedBy")
.HasDatabaseName("IX_DeploymentRecords_DeployedBy");
b.HasIndex("DeploymentId")
.IsUnique();
b.HasIndex("InstanceId");
b.HasIndex("RevisionHash")
.HasDatabaseName("IX_DeploymentRecords_RevisionHash");
b.HasIndex("DeployedAt", "Id")
.IsDescending()
.HasDatabaseName("IX_DeploymentRecords_DeployedAt_Id");
@@ -127,33 +127,12 @@ public class CentralUiRepository : ICentralUiRepository
Guid? bundleImportId = null,
int page = 1,
int pageSize = 50,
CancellationToken cancellationToken = default)
CancellationToken cancellationToken = default,
bool entityNameContains = false)
{
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);
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 query = BuildAuditLogQuery(
user, entityType, action, from, to, entityId, entityName, bundleImportId,
entityNameContains, DateTimeOffset.UtcNow);
var totalCount = await query.CountAsync(cancellationToken);
@@ -169,6 +148,51 @@ public class CentralUiRepository : ICentralUiRepository
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 />
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.
/// </param>
/// <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);
@@ -93,16 +93,26 @@ public class DeploymentManagerRepository : IDeploymentManagerRepository
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)
{
// 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)));
// Default: PREFIX match across the four identifiers a deployment is findable
// by (LIKE 'term%', index-seekable; case-insensitivity is the database
// collation's). Contains is the explicit opt-in scan (LIKE '%term%') and is
// always bounded by the DeployedAt window applied above (ScadaBridge#50).
query = filter.SearchMode == DeploymentSearchMode.Contains
? query.Where(r =>
r.InstanceUniqueName.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;
@@ -873,7 +883,7 @@ public class DeploymentManagerRepository : IDeploymentManagerRepository
/// lockstep. Not a public contract — <see cref="DeploymentListRow"/> is what
/// leaves this class.
/// </summary>
private sealed class DeploymentInstanceJoin
internal sealed class DeploymentInstanceJoin
{
/// <summary>Deployment record primary key.</summary>
public int Id { get; init; }
@@ -7,6 +7,10 @@
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
<ItemGroup>
<InternalsVisibleTo Include="ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" />