Fix 500 on student_quiz_report_v2 when a subject is fully skipped#80
Merged
Conversation
The v2 report templates pipe `percentage` and `accuracy` through `"%.Nf"|format(...)`, which raises TypeError on None and surfaces as a Lambda 500. Upstream stores these as DynamoDB NULL when no questions were attempted in a subject (e.g. a student skipped all 25 Maths questions: 0 correct / 0 wrong is mathematically undefined, so the producer wrote NULL rather than 0). Two-layer fix: - render_v2_report() now coerces None -> 0 for `percentage` and `accuracy` on both `overall_performance` and each entry in `subject_performance`, so any consumer of the v2 payload sees clean numerics. - v2 and v2_print templates additionally guard each `%.Nf|format(...)` call with `or 0` as a defense-in-depth fallback. Also imports `json` at the top of routers/student_quiz_reports.py — it was used at line 656 (chapter_to_link_map = json.load(...)) but never imported, a latent NameError on the v3 codepath that flake8 flagged when committing this change. One-line drive-by fix. Follow-up worth filing separately: the v2 producer / ETL should emit 0.0 (or an explicit not-attempted flag) instead of NULL for these fields, and any existing rows with NULL accuracy should be backfilled. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Production hit a 500 on
https://reports.avantifellows.org/reports/student_quiz_report/PunjabStudents_69d5060a9be5a82f7e85209e/204716. CloudWatch trace:The DynamoDB item for that student has
subject_performance[1].accuracy = NULLfor Maths — the student skipped all 25 questions, soaccuracy = correct / (correct + wrong) = 0/0is mathematically undefined and the upstream pipeline persists it as DynamoDB NULL rather than 0. Jinja's"%.Nf"|format(...)then explodes.Fix (two layers)
render_v2_report()inapp/routers/student_quiz_reports.py: coerceNone → 0forpercentageandaccuracyon bothoverall_performanceand each entry ofsubject_performancebefore passing the payload to the template. This is the primary fix — any consumer of the v2 payload now sees clean numerics.student_quiz_report_v2.htmlandstudent_quiz_report_v2_print.html: every"%.Nf"|format(report.overall_performance.{percentage,accuracy})and"%.Nf"|format(subject.{percentage,accuracy})call now reads... or 0as a defense-in-depth fallback. The template can't crash on these fields even if a future code path skips the renderer sanitization.Scope note: only the v2 + v2_print templates have
"%.Nf"|format(...)on these fields, so those are the only files that can crash this way. The v1 (student_quiz_report.html) and v3 (student_quiz_report_v3.html) templates render the same values withoutformat(...)and so cannot 500 on None.Drive-by
app/routers/student_quiz_reports.pyline 656 usesjson.load(...)butjsonwas never imported (introduced March 2025) — flake8 caught it when committing this change. Addedimport jsonat the top. LatentNameErroron the v3 codepath if that line is ever hit.Follow-up (separate ticket worth filing)
The v2 producer / ETL should emit
0.0(or an explicitnot_attempted: trueflag) instead of DynamoDB NULL foraccuracywhen no questions were attempted. Existing rows withaccuracy: NULLwould also benefit from a one-shot backfill. I scoped this PR to a hotfix so the page stops 500-ing today; the data-quality cleanup is its own piece of work.Test plan
subject_performance[*].accuracy = Noneand confirm it renders0.0%instead of 500-ing./reports/student_quiz_report/PunjabStudents_69d5060a9be5a82f7e85209e/204716and confirm the page renders. Maths row should show0.0%accuracy.?format=pdfand confirm the print template also renders.🤖 Generated with Claude Code