fix: preload player rating history to fix first-click chart render (#10)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
const { db } = require('../db');
|
||||
const { getPlayerFromDB, getRoundHistoryFromDB, savePredictedRatingToDB, savePlayerToDB, getMonthlyHistory, getAllMonthlyHistoriesFromDB } = require('../models/player');
|
||||
const { getPlayerFromDB, getRoundHistoryFromDB, savePredictedRatingToDB, savePlayerToDB, getMonthlyHistory, getAllMonthlyHistoriesFromDB, getAllRatingHistoriesFromDB } = require('../models/player');
|
||||
const { fetchPlayerDataHTTP, parsePlayerData } = require('../scrapers/player-http');
|
||||
const { calculatePredictedRating } = require('./rating-calculator');
|
||||
const logger = require('../logger');
|
||||
@@ -167,6 +167,7 @@ async function getAllRatingsFromDB(progressCallback = null) {
|
||||
|
||||
// Fetch all monthly histories in one query so the per-player loop doesn't add N extra queries
|
||||
const monthlyHistoryMap = await getAllMonthlyHistoriesFromDB(12);
|
||||
const ratingHistoryMap = await getAllRatingHistoriesFromDB();
|
||||
|
||||
const ratings = [];
|
||||
const total = allPlayers.length;
|
||||
@@ -189,6 +190,14 @@ async function getAllRatingsFromDB(progressCallback = null) {
|
||||
|
||||
if (playerData) {
|
||||
playerData.monthlyHistory = monthlyHistoryMap.get(pdgaNumber) ?? [];
|
||||
const rawHistory = ratingHistoryMap.get(pdgaNumber) ?? [];
|
||||
playerData.ratingHistory = rawHistory.map(row => ({
|
||||
date: row.date,
|
||||
rating: row.rating,
|
||||
displayDate: new Date(row.date).toLocaleDateString('en-US', {
|
||||
day: '2-digit', month: 'short', year: 'numeric'
|
||||
})
|
||||
}));
|
||||
// Re-derive now that history is attached — bulk path skipped includeMonthlyHistory
|
||||
const derived = deriveMonthlyDeltas(playerData.rating, player.rating_change, playerData.monthlyHistory);
|
||||
playerData.lastMonthRating = derived.lastMonthRating;
|
||||
@@ -218,7 +227,8 @@ async function getAllRatingsFromDB(progressCallback = null) {
|
||||
stdDev: null,
|
||||
lastMonthRating: (errorRating != null && errorRatingChange != null) ? errorRating - errorRatingChange : null,
|
||||
deltaPredicted: null,
|
||||
monthlyHistory: []
|
||||
monthlyHistory: [],
|
||||
ratingHistory: []
|
||||
};
|
||||
ratings.push(errorData);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user