Something went wrong. Try again.
Synapse is a study workspace. Organize courses read and annotate the material, train with generated exercises and watch your progress forked from denishovart.com/synapse
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126// Progress, computed locally: the attempt log joined with the workspace's// exercises/courses/topics. Mirrors the shapes the Progress page was written// against.
import type { ResultAsync } from 'neverthrow';import type { ProgressCourseDetail, ProgressGlobal } from '../../types';import { allEntities, attemptsDoc, commit, getEntity, run, ws } from '../workspace';import type { DataError } from '../workspace';import { changeTag } from '../changeTags';import type { AttemptRecord } from './attempts';
const HISTORY_DAYS = 30;
interface Joined { attempt: AttemptRecord; course_id: string; topic_id: string | null;}
async function joinedAttempts(): Promise<Joined[]> { const doc = await ws(); const exercises = allEntities(doc, 'exercises'); const attempts = await attemptsDoc(); const map = attempts.getMap('attempts'); const out: Joined[] = []; for (const key of map.keys()) { const value = map.get(key) as AttemptRecord | undefined; if (!value || typeof value !== 'object') continue; const exercise = exercises.get(value.exercise_id); if (!exercise) continue; out.push({ attempt: value, course_id: String(exercise.course_id), topic_id: (exercise.topic_id as string | null) ?? null, }); } return out;}
function rate(rows: AttemptRecord[]): number | null { if (!rows.length) return null; return Math.round((rows.filter((a) => a.is_correct).length / rows.length) * 100);}
export function global(): ResultAsync<ProgressGlobal, DataError> { return run(async () => { const doc = await ws(); const courses = allEntities(doc, 'courses'); const joined = await joinedAttempts();
const perCourse = [...courses].map(([courseId, c]) => { const rows = joined.filter((j) => j.course_id === courseId).map((j) => j.attempt); return { course_id: courseId, code: String(c.code ?? ''), name: String(c.name ?? ''), success_rate: rate(rows), total_attempts: rows.length, exam_blank_attempts: rows.filter((a) => a.mode === 'mock_exam').length, }; });
const since = new Date(Date.now() - HISTORY_DAYS * 24 * 60 * 60 * 1000) .toISOString() .slice(0, 10); const byDay = new Map<string, { total: number; correct: number }>(); for (const { attempt } of joined) { const day = String(attempt.answered_at ?? '').slice(0, 10); if (!day || day < since) continue; const agg = byDay.get(day) ?? { total: 0, correct: 0 }; agg.total += 1; if (attempt.is_correct) agg.correct += 1; byDay.set(day, agg); } const history = [...byDay.entries()] .map(([day, agg]) => ({ day, ...agg })) .sort((a, b) => a.day.localeCompare(b.day));
return { courses: perCourse, history }; });}
export function course(courseId: string): ResultAsync<ProgressCourseDetail, DataError> { return run(async () => { const doc = await ws(); const topics = allEntities(doc, 'topics'); const joined = (await joinedAttempts()).filter((j) => j.course_id === courseId);
const perTopic = [...topics] .filter(([, t]) => t.course_id === courseId) .map(([topicId, t]) => { const rows = joined.filter((j) => j.topic_id === topicId).map((j) => j.attempt); return { topic_id: topicId, name: String(t.name ?? ''), success_rate: rate(rows), total_attempts: rows.length, }; }); return { topics: perTopic }; });}
// Reset = drop the course's attempts from the log (the map tolerates deletes;// append-only is a usage convention, not a constraint).export function resetCourse(courseId: string): ResultAsync<null, DataError> { return run(async () => { const doc = await ws(); const courseName = getEntity(doc, 'courses', courseId)?.name as string | undefined; const exercises = allEntities(doc, 'exercises'); const ids = new Set( [...exercises].filter(([, e]) => e.course_id === courseId).map(([id]) => id), ); const attempts = await attemptsDoc(); const map = attempts.getMap('attempts'); const toDelete: string[] = []; for (const key of map.keys()) { const value = map.get(key) as AttemptRecord | undefined; if (value && ids.has(value.exercise_id)) toDelete.push(key); } for (const key of toDelete) map.delete(key); await commit(attempts, 'attempts', changeTag.exercise.reset(courseName)); return null; });}