Files
satshkd-vercel/aggregate-weekly.js
Claude f7e07aa4bd Fix data display issue by aggregating to weekly values
- Reduced historical data from 4,442 daily entries to 636 weekly entries
- This improves chart rendering performance and ensures all data up to current date is displayed
- Updated btcpoll.js to handle weekly data updates (updates same week or adds new week)
- Added aggregate-weekly.js script for future data aggregation needs
- Data now properly displays from 2013-09-15 to 2025-11-09
2025-11-09 16:11:51 +00:00

59 lines
1.9 KiB
JavaScript

const fs = require('fs');
const path = require('path');
// Read the historical data
const historicalPath = path.join(__dirname, 'public', 'historical');
const historicalData = JSON.parse(fs.readFileSync(historicalPath, 'utf8'));
console.log(`Total daily entries: ${historicalData.length}`);
// Group data by week and take the last entry of each week
const weeklyData = [];
let currentWeek = null;
let weekEntries = [];
historicalData.forEach((entry, index) => {
const date = new Date(entry.date);
// Get the start of the week (Monday)
const weekStart = new Date(date);
const day = weekStart.getDay();
const diff = weekStart.getDate() - day + (day === 0 ? -6 : 1); // Adjust to Monday
weekStart.setDate(diff);
weekStart.setHours(0, 0, 0, 0);
const weekKey = weekStart.toISOString().split('T')[0];
if (currentWeek === null) {
currentWeek = weekKey;
}
if (currentWeek !== weekKey || index === historicalData.length - 1) {
// Save the last entry of the previous week
if (weekEntries.length > 0) {
// Use the last entry of the week (most recent data for that week)
const lastEntry = weekEntries[weekEntries.length - 1];
weeklyData.push(lastEntry);
}
// Start a new week
currentWeek = weekKey;
weekEntries = [entry];
} else {
weekEntries.push(entry);
}
});
// Handle the last week
if (weekEntries.length > 0) {
const lastEntry = weekEntries[weekEntries.length - 1];
weeklyData.push(lastEntry);
}
console.log(`Weekly entries: ${weeklyData.length}`);
console.log(`First entry: ${weeklyData[0].date}`);
console.log(`Last entry: ${weeklyData[weeklyData.length - 1].date}`);
// Write the weekly data back to the historical file
fs.writeFileSync(historicalPath, JSON.stringify(weeklyData));
console.log('✅ Successfully aggregated data to weekly values');