perf(applications): bound correspondence reads to the current page
This commit is contained in:
@@ -542,8 +542,7 @@ Canonical profile:
|
||||
var settings = await GetCachedRuleSettingsAsync(cancellationToken);
|
||||
var now = DateTime.Now;
|
||||
|
||||
var lastMsg = await _db.Correspondences
|
||||
.AsNoTracking()
|
||||
Task<Dictionary<int, DateTime?>> LoadLatestMessagesAsync(IQueryable<Correspondence> messages) => messages
|
||||
.GroupBy(c => c.JobApplicationId)
|
||||
.Select(g => new { JobApplicationId = g.Key, Last = g.Max(x => x.Date) })
|
||||
.ToDictionaryAsync(x => x.JobApplicationId, x => (DateTime?)x.Last, cancellationToken);
|
||||
@@ -556,11 +555,12 @@ 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 pre = await query.ToListAsync(cancellationToken);
|
||||
var filtered = new List<JobApplication>();
|
||||
foreach (var j in pre)
|
||||
{
|
||||
lastMsg.TryGetValue(j.Id, out var lm);
|
||||
latestMessages.TryGetValue(j.Id, out var lm);
|
||||
var d = RulesEngine.Evaluate(settings, j, now, lm);
|
||||
var signal = BuildWorkflowSignal(j, d);
|
||||
var matchesFollowUp = !needsFollowUp || d.NeedsFollowUp;
|
||||
@@ -592,7 +592,7 @@ Canonical profile:
|
||||
var dtoItems = new List<JobApplicationDto>();
|
||||
foreach (var j in pageItems)
|
||||
{
|
||||
lastMsg.TryGetValue(j.Id, out var lm);
|
||||
latestMessages.TryGetValue(j.Id, out var lm);
|
||||
var d = RulesEngine.Evaluate(settings, j, now, lm);
|
||||
dtoItems.Add(BuildJobApplicationDto(j, d));
|
||||
}
|
||||
@@ -623,6 +623,12 @@ Canonical profile:
|
||||
.Take(pageSize)
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
if (items.Count == 0) return Ok(new PagedResult<JobApplicationDto>([], total, page, pageSize));
|
||||
|
||||
var pageIds = items.Select(job => job.Id).ToArray();
|
||||
var lastMsg = await LoadLatestMessagesAsync(_db.Correspondences.AsNoTracking()
|
||||
.Where(message => pageIds.Contains(message.JobApplicationId)));
|
||||
|
||||
var dtos = new List<JobApplicationDto>();
|
||||
foreach (var j in items)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user