Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
## 2026-07-16 - [O(N*M) Nested Loop Lookups in Grading Engines]
**Learning:** In interactive scenarios (such as Bid Elevator and STR Triage), grading engines frequently iterate over user decisions and match them against scenario properties (like keywords or search terms). Performing `array.find()` inside loop bodies or filter predicates results in costly O(N*M) lookups.
**Action:** Convert arrays to `Map` lookups before entering loops/nested scans. Mapping keys once in O(M) time enables O(1) lookups during execution, transforming the time complexity of the grading logic to O(N + M).

## 2026-07-30 - [Scoping User Lesson Progress Queries]
**Learning:** Querying user-specific lesson progress history without a courses or lessons filter fetches the user's entire completion history. As the student completes more lessons, this payload grows unboundedly, increasing database round-trip latency, payload size, and server-side memory consumption.
**Action:** Always extract the relevant lesson IDs first from the courses currently being queried or rendered, and explicitly scope the progress lookup with `lessonId: { in: allLessonIds }`.
16 changes: 11 additions & 5 deletions src/app/(dashboard)/courses/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,17 @@ export default async function CoursesIndexPage() {
},
});

// Get user's lesson progress
const lessonProgress = await db.lessonProgress.findMany({
where: { userId: user.id, deletedAt: null },
select: { lessonId: true, status: true },
});
const allLessons = courses.flatMap((c) => c.modules.flatMap((m) => m.lessons));
const allLessonIds = allLessons.map((l) => l.id);

// Bolt optimization: Scope lessonProgress query to only the relevant lesson IDs
// to avoid fetching the user's entire history and reduce database/memory footprint.
const lessonProgress = allLessonIds.length > 0
? await db.lessonProgress.findMany({
where: { userId: user.id, lessonId: { in: allLessonIds }, deletedAt: null },
select: { lessonId: true, status: true },
})
: [];
const progressMap = new Map(lessonProgress.map((p) => [p.lessonId, p.status]));

return (
Expand Down
17 changes: 11 additions & 6 deletions src/app/(dashboard)/dashboard/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,20 @@ export default async function DashboardPage() {
},
});

// Get user's lesson progress
const lessonProgress = await db.lessonProgress.findMany({
where: { userId: user.id, deletedAt: null },
select: { lessonId: true, status: true },
});
const allLessons = courses.flatMap((c) => c.modules.flatMap((m) => m.lessons));
const allLessonIds = allLessons.map((l) => l.id);

// Bolt optimization: Scope lessonProgress query to only the relevant lesson IDs
// to avoid fetching the user's entire history and reduce database/memory footprint.
const lessonProgress = allLessonIds.length > 0
? await db.lessonProgress.findMany({
where: { userId: user.id, lessonId: { in: allLessonIds }, deletedAt: null },
select: { lessonId: true, status: true },
})
: [];
const progressMap = new Map(lessonProgress.map((p) => [p.lessonId, p.status]));

// Compute aggregate stats
const allLessons = courses.flatMap((c) => c.modules.flatMap((m) => m.lessons));
const totalLessons = allLessons.length;
const completedLessons = allLessons.filter((l) => progressMap.get(l.id) === ProgressStatus.COMPLETED).length;
const inProgressLessons = allLessons.filter((l) => progressMap.get(l.id) === ProgressStatus.IN_PROGRESS).length;
Expand Down