hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import { formatApiError, type PersonRef, type Role, type StudentInsight } from "@/api/client"; |
| 2 | import { getInsightByUserId } from "@/api/insights"; |
| 3 | import { getMyStudents } from "@/api/parents"; |
| 4 | import { getUserSearch } from "@/api/users"; |
| 5 | import { personLabel } from "@/lib/person"; |
| 6 | import { toStudentSignal, type StudentSignal } from "@/lib/insight-run-report"; |
| 7 | |
| 8 | /** Students the viewer may analyse: a parent's linked children, or the school's students for staff. */ |
| 9 | export async function loadInsightStudents(role: Role): Promise<PersonRef[]> { |
| 10 | return role === "parent" |
| 11 | ? (await getMyStudents({ limit: 200 })).items |
| 12 | : (await getUserSearch("", undefined, "student", { limit: 200 })).items; |
| 13 | } |
| 14 | |
| 15 | /** Parallel insight reads; there is no bulk endpoint. */ |
| 16 | const READ_CONCURRENCY = 6; |
| 17 | const insightCache = new Map<string, StudentInsight>(); |
| 18 | const cacheKey = (scope: string, id: string) => `${scope}\0${id}`; |
| 19 | |
| 20 | export function cachedStudentSignal(person: PersonRef, scope: string): StudentSignal | null { |
| 21 | const insight = insightCache.get(cacheKey(scope, person.id)); |
| 22 | return insight ? toStudentSignal({ id: person.id, name: personLabel(person) }, insight, null) : null; |
| 23 | } |
| 24 | |
| 25 | export function clearStudentSignalCache(scope: string): void { |
| 26 | for (const key of insightCache.keys()) if (key.startsWith(`${scope}\0`)) insightCache.delete(key); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Read the requested students' insights a few at a time, reporting each row as it |
| 31 | * lands so the table fills progressively instead of waiting on the slowest. |
| 32 | * A failed read becomes that row's `error` state, never a made-up value. |
| 33 | */ |
| 34 | export async function loadStudentSignals( |
| 35 | students: PersonRef[], |
| 36 | onRow: (signal: StudentSignal) => void, |
| 37 | read: (userId: string) => Promise<StudentInsight> = getInsightByUserId, |
| 38 | scope?: string, |
| 39 | ): Promise<void> { |
| 40 | const queue = [...students]; |
| 41 | const worker = async () => { |
| 42 | for (let person = queue.shift(); person; person = queue.shift()) { |
| 43 | const identity = { id: person.id, name: personLabel(person) }; |
| 44 | try { |
| 45 | const cached = scope ? insightCache.get(cacheKey(scope, person.id)) : undefined; |
| 46 | const insight = cached ?? await read(person.id); |
| 47 | if (scope && !cached) insightCache.set(cacheKey(scope, person.id), insight); |
| 48 | onRow(toStudentSignal(identity, insight, null)); |
| 49 | } catch (error) { |
| 50 | onRow(toStudentSignal(identity, null, formatApiError(error))); |
| 51 | } |
| 52 | } |
| 53 | }; |
| 54 | await Promise.all(Array.from({ length: READ_CONCURRENCY }, worker)); |
| 55 | } |
| 56 | |
| 57 | export type InsightOverview = { |
| 58 | total: number; |
| 59 | /** Rows read so far (any state but `not_loaded`). */ |
| 60 | loaded: number; |
| 61 | /** Students with a computed summary. */ |
| 62 | analysed: number; |
| 63 | /** Students with at least one attention item. */ |
| 64 | needAttention: number; |
| 65 | /** Mean of the per-student mark averages that exist; null when none do. */ |
| 66 | marksAverage: number | null; |
| 67 | /** Mean of the observed attendance rates (0..1); null when none observed. */ |
| 68 | attendanceRate: number | null; |
| 69 | }; |
| 70 | |
| 71 | /** The school picture over the rows read so far — only fields a summary actually measured. */ |
| 72 | export function insightOverview(signals: StudentSignal[], total: number): InsightOverview { |
| 73 | const analysed = signals.filter((signal) => signal.state === "ok"); |
| 74 | const marks = analysed.map((signal) => signal.marks.average).filter((value): value is number => value != null); |
| 75 | const attendance = analysed |
| 76 | .filter((signal) => signal.attendance.observed > 0 && signal.attendance.rate != null) |
| 77 | .map((signal) => signal.attendance.rate as number); |
| 78 | const mean = (values: number[]) => (values.length ? values.reduce((sum, value) => sum + value, 0) / values.length : null); |
| 79 | return { |
| 80 | total, |
| 81 | loaded: signals.filter((signal) => signal.state !== "not_loaded").length, |
| 82 | analysed: analysed.length, |
| 83 | needAttention: signals.filter((signal) => signal.attention > 0).length, |
| 84 | marksAverage: mean(marks), |
| 85 | attendanceRate: mean(attendance), |
| 86 | }; |
| 87 | } |