fix: use re-fetched timestamp after recompute + rename helper var (#29)
Reviewer 1 flagged: staleness-check read predicted_calculated_at from the original cachedPlayer snapshot even after recompute, so newly calculated ratings (predicted_calculated_at = NULL in snapshot) were immediately nulled by the staleness branch. Fix: read predicted_calculated_at from updatedPlayer too. Reviewer 2 nit: rename thisMonths → secondTuesday for consistency with the original variable name in getNextPDGAUpdateDate.
This commit is contained in:
@@ -42,19 +42,21 @@ async function getPlayerDataFromDB(pdgaNumber, { includeMonthlyHistory = true }
|
|||||||
let stdDev = cachedPlayer.std_dev;
|
let stdDev = cachedPlayer.std_dev;
|
||||||
let excludedRoundsCount = cachedPlayer.excluded_rounds_count;
|
let excludedRoundsCount = cachedPlayer.excluded_rounds_count;
|
||||||
let cutoffRating = cachedPlayer.cutoff_rating;
|
let cutoffRating = cachedPlayer.cutoff_rating;
|
||||||
|
let predictedCalculatedAtRaw = cachedPlayer.predicted_calculated_at;
|
||||||
if (!predictedRating || predictedRating === 0) {
|
if (!predictedRating || predictedRating === 0) {
|
||||||
predictedRating = await getPredictedRatingFromDB(pdgaNumber);
|
predictedRating = await getPredictedRatingFromDB(pdgaNumber);
|
||||||
const updatedPlayer = await getPlayerFromDB(pdgaNumber);
|
const updatedPlayer = await getPlayerFromDB(pdgaNumber);
|
||||||
stdDev = updatedPlayer?.std_dev;
|
stdDev = updatedPlayer?.std_dev;
|
||||||
excludedRoundsCount = updatedPlayer?.excluded_rounds_count;
|
excludedRoundsCount = updatedPlayer?.excluded_rounds_count;
|
||||||
cutoffRating = updatedPlayer?.cutoff_rating;
|
cutoffRating = updatedPlayer?.cutoff_rating;
|
||||||
|
predictedCalculatedAtRaw = updatedPlayer?.predicted_calculated_at;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Staleness-check: invalidate cached predicted_rating if the PDGA cycle has
|
// Staleness-check: invalidate cached predicted_rating if the PDGA cycle has
|
||||||
// rolled over since it was calculated. Don't recompute — round_history may be
|
// rolled over since it was calculated. Don't recompute — round_history may be
|
||||||
// equally stale. UI will show "—" until the next manual refresh.
|
// equally stale. UI will show "—" until the next manual refresh.
|
||||||
const predictedCalculatedAt = cachedPlayer.predicted_calculated_at
|
const predictedCalculatedAt = predictedCalculatedAtRaw
|
||||||
? new Date(cachedPlayer.predicted_calculated_at)
|
? new Date(predictedCalculatedAtRaw)
|
||||||
: null;
|
: null;
|
||||||
const previousUpdate = getPreviousPDGAUpdateDate();
|
const previousUpdate = getPreviousPDGAUpdateDate();
|
||||||
const hasPredicted = predictedRating !== null && predictedRating !== 0;
|
const hasPredicted = predictedRating !== null && predictedRating !== 0;
|
||||||
|
|||||||
@@ -51,10 +51,10 @@ function getNextPDGAUpdateDate() {
|
|||||||
const currentMonth = today.getMonth();
|
const currentMonth = today.getMonth();
|
||||||
const currentYear = today.getFullYear();
|
const currentYear = today.getFullYear();
|
||||||
|
|
||||||
const thisMonths = secondTuesdayOf(currentYear, currentMonth);
|
const secondTuesday = secondTuesdayOf(currentYear, currentMonth);
|
||||||
|
|
||||||
if (today <= thisMonths) {
|
if (today <= secondTuesday) {
|
||||||
return thisMonths;
|
return secondTuesday;
|
||||||
} else {
|
} else {
|
||||||
const nextMonth = currentMonth === 11 ? 0 : currentMonth + 1;
|
const nextMonth = currentMonth === 11 ? 0 : currentMonth + 1;
|
||||||
const nextYear = currentMonth === 11 ? currentYear + 1 : currentYear;
|
const nextYear = currentMonth === 11 ? currentYear + 1 : currentYear;
|
||||||
@@ -66,8 +66,8 @@ function getPreviousPDGAUpdateDate() {
|
|||||||
const today = new Date();
|
const today = new Date();
|
||||||
const year = today.getFullYear();
|
const year = today.getFullYear();
|
||||||
const month = today.getMonth();
|
const month = today.getMonth();
|
||||||
const thisMonths = secondTuesdayOf(year, month);
|
const secondTuesday = secondTuesdayOf(year, month);
|
||||||
if (today > thisMonths) return thisMonths;
|
if (today > secondTuesday) return secondTuesday;
|
||||||
// Otherwise: last month's second Tuesday
|
// Otherwise: last month's second Tuesday
|
||||||
const prevMonth = month === 0 ? 11 : month - 1;
|
const prevMonth = month === 0 ? 11 : month - 1;
|
||||||
const prevYear = month === 0 ? year - 1 : year;
|
const prevYear = month === 0 ? year - 1 : year;
|
||||||
|
|||||||
Reference in New Issue
Block a user