diff --git a/server/src/db/db.ts b/server/src/db/db.ts index 731f25b..382c6e1 100644 --- a/server/src/db/db.ts +++ b/server/src/db/db.ts @@ -139,6 +139,13 @@ CREATE TABLE IF NOT EXISTS inplay_history ( PRIMARY KEY (fixture_num, minute, home_score, away_score) ); +CREATE TABLE IF NOT EXISTS model_history ( + t TEXT NOT NULL, + label TEXT NOT NULL, + top TEXT NOT NULL, + key TEXT NOT NULL +); + CREATE TABLE IF NOT EXISTS ingest_log ( id INTEGER PRIMARY KEY AUTOINCREMENT, source TEXT NOT NULL, @@ -473,3 +480,27 @@ export function getMatchExt(fixtureNum: number): { data: T; updated if (!row) return null; return { data: JSON.parse(row.json) as T, updatedAt: row.updated_at }; } + +// ---- title-race diary (championship odds after each result) ---- +// Persisted so the Predict chart survives restarts; `key` is the finished- +// result signature the point was computed from (dedupes boot recomputes). + +export interface ModelHistoryRecord { + t: string; + label: string; + top: { team: string; champion: number }[]; + key: string; +} + +export function saveModelHistory(p: { t: string; label: string; top: { team: string; champion: number }[] }, key: string): void { + db() + .prepare('INSERT INTO model_history (t, label, top, key) VALUES (?, ?, ?, ?)') + .run(p.t, p.label, JSON.stringify(p.top), key); +} + +export function allModelHistory(): ModelHistoryRecord[] { + const rows = db() + .prepare('SELECT t, label, top, key FROM model_history ORDER BY t DESC LIMIT 200') + .all() as unknown as { t: string; label: string; top: string; key: string }[]; + return rows.reverse().map((r) => ({ t: r.t, label: r.label, top: JSON.parse(r.top) as ModelHistoryRecord['top'], key: r.key })); +} diff --git a/server/src/index.ts b/server/src/index.ts index 1693866..d8200fb 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -10,7 +10,7 @@ import { startScheduler } from './ingest/scheduler'; import { ModelEngine } from './model'; import { buildPreview, buildTeamProfile } from './preview'; import { buildScoreboard, snapshotCheck } from './scoreboard'; -import { db, healthAll, oddsFor, latestOddsAll, dbCounts, inplayHistory, recordInplay, getMatchExt, getMatchProvider, allFixtureResults, saveFixtureResult } from './db/db'; +import { db, healthAll, oddsFor, latestOddsAll, dbCounts, inplayHistory, recordInplay, getMatchExt, getMatchProvider, allFixtureResults, saveFixtureResult, allModelHistory, saveModelHistory } from './db/db'; import { inPlayProbs } from '../../src/lib/model/inplay'; import { PushNotifier, pushEnabled, subscribe as pushSubscribe, unsubscribe as pushUnsubscribe, vapidPublicKey } from './push'; import type { EspnSummary } from './ingest/espnNormalize'; @@ -45,8 +45,9 @@ export function buildServer() { if (state.applyScore(r.fixture_num, r.home_score, r.away_score, r.status as MatchStatus, null, 'seed')) restored++; } if (restored) console.log(`[state] restored ${restored} fixture result(s) from the DB`); - const model = new ModelEngine(STATIC_DIR); + const model = new ModelEngine(STATIC_DIR, { load: allModelHistory, save: saveModelHistory }); model.refitTeamDc(state.allFixtures()); // current Team-DC from day one (covers restarts mid-tournament) + model.rebuildHistory(state.allFixtures()); // one-time diary replay when nothing is persisted yet model.recompute(state.allFixtures()); const clients = new Set(); diff --git a/server/src/model.ts b/server/src/model.ts index 94b8647..7dbc256 100644 Binary files a/server/src/model.ts and b/server/src/model.ts differ diff --git a/src/components/OddsChart.tsx b/src/components/OddsChart.tsx index 8e014ef..c38501b 100644 --- a/src/components/OddsChart.tsx +++ b/src/components/OddsChart.tsx @@ -1,6 +1,7 @@ import { useMemo } from 'react'; import { LineChart, Line, XAxis, YAxis, Tooltip, ResponsiveContainer, CartesianGrid, Legend } from 'recharts'; import { teamFlag } from '@/lib/teams'; +import { useFormat } from '@/lib/i18n'; import type { OddsHistoryPoint } from '@/lib/types'; const PALETTE = [ @@ -15,23 +16,32 @@ const PALETTE = [ /** Championship odds over time — one line per top contender, a point per result * that moved the model. */ export function OddsChart({ history }: { history: OddsHistoryPoint[] }) { + const { locale } = useFormat(); const { data, teams } = useMemo(() => { const last = history[history.length - 1]; const teams = (last?.top ?? []).slice(0, 6).map((t) => t.team); + const tag = locale === 'de' ? 'de-DE' : 'en-GB'; const data = history.map((h, i) => { - const row: Record = { i: i + 1, label: h.label }; + const day = new Date(h.t).toLocaleDateString(tag, { day: 'numeric', month: 'short' }); + const row: Record = { i: i + 1, label: h.label, day }; for (const t of h.top) if (teams.includes(t.team)) row[t.team] = Math.round(t.champion * 1000) / 10; return row; }); return { data, teams }; - }, [history]); + }, [history, locale]); return (
- + String(data[Number(i) - 1]?.day ?? '')} + minTickGap={28} + />