/** * Home's readiness score: 100 minus what is open, by category. * * Each category can cost at most `max` points, and the caps add up to 100, so * one category can never pin the score to 0 on its own. Within a category the * cost levels off as the count grows (count / (count + half)), so the first * issues cost the most but fixing any of them still moves the score: 438 open * medium vulnerabilities cost 14 points, 138 cost 11, 38 cost 6. * * It counts open problems only. It is not a conformity assessment. * Used by the API (`compliance_score`) and by Home's breakdown, so both agree. */ export type ReadinessKey = 'overdue' | 'critical' | 'high' | 'medium'; export interface ReadinessInput { overdue_incidents: number; critical_vulnerabilities: number; high_vulnerabilities: number; medium_vulnerabilities: number; } export interface ReadinessLine { key: ReadinessKey; count: number; /** Points this category costs (0 when nothing is open). */ points: number; /** The most this category can ever cost. */ max: number; } export interface Readiness { score: number; lines: ReadinessLine[]; } export const READINESS_RULES: ReadonlyArray<{ key: ReadinessKey; max: number; half: number }> = [ { key: 'overdue', max: 40, half: 1 }, // a report past its Article 14 deadline { key: 'critical', max: 25, half: 2 }, { key: 'high', max: 20, half: 5 }, { key: 'medium', max: 15, half: 60 }, ]; const countFor = (input: ReadinessInput, key: ReadinessKey): number => { const n = key === 'overdue' ? input.overdue_incidents : key === 'critical' ? input.critical_vulnerabilities : key === 'high' ? input.high_vulnerabilities : input.medium_vulnerabilities; return Number.isFinite(n) && n > 0 ? Math.floor(n) : 0; }; export function computeReadiness(input: ReadinessInput): Readiness { const lines = READINESS_RULES.map(({ key, max, half }) => { const count = countFor(input, key); // Ceil so any open issue costs at least a point; count / (count + half) < 1 keeps it within max. const points = count === 0 ? 0 : Math.min(max, Math.ceil((max * count) / (count + half))); return { key, count, points, max }; }); const score = Math.max(0, Math.min(100, 100 - lines.reduce((sum, l) => sum + l.points, 0))); return { score, lines }; }