Skip to content

feat(teacher): course analytics page + confusion-hotspots widget - #579

Merged
guillermoscript merged 4 commits into
masterfrom
feat/teacher-analytics-hotspots-567
Jul 28, 2026
Merged

feat(teacher): course analytics page + confusion-hotspots widget#579
guillermoscript merged 4 commits into
masterfrom
feat/teacher-analytics-hotspots-567

Conversation

@guillermoscript

@guillermoscript guillermoscript commented Jul 27, 2026

Copy link
Copy Markdown
Owner

What

Teachers could not see how an exercise or AI-generated drill performs across students. The analysis already existed — lms_get_confusion_hotspots fuses practice, exercise and exam-question outcomes with Elo item_ratings — but it was reachable only from an MCP client, had no widget, and nothing under app/ read it.

This adds both surfaces and closes the gap that actually answers "is this too hard?": exercises.difficulty_level (the teacher's own easy/medium/hard label) was never compared against the rating the platform measured from real attempts.

Web — /dashboard/teacher/courses/[id]/analytics

  • Server-rendered; 7/30/90-day window via plain links, so the page ships no client JS. Linked from the course header.
  • Difficulty calibration — declared label beside measured Elo rating, with a Harder than labelled / Easier than labelled flag.
  • Confusion hotspots — severity-ranked across four signals: practice drills, exercise evaluations, lesson checkpoints, exam-question miss rates.
  • lesson_checkpoint_attempts was never read by any teacher surface despite shipping a purpose-built ..._teacher_metrics_idx.
  • Sweeps use fetchAllRows, so a course busy enough to hit PostgREST's 1000-row cap cannot silently under-report. A signal that fails to load surfaces as a named warning instead of an empty list that reads as "nobody is struggling".
  • Ownership-gated to the course author; all reads run under the teacher's own RLS policies (no admin client).

MCP

  • resources/confusion-hotspots/widget.tsx, wired to the existing tool — it was the only analytics tool without a widget.
  • hardest_items now carries difficulty_level + mismatch.
  • Hotspot scope "lesson""practice": those rows are drill topics that may not map to a lesson at all.
  • Demo fixtures (default / all-clear / empty) for no-DB previews.

Notes for the reviewer

  • Deliberate divergence: the web module reads a 4th signal (checkpoints) the MCP tool does not. mcp-server is a standalone package outside the packages/* workspace, so the two cannot share code; the shared thresholds (severity formula, MISS_RATIO, LOW_SCORE, DIFFICULTY_BANDS) are kept identical and documented in both files.
  • Typing the Supabase client properly (instead of SupabaseClient<any>) caught two real defects: checkpoint_id is a number, and submission_date is nullable and so cannot be compared bare when picking a student's latest retake.
  • No migration.

QA

  1. npm run dev, sign in as creator@codeacademy.com / password123 on code-academy.lvh.me:<port>.
  2. Open a course → Analytics in the header.
  3. With no attempt data the page shows its "no student activity" state; seed item_ratings / practice_attempts / exercise_evaluations for the course to see calibration + hotspots.
  4. Widget: cd mcp-server && MCP_DEMO_WIDGETS=1 npm run start, then render lms_demo_confusion_hotspots (variants default, all-clear, empty).

Verification performed

  • Next build + typecheck + lint clean; 634 unit tests pass (23 new, every one mutation-checked).
  • mcp-server build (19 widgets) and type check pass. 4 pre-existing useCallTool type errors in other widgets are untouched by this PR.
  • Page rendered live against seeded local data covering all four signals and both label-vs-Elo mismatch directions; seed rows and the 5 throwaway users removed afterwards (verified every source table back to its pre-seed count).
  • Widget rendered headlessly in all three fixture states.

Screenshots

Analytics page — 30d (English)

Four scopes present (Exercise, Checkpoint, Practice, Exam question), 3 items flagged as mislabelled, both "Harder than labelled" and "Easier than labelled".

Analytics page, 30-day window

The look-back window actually filters

The seed spreads attempts over three time bands, so the three windows return different data — 7d sees 10 practice / 20 exercise / 5 checkpoint / 3 exam, 90d sees 17 / 27 / 9 / 5.

7 days 90 days
7-day window 90-day window

Spanish

This render caught a real bug, now fixed in a0452bb: the hotspot detail lines were a prebuilt English sentence, so they stayed English while the rest of the page translated.

Analytics page in Spanish

Widget (MCP)

default all-clear empty
default fixture all-clear fixture empty fixture

Not covered

No Playwright E2E. Non-author access is refused by the same author_id ownership check the rest of the teacher area uses, but is not exercised by a test. Elo ratings in the screenshots were back-filled to the values a live item reaches after months of traffic; attempt counts and pass/fail outcomes come from the seeded attempts themselves.

🤖 Generated with Claude Code

https://claude.ai/code/session_01VqQTE7LDYPx7d15BcWLGfC

guillermoscript and others added 4 commits July 27, 2026 23:18
Teachers had no way to see how an exercise performs across students. The
data existed — `lms_get_confusion_hotspots` already fused practice, exercise
and exam-question outcomes with Elo `item_ratings` — but it was reachable
only from an MCP client, had no widget, and nothing in the web app read it.

Adds both surfaces, and closes the gap that actually answers "is this too
hard?": `exercises.difficulty_level` (the teacher's own easy/medium/hard
label) was never compared against the rating the platform measured from real
attempts. Both surfaces now show the label beside the measured rating and
flag the divergence.

Web
  - /dashboard/teacher/courses/[id]/analytics — server-rendered, 7/30/90-day
    window via plain links (no client JS), linked from the course header.
  - lib/analytics/confusion-hotspots.ts fuses four signals: practice drills,
    exercise evaluations, lesson checkpoints and exam-question miss rates.
    `lesson_checkpoint_attempts` was never read by any teacher surface
    despite shipping a purpose-built `..._teacher_metrics_idx`.
  - Sweeps use `fetchAllRows`, so a course busy enough to hit PostgREST's
    1000-row cap cannot silently under-report. A signal that fails to load
    becomes a named warning in the UI rather than an empty list that looks
    like "nobody is struggling".
  - Ownership-gated to the course author; every read runs under the teacher's
    own RLS policies, no admin client.

MCP
  - resources/confusion-hotspots/widget.tsx, wired to the existing tool.
  - hardest_items now carries difficulty_level + mismatch.
  - Hotspot scope "lesson" renamed to "practice": those rows are drill
    topics, which may not map to a lesson at all.
  - Demo fixtures (default / all-clear / empty) for no-DB previews.

Typing the Supabase client (rather than `SupabaseClient<any>`) caught two
real defects in the hand-written row shapes: `checkpoint_id` is a number,
and `submission_date` is nullable and so cannot be compared bare when
picking a student's latest retake.

Verified: Next build + typecheck + lint clean; mcp-server build (19 widgets)
and type check pass; widget rendered headlessly in all three fixture states;
page rendered live against seeded local data as creator@codeacademy.com and
the seed rows removed afterwards.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VqQTE7LDYPx7d15BcWLGfC
The manual pass could only exercise practice and exercise hotspots with a
single student, because every source table was empty locally. Exam
questions, lesson checkpoints, the fetchAllRows paging loop and the warning
path all shipped unverified. 23 tests close that.

Each test was mutation-checked — the suite was re-run against a deliberately
broken copy of the logic to prove the assertions can fail:
  - first-attempt-wins instead of latest-attempt   → caught
  - miss threshold moved from 70% to 50%           → caught
  - `easy` given a floor (so it could read easier) → caught
  - oldest retake wins instead of newest           → caught

That last mutation initially SURVIVED, which exposed a genuinely misleading
test: a NULL `submission_date` never passes the `.gte(cutoff)` window filter
(NULL comparisons are never true), so such a row cannot reach the retake
logic at all. The test claiming to prove null-handling was passing for the
wrong reason. Split into an honest pair: one that exercises retake selection
with two dated submissions, and one that pins the real upstream behaviour.

fake-supabase gains `range()` (needed by fetchAllRows) and a real `order()`.
`count` now reports the FULL match count rather than the post-limit count,
matching PostgREST — a per-page count would make a truncated sweep look
complete, defeating the assertion fetchAllRows exists to make. All 611
pre-existing tests still pass.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VqQTE7LDYPx7d15BcWLGfC
The analytics page shipped its per-hotspot "why" line as a prebuilt English
sentence built inside lib/analytics/confusion-hotspots.ts, so on /es every
heading, badge and footer translated while the line carrying the actual
finding stayed in English:

  4 of 5 student(s) still failing on their latest attempt · average 52%
  3 of 5 student(s) missed it on their latest submission · 60% miss rate

Caught by rendering the page under /es for the QA screenshots, not by a test —
the strings were correct, just not translatable.

Hotspot now carries numbers only (`totalAttempts` replaces `evidence`) and the
page formats them through next-intl. Each signal counts something different, so
each scope gets its own message rather than one generic sentence that would
read wrong for three scopes out of four.

The MCP tool keeps its English `evidence` string on purpose: its reader is a
model, not a localised UI. Noted in the twin-implementation header.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VqQTE7LDYPx7d15BcWLGfC
Page rendered against seeded data in the local Code Academy tenant with all
four signals populated (practice, exercise, checkpoint, exam question) and both
label-vs-Elo mismatch directions present. 7d/30d/90d differ because the seed
spreads attempts across three time bands, so the window switcher is shown doing
real work rather than re-rendering the same list.

Widget shots cover the three demo fixtures: default, all-clear, empty.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VqQTE7LDYPx7d15BcWLGfC
@guillermoscript
guillermoscript marked this pull request as ready for review July 28, 2026 01:15
@guillermoscript
guillermoscript merged commit 5de9ad2 into master Jul 28, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant