Session 45: Snapshot pipeline + GameCard swap + live ticker (2100 tests)

The on-demand "Read" grade model is RETIRED. A scheduled pipeline pre-grades the
slate, locks grades to the line, tracks movement; the dashboard shows them already
there. Orchestrates existing services — nothing rebuilt.

- snapshotService.runSnapshot(sport): getOdds → gradeAndCacheSlate → classify
  archetype per player → lock gradedAt → line deltas vs previous snapshot → write
  snapshot:{sport}:latest/previous + grades:{sport} → ticker events. Fully
  injectable, zero-network unit tests. runAllSnapshots = cron entrypoint.
- Internal trigger POST /api/internal/snapshot/:sport + /all (requireInternalAuth).
  In-process cron (SNAPSHOT_CRON=1, UTC 14,19,22,1,3) in server.js, no new dep.
- Public reads: GET /api/snapshot/:sport (cache-only) + GET /api/ticker (merges
  TICKER_MANUAL pins) + Next proxies.
- GameCard swap: live Slate renders vyndr/GameCard (legacy kept for types only),
  overlays locked grades onto game props → player name once + archetype badge +
  "Graded Xh ago at -115 · Current 2.5 · ▲ TOWARD +1.0". Ungraded → "Awaiting next
  scan", NO Read button. On-demand onGrade flow deleted.
- Ticker polls /api/ticker every 30s, graceful fallback to hardcoded items.
- NBA/WNBA: espnStatsAdapter free fallback (defensive parse → found:false on shape
  mismatch) wired into resolvePlayerStats after the offline Python service.

Env: PROPLINE_API_KEY_1/2/3, VYNDR_INTERNAL_KEY, SNAPSHOT_CRON=1, TICKER_MANUAL.
Backend 2061 -> 2100 tests (+39), 173 suites. Web build clean (exit 0).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Kev
2026-06-18 21:34:29 -04:00
parent 7969a4971a
commit f8b120c0aa
24 changed files with 1425 additions and 129 deletions
+49
View File
@@ -0,0 +1,49 @@
'use strict';
/**
* snapshotScheduler — in-process snapshot cron (Session 45).
*
* No new dependency: a 1-minute unref'd interval that fires `runAllSnapshots`
* at the configured UTC hours (default 14,19,22,1,3 = 10AM/3PM/6PM/9PM/11PM ET,
* matching the sports cycle — morning research, afternoon news, pre-game lock,
* in-game). Gated on SNAPSHOT_CRON=1 so it never runs in dev/test or on a
* container that shouldn't own the schedule. Prefer an EXTERNAL cron (n8n) hitting
* POST /api/internal/snapshot/all when running multiple API replicas — this
* in-process variant assumes a single scheduler instance.
*/
const HOURS_UTC = (process.env.SNAPSHOT_HOURS_UTC || '14,19,22,1,3')
.split(',')
.map((n) => parseInt(n, 10))
.filter((n) => Number.isInteger(n) && n >= 0 && n <= 23);
function startSnapshotScheduler(opts = {}) {
if (process.env.SNAPSHOT_CRON !== '1') return null;
const runAll = opts.runAllSnapshots || require('./services/snapshotService').runAllSnapshots;
const now = opts.now || (() => new Date());
let lastFiredSlot = null;
const tick = async () => {
const d = now();
if (d.getUTCMinutes() !== 0) return;
const h = d.getUTCHours();
if (!HOURS_UTC.includes(h)) return;
const slot = `${d.getUTCFullYear()}-${d.getUTCMonth()}-${d.getUTCDate()}-${h}`;
if (slot === lastFiredSlot) return; // fire once per slot
lastFiredSlot = slot;
try {
const results = await runAll();
const ok = results.filter((r) => r.status === 'ok');
console.log(`[snapshot] cron fired ${h}:00 UTC — ${ok.length}/${results.length} sports graded`);
} catch (e) {
console.warn('[snapshot] cron run failed:', e.message);
}
};
const interval = setInterval(tick, 60_000);
if (interval.unref) interval.unref();
console.log(`[snapshot] scheduler armed for UTC hours: ${HOURS_UTC.join(', ')}`);
return { interval, tick };
}
module.exports = { startSnapshotScheduler, HOURS_UTC };