import { basename, dirname } from "node:path"; import { DatabaseSync } from "node:sqlite"; /** 只读已有副本;保留 incomplete 的数值及重复尝试,不冒充有效成绩。 */ export function referenceScores(path: string) { const study = basename(dirname(path)); const db = new DatabaseSync(path, { readOnly: true }); try { type Fact = { seq: number; content: T }; const facts = (kind: string): Fact[] => ( db .prepare( "SELECT created_seq, payload FROM facts WHERE kind = ? ORDER BY created_seq", ) .all(kind) as Array<{ created_seq: number; payload: string }> ).map((r) => ({ seq: r.created_seq, content: JSON.parse(r.payload) as T, })); const links = facts<{ purpose: string; roundId: string; runId: string; candidateId: string; }>("run-link"); const observations = facts<{ runId: string; status: string; finalMetric: { value: number | null }; reasons: string[]; }>("run-observation"); const adoption = facts<{ roundId: string; adoptedRunId: string | null; newBaselineId: string; }>("adoption").find((r) => r.content.roundId === `${study}.r1`)!; return links .filter( (r) => r.content.purpose === "forward" && r.content.roundId === `${study}.r1`, ) .map((link) => { const observation = observations.find( (r) => r.content.runId === link.content.runId, )!; return { source: path, study, candidate: link.content.candidateId, run: link.content.runId, status: observation.content.status, rawScore: observation.content.finalMetric.value, validScore: observation.content.status === "valid" ? observation.content.finalMetric.value : null, adopted: adoption.content.adoptedRunId === link.content.runId, newBaseline: adoption.content.newBaselineId, linkSeq: link.seq, observationSeq: observation.seq, adoptionSeq: adoption.seq, reasons: observation.content.reasons, }; }); } finally { db.close(); } }