perf(applications): scope latest mail to filtered candidates

This commit is contained in:
cesnimda
2026-11-19 20:47:27 +01:00
parent 2a212c78d2
commit 1047dfa267
3 changed files with 44 additions and 12 deletions
+22 -11
View File
@@ -21,7 +21,7 @@ public sealed class JobQueryParityTests
[Theory]
[InlineData(250)]
[InlineData(2500)]
public async Task Ordinary_table_materialises_correspondence_groups_only_for_the_returned_page(int size)
public async Task Job_list_reads_correspondence_only_for_relevant_applications(int size)
{
await using var connection = new SqliteConnection("Data Source=:memory:");
await connection.OpenAsync();
@@ -36,7 +36,7 @@ public sealed class JobQueryParityTests
var jobs = Enumerable.Range(0, size).Select(i => new JobApplication
{
Company = company, OwnerUserId = "owner", JobTitle = $"Application {i:D4}", Status = "Applied",
DateApplied = new DateTime(2026, 10, 1),
DateApplied = new DateTime(2026, 10, 1), Location = i % 50 == 0 ? "Bergen" : "Oslo",
}).ToList();
db.JobApplications.AddRange(jobs);
await db.SaveChangesAsync();
@@ -62,20 +62,31 @@ public sealed class JobQueryParityTests
Assert.Equal(size, page.Total);
var expected = jobs.Skip(15).Take(15).Select(job => job.Id).ToArray();
Assert.Equal(expected, page.Items.Select(job => job.Id));
var (sql, parameters) = Assert.Single(aggregate.Commands);
await using var replay = connection.CreateCommand();
replay.CommandText = sql;
foreach (var parameter in parameters) replay.Parameters.AddWithValue(parameter.Key, parameter.Value ?? DBNull.Value);
var materialised = new List<int>();
await using (var reader = await replay.ExecuteReaderAsync())
while (await reader.ReadAsync()) materialised.Add(reader.GetInt32(0));
Assert.Equal(15, materialised.Count);
Assert.Equal(expected.Order(), materialised.Order());
async Task AssertMessageRows(int[] expectedIds)
{
var (sql, parameters) = Assert.Single(aggregate.Commands);
await using var replay = connection.CreateCommand();
replay.CommandText = sql;
foreach (var parameter in parameters) replay.Parameters.AddWithValue(parameter.Key, parameter.Value ?? DBNull.Value);
var materialised = new List<int>();
await using (var reader = await replay.ExecuteReaderAsync())
while (await reader.ReadAsync()) materialised.Add(reader.GetInt32(0));
Assert.Equal(expectedIds.Length, materialised.Count);
Assert.Equal(expectedIds.Order(), materialised.Order());
}
await AssertMessageRows(expected);
aggregate.Commands.Clear();
await controller.GetBoard();
await controller.GetAll(q: "absent");
Assert.Empty(aggregate.Commands);
var candidates = jobs.Where(job => job.Location == "Bergen").Select(job => job.Id).ToArray();
var filtered = await controller.GetAll(location: "Bergen", readiness: "needs-work", sortBy: "jobTitle");
var filteredPage = Assert.IsType<PagedResult<JobApplicationDto>>(Assert.IsType<OkObjectResult>(filtered.Result).Value);
Assert.Equal(candidates.Length, filteredPage.Total);
Assert.Equal(candidates.Take(15), filteredPage.Items.Select(job => job.Id));
await AssertMessageRows(candidates);
}
private sealed class CorrespondenceAggregateRecorder : DbCommandInterceptor
@@ -538,7 +538,9 @@ Canonical profile:
{
// Both filters depend on rule evaluation and workflow signals that include normalized
// note content, so evaluate before pagination to keep totals/pages correct.
var latestMessages = await LoadLatestMessagesAsync(_db.Correspondences.AsNoTracking());
var candidateIds = query.Select(j => j.Id);
var latestMessages = await LoadLatestMessagesAsync(_db.Correspondences.AsNoTracking()
.Where(message => candidateIds.Contains(message.JobApplicationId)));
var pre = await query.ToListAsync(cancellationToken);
var filtered = new List<JobApplication>();
foreach (var j in pre)
@@ -12,3 +12,22 @@ The focused SQLite test records and replays the actual SQL aggregate. Each fixtu
**PASSED** 36 selected Release tests with pinned SDK 9.0.317 and no restore. 18 JobQueryParity, 10 ApplicationCalendar, five RulesEngineProspect and three OwnershipGuard cases. The 16 existing parity cases cover ordering, filters, board, ownership and pagination. Two expected pre-fix failures are preserved. [Native red and final test logs](../ui-modernisation/evidence/perf-job-page/)
These counts measure rows returned by the aggregate under a disposable SQLite fixture. MariaDB plan/rows examined, API response time and production performance remain unmeasured. No index, migration or worker configuration changed.
## Filtered readiness and follow-up views
On 19 November the same SQLite regression fixture exposed a separate account-wide read in the derived-filter path. With a Bergen location filter, the old latest-message aggregate returned all 250 or 2,500 owned groups. Only five or 50 applications respectively were candidates for the readiness evaluation. The controller now limits the correspondence aggregate to the existing SQL candidate query before evaluating readiness or follow-up and paging. It uses a database subquery so the application need not send an unbounded list of IDs.
| Owned jobs with correspondence | Aggregate rows before | Aggregate rows after | Matching jobs on first page |
| ---: | ---: | ---: | ---: |
| 250 | 250 | 5 | 5 |
| 2,500 | 2,500 | 50 | 15 |
The test records and replays the actual EF-generated aggregate and compares its IDs with the fixture. Both new assertions failed on baseline `2a212c78`. The ordinary table still returned 15 page-scoped groups and the ordinary board and empty page issued no aggregate. The 38-test affected Release run passed, including table/board parity, ownership and workflow signals. The local native test receipt had SHA-256 `f84ca67a361dbcbca83ae762a02a5a90526a78d0e4ee8a3e1098773f0d29987b`.
Run the pinned .NET 9.0.317 SDK against `JobTrackerApi.Tests/JobTrackerApi.Tests.csproj` with `--no-restore -c Release` and the filter below.
```text
FullyQualifiedName~JobQueryParityTests|FullyQualifiedName~ApplicationCalendar|FullyQualifiedName~RulesEngineProspect|FullyQualifiedName~OwnershipGuard|FullyQualifiedName~JobApplicationsWorkflowSignalsTests
```
The measurements establish returned aggregate rows on these fixtures. Runtime on MariaDB, query plans for unselective searches and end-to-end latency remain open. The rollback unit is the filtered correspondence query change in `ReadJobsAsync`.