fix: invalidate stale predicted_rating after PDGA cycle rollover (#29)

This commit is contained in:
Samuel Enocsson
2026-06-09 10:58:42 +02:00
parent 5198a1c0f4
commit 27ffa096e4
4 changed files with 60 additions and 26 deletions
+20 -1
View File
@@ -1,7 +1,7 @@
const { db } = require('../db');
const { getPlayerFromDB, getRoundHistoryFromDB, savePredictedRatingToDB, savePlayerToDB, getMonthlyHistory, getAllMonthlyHistoriesFromDB, getAllRatingHistoriesFromDB } = require('../models/player');
const { fetchPlayerDataHTTP, parsePlayerData } = require('../scrapers/player-http');
const { calculatePredictedRating } = require('./rating-calculator');
const { calculatePredictedRating, getPreviousPDGAUpdateDate } = require('./rating-calculator');
const logger = require('../logger');
function formatDisplayDate(dateStr) {
@@ -50,6 +50,25 @@ async function getPlayerDataFromDB(pdgaNumber, { includeMonthlyHistory = true }
cutoffRating = updatedPlayer?.cutoff_rating;
}
// Staleness-check: invalidate cached predicted_rating if the PDGA cycle has
// rolled over since it was calculated. Don't recompute — round_history may be
// equally stale. UI will show "—" until the next manual refresh.
const predictedCalculatedAt = cachedPlayer.predicted_calculated_at
? new Date(cachedPlayer.predicted_calculated_at)
: null;
const previousUpdate = getPreviousPDGAUpdateDate();
const hasPredicted = predictedRating !== null && predictedRating !== 0;
const isStale = hasPredicted && (
predictedCalculatedAt === null || predictedCalculatedAt < previousUpdate
);
if (isStale) {
logger.debug(`PDGA ${pdgaNumber}: predicted rating stale (calculated ${predictedCalculatedAt?.toISOString() ?? 'unknown'}, cycle rolled ${previousUpdate.toISOString()})`);
predictedRating = null;
stdDev = null;
excludedRoundsCount = null;
cutoffRating = null;
}
const rating = cachedPlayer.current_rating;
const rawRatingChange = cachedPlayer.rating_change;
const resolvedPredicted = predictedRating > 0 ? predictedRating : null;