import type { QADashboardData } from './qaDashboardData' import { currentQAWeek } from './qaWeeks' export interface HistoricalPlotPoint { label: string value: number | null hint: string frozen?: boolean source?: 'user_provided_history' | 'system_frozen' | 'master_live_cumulative_v1' | 'master_live_calendar_month_v1' month?: string week?: string } // Never manufacture row counts or connect a line across missing weeks/policy changes. export function historicalTrend(data: QADashboardData, correctness: boolean): HistoricalPlotPoint[] { if (data.quality_period) { return (data.calendar_months ?? []).map((point) => { const rate = correctness ? point.correctness : point.coverage return { label: point.month, month: point.month, value: rate.percent, source: point.source, hint: `${point.month}:${rate.percent === null ? '—' : rate.percent.toFixed(2) + '%'};${rate.numerator}/${rate.denominator};月内 ${point.records} 条,排除 ${point.records - rate.denominator} 条;[${point.month_start}, ${point.cutoff_at});每月独立,是÷(是+否),当前有效字段动态重算,非历史冻结`, } }) } const frozen = new Map(data.frozen_weeks.map((p) => [Date.parse(p.week_start), p])) const reported = new Map(data.reported_weeks.map((p) => [Date.parse(p.week_start), p])) const cumulative = new Map((data.cumulative_weeks ?? []).map((p) => [Date.parse(p.week_start), p])) const starts = [...frozen.keys(), ...reported.keys(), ...cumulative.keys()] if (!starts.length) return [] const out: HistoricalPlotPoint[] = [] for (let at = Math.min(...starts); at < Date.parse(data.end) && out.length < 5200; at += 7 * 86400000) { const w = currentQAWeek(new Date(at)), label = `${w.year}W${String(w.week).padStart(2, '0')}`, supplied = reported.get(at), point = frozen.get(at) const live = cumulative.get(at) if (live) { const rate = correctness ? live.correctness : live.coverage out.push({ label, week: live.week, value: rate.percent, source: 'master_live_cumulative_v1', hint: `${live.week}:${rate.percent?.toFixed(2) ?? '—'}%;${rate.numerator}/${rate.denominator};总表累计 ${live.records} 条,截至 ${live.cutoff_at};当前有效字段动态重算,是÷(是+否),非历史冻结`, }) continue } if (supplied) { const value = correctness ? supplied.correctness_percent : supplied.coverage_percent out.push({ label, week: supplied.week, value, source: 'user_provided_history', hint: `${supplied.week}:${value.toFixed(2)}%;用户提供历史统计,未提供分子/分母;不按当前规则重算`, }) continue } const rate = point ? (correctness ? point.correctness : point.coverage) : null out.push({ label, week: `${w.year}WK${String(w.week).padStart(2, '0')}`, value: rate?.percent ?? null, frozen: point?.frozen_at !== null && point?.frozen_at !== undefined, source: point?.frozen_at ? 'system_frozen' : undefined, hint: point?.frozen_at ? `${label}:${rate?.percent?.toFixed(2) ?? '—'}%;${rate?.numerator}/${rate?.denominator};系统冻结于 ${point.frozen_at}` : `${label}:未提供历史统计,缺少当时冻结快照`, }) } return out }