Session B (night2): streaks/hot-list engine — producer + form heat + THE LENS
RESURRECT verdict: the Session-23 engine existed, was pure, tested, and
mounted — it starved because every producer was external and unarmed
(tank01-prefetch via n8n, offline Python flow). The snapshot pipeline is
now the producer: each run merges slate players' real game logs (already
fetched for archetypes — zero extra calls) into rosterlogs:{sport}.
- computeFormHeat: hot hitters (7d AVG), hot sluggers (SLG), hot shooters
(FG%) — correct sum/sum rate math, season baseline else prior stretch,
min-sample refusals, never extrapolated.
- streakLens: no raw streak renders alone — built-vs opponents, tonight's
matchup + opposing SP w/ ERA, step-up/step-down difficulty, one-line
read. Absent context = say less, never invent.
- /api/streaks/:sport: heat merged into the feed, lens applied from cached
schedule + pitchers (time-bounded, can never hang the route), snapshot
grade letters joined.
- ACCEPTANCE (live statsapi, real 2026 logs): 32 rows found — Turang
12-gm on-base, Reynolds 8-gm on-base, Pratt 7-gm on-base + 3-gm
multi-hit, Meidroth 5-gm on-base, Cortes 5-gm on-base.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -130,6 +130,15 @@ async function resolvePlayerStats(name, sport, opts = {}) {
|
||||
season: mlbSeasonRows(res.season, res.group),
|
||||
last10: mlbLast10Rows(res.last10, res.group),
|
||||
splits: [],
|
||||
// Session 60 (night2/B) — the RAW flattened game log, most-recent
|
||||
// first, for the streaks/hot-list roster blob. Free: the adapter
|
||||
// already fetched it for this resolve; nothing extra is called.
|
||||
rawLog: (res.last10 || [])
|
||||
.map((r) => ({ date: r.date || null, opponent: r.opponent || null, isHome: r.isHome ?? null, ...(r.stat || {}) }))
|
||||
.reverse(),
|
||||
seasonRaw: res.season || null,
|
||||
group: res.group || null,
|
||||
playerId: res.id ?? null,
|
||||
};
|
||||
}
|
||||
if (sp === 'nba' || sp === 'wnba') {
|
||||
|
||||
@@ -169,6 +169,28 @@ async function pushTickerItems(events, deps) {
|
||||
await deps.cacheSet('ticker:items', merged, TICKER_TTL);
|
||||
}
|
||||
|
||||
// Session 60 (night2/B) — accumulate slate players' game logs into the
|
||||
// roster blob the streaks/hot-list engines read. Merge by nameKey (newer
|
||||
// entry wins), cap the blob, 72h TTL (a player off the slate for 3 days
|
||||
// ages out — honest churn, not a leak).
|
||||
const ROSTERLOGS_TTL = 72 * 3600;
|
||||
const ROSTERLOGS_CAP = 300;
|
||||
|
||||
async function mergeRosterLogs(sport, entries, deps) {
|
||||
if (!entries || entries.length === 0) return;
|
||||
try {
|
||||
const key = `rosterlogs:${sport}`;
|
||||
const existing = await deps.cacheGet(key);
|
||||
const byKey = new Map();
|
||||
for (const e of Array.isArray(existing) ? existing : []) byKey.set(nameKey(e.name), e);
|
||||
for (const e of entries) byKey.set(nameKey(e.name), e); // fresh resolve wins
|
||||
const merged = [...byKey.values()].slice(-ROSTERLOGS_CAP);
|
||||
await deps.cacheSet(key, merged, ROSTERLOGS_TTL);
|
||||
} catch (e) {
|
||||
console.warn(`[snapshot] rosterlogs merge failed for ${sport}:`, e.message);
|
||||
}
|
||||
}
|
||||
|
||||
const ACTIVE_SPORTS = ['mlb', 'nba', 'wnba', 'soccer'];
|
||||
|
||||
/**
|
||||
@@ -271,6 +293,14 @@ async function runSnapshot(sport, opts = {}) {
|
||||
// (statsapi/ESPN), never guessed. Feeds the ledger team/opponent columns
|
||||
// and the slate join guard (a prop only attaches to its own game).
|
||||
const teamByPlayer = {};
|
||||
// Session 60 (night2/B) — THE STREAKS PRODUCER. The aggregator (streaks +
|
||||
// hot lists) starved because its data producers were all external and
|
||||
// unarmed (tank01-prefetch via n8n, the offline Python grading flow).
|
||||
// The stats resolve above already fetched each slate player's game log —
|
||||
// accumulate it into the `rosterlogs:{sport}` blob rosterLogs.loadRosterLogs
|
||||
// reads FIRST. Zero extra API calls; the pipeline now feeds its own
|
||||
// free layer.
|
||||
const logEntries = [];
|
||||
await mapLimit(players, STATS_CONCURRENCY, async (player) => {
|
||||
try {
|
||||
const stats = await deps.resolveStats(player, sp);
|
||||
@@ -278,9 +308,20 @@ async function runSnapshot(sport, opts = {}) {
|
||||
const c = deps.classify(sp, stats.classifierInput || {});
|
||||
archByPlayer[player] = c.primary ? c.primary.name : null;
|
||||
if (stats.team) teamByPlayer[player] = stats.team;
|
||||
if (Array.isArray(stats.rawLog) && stats.rawLog.length > 0) {
|
||||
logEntries.push({
|
||||
name: normalizeName(player).display || player,
|
||||
playerId: stats.playerId ?? null,
|
||||
team: stats.team || null,
|
||||
group: stats.group || null,
|
||||
seasonRaw: stats.seasonRaw || null,
|
||||
games: stats.rawLog,
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch { /* graceful — no badge */ }
|
||||
});
|
||||
await mergeRosterLogs(sp, logEntries, deps);
|
||||
|
||||
const enriched = graded.map((g) => ({
|
||||
...g,
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* The VYNDR Lens (Session 60, night2/B) — THE RULE: no raw streak ever
|
||||
* renders alone. Every streak/hot-list row carries:
|
||||
* 1. what it was BUILT against (the streak-window opponents — real, from
|
||||
* the game log),
|
||||
* 2. tonight's matchup (opponent + MLB opposing probable SP w/ ERA, from
|
||||
* the free schedule/pitcher caches),
|
||||
* 3. difficulty vs the streak's diet (MLB: SP ERA vs league ~4.00),
|
||||
* 4. a one-line read composing the above.
|
||||
*
|
||||
* Interpreted data is the FREE layer; the grade on it stays paid. Every
|
||||
* field is real or absent — when we can't rate the matchup we say less,
|
||||
* we never invent. Pure module: callers supply tonight's context.
|
||||
*/
|
||||
|
||||
// League-average SP ERA bands (MLB ~4.00 in the modern run environment).
|
||||
const ERA_STEP_UP = 3.4; // facing a SP at/below this = harder than average
|
||||
const ERA_STEP_DOWN = 4.6; // at/above this = softer than average
|
||||
|
||||
const token = (name) => String(name || '').toLowerCase().replace(/[^a-z0-9 ]/g, '').trim();
|
||||
const mascot = (name) => { const t = token(name).split(' ').filter(Boolean); return t.length ? t[t.length - 1] : ''; };
|
||||
const teamsMatch = (a, b) => {
|
||||
if (!a || !b) return false;
|
||||
return token(a) === token(b) || (mascot(a) !== '' && mascot(a) === mascot(b));
|
||||
};
|
||||
|
||||
/**
|
||||
* Find tonight's game + opponent for a team from the cached schedule
|
||||
* (`schedule:{sport}:{date}` shape: [{ homeTeam:{name,abbreviation}, awayTeam, gameTime }]).
|
||||
*/
|
||||
function tonightFor(team, scheduleGames) {
|
||||
if (!team || !Array.isArray(scheduleGames)) return null;
|
||||
for (const g of scheduleGames) {
|
||||
const home = g.homeTeam || {};
|
||||
const away = g.awayTeam || {};
|
||||
if (teamsMatch(team, home.name) || teamsMatch(team, home.abbreviation)) {
|
||||
return { opponent: away.name || away.abbreviation || null, opponentAbbr: away.abbreviation || null, isHome: true, gameTime: g.gameTime || null };
|
||||
}
|
||||
if (teamsMatch(team, away.name) || teamsMatch(team, away.abbreviation)) {
|
||||
return { opponent: home.name || home.abbreviation || null, opponentAbbr: home.abbreviation || null, isHome: false, gameTime: g.gameTime || null };
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tonight's opposing probable SP for a team, from the probable-pitchers
|
||||
* games ([{ home: {team,pitcher,era}, away: {...} }]). The OPPOSING side.
|
||||
*/
|
||||
function opposingPitcherFor(team, pitcherGames) {
|
||||
if (!team || !Array.isArray(pitcherGames)) return null;
|
||||
for (const g of pitcherGames) {
|
||||
if (g && g.home && teamsMatch(team, g.home.team)) return g.away && g.away.pitcher ? g.away : null;
|
||||
if (g && g.away && teamsMatch(team, g.away.team)) return g.home && g.home.pitcher ? g.home : null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function eraDifficulty(era) {
|
||||
const e = Number(era);
|
||||
if (!Number.isFinite(e) || e <= 0) return null;
|
||||
if (e <= ERA_STEP_UP) return 'step up';
|
||||
if (e >= ERA_STEP_DOWN) return 'step down';
|
||||
return 'neutral';
|
||||
}
|
||||
|
||||
const isPitcherRow = (row) => row && (row.type === 'k_streak' || row.type === 'qs_streak');
|
||||
|
||||
/**
|
||||
* Build the lens for one streak/heat row. ctx = { scheduleGames, pitcherGames }.
|
||||
* Returns { builtVs, matchup, difficulty, read } — every field real or null.
|
||||
*/
|
||||
function buildLens(row, ctx = {}) {
|
||||
const lens = { builtVs: null, matchup: null, difficulty: null, read: null };
|
||||
if (!row) return lens;
|
||||
|
||||
const opps = Array.isArray(row.opponents) ? row.opponents.filter(Boolean) : [];
|
||||
if (opps.length > 0) lens.builtVs = opps.slice(0, 4);
|
||||
|
||||
const tonight = tonightFor(row.team, ctx.scheduleGames);
|
||||
if (tonight && tonight.opponent) {
|
||||
lens.matchup = `${tonight.isHome ? 'vs' : '@'} ${tonight.opponent}`;
|
||||
// MLB batter lens: the opposing probable SP is the matchup that matters.
|
||||
if (String(row.sport).toLowerCase() === 'mlb' && !isPitcherRow(row)) {
|
||||
const sp = opposingPitcherFor(row.team, ctx.pitcherGames);
|
||||
if (sp && sp.pitcher) {
|
||||
lens.matchup += ` · SP ${sp.pitcher}${sp.era != null ? ` (${sp.era} ERA)` : ''}`;
|
||||
lens.difficulty = eraDifficulty(sp.era);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The one-line read: composed only from parts we actually have.
|
||||
const bits = [row.description];
|
||||
if (lens.builtVs) bits.push(`built vs ${lens.builtVs.join(', ')}`);
|
||||
if (lens.matchup) {
|
||||
if (lens.difficulty === 'step up') bits.push(`tonight ${lens.matchup} — a step up`);
|
||||
else if (lens.difficulty === 'step down') bits.push(`tonight ${lens.matchup} — a softer spot`);
|
||||
else bits.push(`tonight ${lens.matchup}`);
|
||||
}
|
||||
lens.read = bits.join('; ');
|
||||
return lens;
|
||||
}
|
||||
|
||||
/** Attach the lens to every row (in place shape: { ...row, lens }). */
|
||||
function applyLens(rows, ctx = {}) {
|
||||
return (rows || []).map((r) => ({ ...r, lens: buildLens(r, ctx) }));
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
buildLens,
|
||||
applyLens,
|
||||
tonightFor,
|
||||
opposingPitcherFor,
|
||||
__internals: { eraDifficulty, teamsMatch, ERA_STEP_UP, ERA_STEP_DOWN },
|
||||
};
|
||||
@@ -191,7 +191,7 @@ function computePlayerStreaks(player, sport, opts = {}) {
|
||||
continue;
|
||||
}
|
||||
const run = consecutiveRun(games, spec.value, spec.threshold);
|
||||
if (run >= MIN_STREAK) found.push(makeStreak(player, sport, spec, run));
|
||||
if (run >= MIN_STREAK) found.push(makeStreak(player, sport, spec, run, undefined, games));
|
||||
}
|
||||
|
||||
// Collapse tiered specs (e.g. 25+ and 20+ points) to one entry per
|
||||
@@ -210,7 +210,12 @@ function computePlayerStreaks(player, sport, opts = {}) {
|
||||
return Array.from(best.values()).map(({ _collapse, ...rest }) => rest);
|
||||
}
|
||||
|
||||
function makeStreak(player, sport, spec, run, rateValue) {
|
||||
function makeStreak(player, sport, spec, run, rateValue, games) {
|
||||
// Session 60 (night2/B) — the opponents the streak was BUILT against
|
||||
// (unique, streak-window only). Lens fuel: "built vs OAK, LAA, SEA".
|
||||
const opponents = Array.isArray(games)
|
||||
? [...new Set(games.slice(0, run).map((g) => g && g.opponent).filter(Boolean))]
|
||||
: [];
|
||||
return {
|
||||
sport,
|
||||
player: player.name || player.player || null,
|
||||
@@ -222,6 +227,7 @@ function makeStreak(player, sport, spec, run, rateValue) {
|
||||
currentStreak: run,
|
||||
rate: rateValue ?? null,
|
||||
description: describe(spec, run),
|
||||
opponents,
|
||||
active: true,
|
||||
_collapse: spec.collapse || spec.key, // internal — stripped before return
|
||||
};
|
||||
@@ -245,9 +251,114 @@ function computeStreaks(players, sport, opts = {}) {
|
||||
return all;
|
||||
}
|
||||
|
||||
// ---- Session 60 (night2/B): FORM HEAT — 7-day rate vs baseline ----------
|
||||
// Hot hitters (AVG), hot sluggers (SLG/ISO), hot shooters (FG%). Rates are
|
||||
// computed the correct way (Σmakes/Σattempts over the window, NOT a mean of
|
||||
// per-game rates). Baseline = the player's season rate when the roster blob
|
||||
// carries one, else the pre-window games — labeled accordingly, never faked.
|
||||
|
||||
const fmt3 = (n) => {
|
||||
const s = n.toFixed(3);
|
||||
return s.startsWith('0.') ? s.slice(1) : s; // .412, not 0.412
|
||||
};
|
||||
|
||||
function windowSplit(games, now, windowDays = 7) {
|
||||
const cutoff = (now || Date.now()) - windowDays * 86_400_000;
|
||||
const dated = games.filter((g) => g && g.date && Number.isFinite(new Date(g.date).getTime()));
|
||||
if (dated.length === 0) return { recent: games.slice(0, 5), rest: games.slice(5) };
|
||||
return {
|
||||
recent: dated.filter((g) => new Date(g.date).getTime() >= cutoff),
|
||||
rest: dated.filter((g) => new Date(g.date).getTime() < cutoff),
|
||||
};
|
||||
}
|
||||
|
||||
const sumOf = (rows, ...keys) => rows.reduce((acc, r) => acc + num(r, ...keys), 0);
|
||||
|
||||
// Rate over a set of games: { made, att, rate|null }.
|
||||
function ratio(rows, madeKeys, attKeys) {
|
||||
const made = sumOf(rows, ...madeKeys);
|
||||
const att = sumOf(rows, ...attKeys);
|
||||
return { made, att, rate: att > 0 ? made / att : null };
|
||||
}
|
||||
|
||||
const HEAT_SPECS = {
|
||||
mlb: [
|
||||
{
|
||||
type: 'hot_hitter', category: 'hits', label: 'AVG',
|
||||
made: ['hits', 'H', 'h'], att: ['atBats', 'ab', 'AB'],
|
||||
seasonKey: 'avg', minAtt: 15, minDelta: 0.05, fmt: fmt3,
|
||||
line: (r, b, src) => `hitting ${fmt3(r)} over the last 7 days (${src} ${fmt3(b)})`,
|
||||
},
|
||||
{
|
||||
type: 'hot_slugger', category: 'total_bases', label: 'SLG',
|
||||
made: ['totalBases', 'TB', 'total_bases'], att: ['atBats', 'ab', 'AB'],
|
||||
seasonKey: 'slg', minAtt: 15, minDelta: 0.09, fmt: fmt3,
|
||||
line: (r, b, src) => `slugging ${fmt3(r)} over the last 7 days (${src} ${fmt3(b)})`,
|
||||
},
|
||||
],
|
||||
wnba: [
|
||||
{
|
||||
type: 'hot_shooter', category: 'points', label: 'FG%',
|
||||
made: ['fgm', 'field_goals_made', 'fieldGoalsMade'], att: ['fga', 'field_goals_attempted', 'fieldGoalsAttempted'],
|
||||
seasonKey: 'fgPct', minAtt: 20, minDelta: 0.05, fmt: (n) => `${Math.round(n * 100)}%`,
|
||||
line: (r, b, src) => `shooting ${Math.round(r * 100)}% over the last 7 days (${src} ${Math.round(b * 100)}%)`,
|
||||
},
|
||||
],
|
||||
};
|
||||
HEAT_SPECS.nba = HEAT_SPECS.wnba;
|
||||
|
||||
/**
|
||||
* Form-heat rows in the streak shape (they merge into the same feed).
|
||||
* players = [{ name, playerId, team, games, seasonRaw? }] — games
|
||||
* most-recent-first with date fields.
|
||||
*/
|
||||
function computeFormHeat(players, sport, opts = {}) {
|
||||
const specs = HEAT_SPECS[String(sport || '').toLowerCase()] || [];
|
||||
if (specs.length === 0 || !Array.isArray(players)) return [];
|
||||
const out = [];
|
||||
for (const p of players) {
|
||||
const games = Array.isArray(p?.games) ? p.games.slice() : [];
|
||||
if (opts.chronological) games.reverse();
|
||||
if (games.length === 0) continue;
|
||||
const { recent, rest } = windowSplit(games, opts.now, opts.windowDays || 7);
|
||||
for (const spec of specs) {
|
||||
const cur = ratio(recent, spec.made, spec.att);
|
||||
if (cur.rate == null || cur.att < spec.minAtt) continue;
|
||||
// Baseline: season rate from the blob when present, else prior games.
|
||||
let baseline = null;
|
||||
let baselineSrc = 'season';
|
||||
const seasonVal = p.seasonRaw && Number(p.seasonRaw[spec.seasonKey]);
|
||||
if (Number.isFinite(seasonVal) && seasonVal > 0) baseline = seasonVal;
|
||||
else {
|
||||
const prior = ratio(rest, spec.made, spec.att);
|
||||
if (prior.rate != null && prior.att >= spec.minAtt) { baseline = prior.rate; baselineSrc = 'prior stretch'; }
|
||||
}
|
||||
if (baseline == null || cur.rate - baseline < spec.minDelta) continue;
|
||||
out.push({
|
||||
sport: String(sport).toLowerCase(),
|
||||
player: p.name || p.player || null,
|
||||
playerId: p.playerId ?? p.id ?? null,
|
||||
team: p.team || null,
|
||||
type: spec.type,
|
||||
category: spec.category,
|
||||
threshold: null,
|
||||
currentStreak: recent.length,
|
||||
rate: Math.round(cur.rate * 1000) / 1000,
|
||||
baseline: Math.round(baseline * 1000) / 1000,
|
||||
description: spec.line(cur.rate, baseline, baselineSrc),
|
||||
opponents: [...new Set(recent.map((g) => g && g.opponent).filter(Boolean))],
|
||||
active: true,
|
||||
});
|
||||
}
|
||||
}
|
||||
out.sort((a, b) => (b.rate - b.baseline) - (a.rate - a.baseline));
|
||||
return opts.limit && opts.limit > 0 ? out.slice(0, opts.limit) : out;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
computeStreaks,
|
||||
computePlayerStreaks,
|
||||
computeFormHeat,
|
||||
specsFor,
|
||||
__internals: { consecutiveRun, rateOverWindow, nba, mlb, nfl, soccer, MIN_STREAK },
|
||||
__internals: { consecutiveRun, rateOverWindow, windowSplit, ratio, nba, mlb, nfl, soccer, MIN_STREAK, HEAT_SPECS },
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user