forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits

insight-students.ts72 lines, 3.1 KiBHistoryRawDownload
1import { formatApiError, type PersonRef, type Role, type StudentInsight } from "@/api/client";
2import { getInsightByUserId } from "@/api/insights";
3import { getMyStudents } from "@/api/parents";
4import { getUserSearch } from "@/api/users";
5import { personLabel } from "@/lib/person";
6import { 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. */
9export 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. */
16const READ_CONCURRENCY = 6;
17
18/**
19 * Read every student's insight a few at a time, reporting each row as it
20 * lands so the table fills progressively instead of waiting on the slowest.
21 * A failed read becomes that row's `error` state, never a made-up value.
22 */
23export async function loadStudentSignals(
24 students: PersonRef[],
25 onRow: (signal: StudentSignal) => void,
26 read: (userId: string) => Promise<StudentInsight> = getInsightByUserId,
27): Promise<void> {
28 const queue = [...students];
29 const worker = async () => {
30 for (let person = queue.shift(); person; person = queue.shift()) {
31 const identity = { id: person.id, name: personLabel(person) };
32 try {
33 onRow(toStudentSignal(identity, await read(person.id), null));
34 } catch (error) {
35 onRow(toStudentSignal(identity, null, formatApiError(error)));
36 }
37 }
38 };
39 await Promise.all(Array.from({ length: READ_CONCURRENCY }, worker));
40}
41
42export type InsightOverview = {
43 total: number;
44 /** Rows read so far (any state but `not_loaded`). */
45 loaded: number;
46 /** Students with a computed summary. */
47 analysed: number;
48 /** Students with at least one attention item. */
49 needAttention: number;
50 /** Mean of the per-student mark averages that exist; null when none do. */
51 marksAverage: number | null;
52 /** Mean of the observed attendance rates (0..1); null when none observed. */
53 attendanceRate: number | null;
54};
55
56/** The school picture over the rows read so far — only fields a summary actually measured. */
57export function insightOverview(signals: StudentSignal[], total: number): InsightOverview {
58 const analysed = signals.filter((signal) => signal.state === "ok");
59 const marks = analysed.map((signal) => signal.marks.average).filter((value): value is number => value != null);
60 const attendance = analysed
61 .filter((signal) => signal.attendance.observed > 0 && signal.attendance.rate != null)
62 .map((signal) => signal.attendance.rate as number);
63 const mean = (values: number[]) => (values.length ? values.reduce((sum, value) => sum + value, 0) / values.length : null);
64 return {
65 total,
66 loaded: signals.filter((signal) => signal.state !== "not_loaded").length,
67 analysed: analysed.length,
68 needAttention: signals.filter((signal) => signal.attention > 0).length,
69 marksAverage: mean(marks),
70 attendanceRate: mean(attendance),
71 };
72}