perf: aggregate batch status statistics in a single query

This commit is contained in:
2026-10-18 12:14:42 +08:00
parent 7e2cdbd907
commit 000638718c
2 changed files with 48 additions and 5 deletions
+16 -5
View File
@@ -468,12 +468,15 @@ def create_batch(data: BatchInput, db: Session = Depends(get_db)):
return services.create_batch(data, db)
def status_count_dict(counts: dict) -> dict:
return {"status_counts": counts, "needs_review": counts.get("needs_review", 0),
"unscorable": counts.get("unscorable", 0), "failed": counts.get("failed", 0)}
def batch_status_counts(db, batch_id: int) -> dict:
rows = db.execute(select(CriterionScore.status, func.count(CriterionScore.id))
.where(CriterionScore.batch_id == batch_id).group_by(CriterionScore.status)).all()
counts = {status: count for status, count in rows}
return {"status_counts": counts, "needs_review": counts.get("needs_review", 0),
"unscorable": counts.get("unscorable", 0), "failed": counts.get("failed", 0)}
return status_count_dict({status: count for status, count in rows})
def batch_version_metadata(batch: ScoringBatch) -> dict:
"""Display the immutable rule/course versions, not their database IDs."""
@@ -485,12 +488,20 @@ def batch_version_metadata(batch: ScoringBatch) -> dict:
@router.get("/scoring-batches")
def list_batches(db: Session = Depends(get_db)):
batches = db.scalars(select(ScoringBatch).order_by(ScoringBatch.id.desc())).all()
counts_by_batch = {}
if batches:
rows = db.execute(select(CriterionScore.batch_id, CriterionScore.status, func.count(CriterionScore.id))
.where(CriterionScore.batch_id.in_([batch.id for batch in batches]))
.group_by(CriterionScore.batch_id, CriterionScore.status)).all()
for batch_id, status, count in rows:
counts_by_batch.setdefault(batch_id, {})[status] = count
return [{**{key: getattr(batch, key) for key in ("id", "course_version_id", "rubric_version_id", "status", "progress_done", "progress_total", "error", "created_at", "finished_at")},
"status_label": BATCH_STATUS_LABELS.get(batch.status, batch.status),
"project_ids": batch.project_ids, "legacy_membership": batch.project_ids is None,
**batch_version_metadata(batch),
**batch_status_counts(db, batch.id)}
for batch in db.scalars(select(ScoringBatch).order_by(ScoringBatch.id.desc()))]
**status_count_dict(counts_by_batch.get(batch.id, {}))}
for batch in batches]
@router.get("/scoring-batches/{batch_id}/progress")