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

Merged
shcizo merged 2 commits from fix/invalidate-stale-predicted-rating-29 into main 2026-06-09 14:02:44 +02:00
2 changed files with 9 additions and 7 deletions
Showing only changes of commit c7fb4a7068 - Show all commits
+4 -2
View File
@@ -42,19 +42,21 @@ async function getPlayerDataFromDB(pdgaNumber, { includeMonthlyHistory = true }
let stdDev = cachedPlayer.std_dev;
let excludedRoundsCount = cachedPlayer.excluded_rounds_count;
let cutoffRating = cachedPlayer.cutoff_rating;
let predictedCalculatedAtRaw = cachedPlayer.predicted_calculated_at;
if (!predictedRating || predictedRating === 0) {
predictedRating = await getPredictedRatingFromDB(pdgaNumber);
const updatedPlayer = await getPlayerFromDB(pdgaNumber);
stdDev = updatedPlayer?.std_dev;
excludedRoundsCount = updatedPlayer?.excluded_rounds_count;
cutoffRating = updatedPlayer?.cutoff_rating;
predictedCalculatedAtRaw = updatedPlayer?.predicted_calculated_at;
}
// 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)
const predictedCalculatedAt = predictedCalculatedAtRaw
? new Date(predictedCalculatedAtRaw)
: null;
const previousUpdate = getPreviousPDGAUpdateDate();
const hasPredicted = predictedRating !== null && predictedRating !== 0;
+5 -5
View File
@@ -51,10 +51,10 @@ function getNextPDGAUpdateDate() {
const currentMonth = today.getMonth();
const currentYear = today.getFullYear();
const thisMonths = secondTuesdayOf(currentYear, currentMonth);
const secondTuesday = secondTuesdayOf(currentYear, currentMonth);
if (today <= thisMonths) {
return thisMonths;
if (today <= secondTuesday) {
return secondTuesday;
} else {
const nextMonth = currentMonth === 11 ? 0 : currentMonth + 1;
const nextYear = currentMonth === 11 ? currentYear + 1 : currentYear;
@@ -66,8 +66,8 @@ function getPreviousPDGAUpdateDate() {
const today = new Date();
const year = today.getFullYear();
const month = today.getMonth();
const thisMonths = secondTuesdayOf(year, month);
if (today > thisMonths) return thisMonths;
const secondTuesday = secondTuesdayOf(year, month);
if (today > secondTuesday) return secondTuesday;
// Otherwise: last month's second Tuesday
const prevMonth = month === 0 ? 11 : month - 1;
const prevYear = month === 0 ? year - 1 : year;