Optimize exam publishing and experiment grade aggregation

This commit is contained in:
biss committed 2026-12-01 10:07:28 +08:00
1 parent 1d5abdfa96
commit 167af46687
2 files changed
+13 -10

No files matched your search

@@ -126,6 +126,11 @@ public sealed class ExamPublishJobProcessor(
.LoadForTasksAsync(db, taskIds, ct))
.GroupBy(x => x.TeachingTaskId)
.ToDictionary(x => x.Key, x => x.Count());
var seatCounts = await db.ExamSeats.AsNoTracking()
.Where(seat => seat.ExamSession!.ExamPlanId == job.PlanId)
.GroupBy(seat => seat.ExamSessionId)
.Select(group => new { SessionId = group.Key, Count = group.Count() })
.ToDictionaryAsync(item => item.SessionId, item => item.Count, ct);
// Step 4: validate each session's assignment completeness
var unassignedSessionIds = new List<Guid>();
@@ -134,11 +139,7 @@ public sealed class ExamPublishJobProcessor(
var rosterCount = rosterCounts.GetValueOrDefault(session.TeachingTaskId);
if (session.RoomLinkCount > 0)
{
// Has room links — check seat assignment via separate query
var assignedSeatCount = await db.ExamSeats
.CountAsync(seat =>
seat.ExamSessionId == session.Id,
ct);
var assignedSeatCount = seatCounts.GetValueOrDefault(session.Id);
if (assignedSeatCount != rosterCount)
unassignedSessionIds.Add(session.Id);
}
@@ -23,10 +23,11 @@ public static class ExperimentGradeAggregationService
.ToArray();
var refreshedAt = DateTime.UtcNow;
var totalWeight = sheets.Sum(x => x.ContributionWeight);
var scoresBySheet = sheets.Select(sheet => sheet.Scores.ToLookup(score => score.StudentId)).ToArray();
foreach (var studentId in studentIds)
{
var score = CalculateStudentScore(sheets, studentId);
var score = CalculateStudentScore(sheets, scoresBySheet, studentId);
if (!existing.Remove(studentId, out var aggregate))
{
aggregate = new Domain.Academic.ExperimentCourseGrade
@@ -100,16 +101,17 @@ public static class ExperimentGradeAggregationService
.ToListAsync(cancellationToken);
private static decimal? CalculateStudentScore(
IReadOnlyCollection<PublishedExperimentSheet> sheets,
IReadOnlyList<PublishedExperimentSheet> sheets,
IReadOnlyList<ILookup<Guid, PublishedExperimentScore>> scoresBySheet,
Guid studentId)
{
decimal weightedTotal = 0;
decimal totalWeight = 0;
if (sheets.Count == 0) return null;
foreach (var sheet in sheets)
for (var index = 0; index < sheets.Count; index++)
{
var score = sheet.Scores.FirstOrDefault(x =>
x.StudentId == studentId);
var sheet = sheets[index];
var score = scoresBySheet[index][studentId].FirstOrDefault();
if (score?.TotalScore is not decimal totalScore) return null;
weightedTotal += totalScore * sheet.ContributionWeight;
totalWeight += sheet.ContributionWeight;