mirror of
https://github.com/aljazceru/satshkd-vercel.git
synced 2025-12-17 05:04:24 +01:00
Merge pull request #5 from aljazceru/claude/fix-data-display-issue-011CUxbyjcjzZqztEKnwkp72
Fix data display issue by aggregating to weekly values
This commit is contained in:
58
aggregate-weekly.js
Normal file
58
aggregate-weekly.js
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
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');
|
||||||
29
btcpoll.js
29
btcpoll.js
@@ -44,7 +44,17 @@ async function BTCDaily() {
|
|||||||
return row
|
return row
|
||||||
}
|
}
|
||||||
|
|
||||||
// update file in the target github repo
|
// Get the Monday of the week for a given date
|
||||||
|
function getWeekStart(date) {
|
||||||
|
const d = new Date(date);
|
||||||
|
const day = d.getDay();
|
||||||
|
const diff = d.getDate() - day + (day === 0 ? -6 : 1); // Adjust to Monday
|
||||||
|
d.setDate(diff);
|
||||||
|
d.setHours(0, 0, 0, 0);
|
||||||
|
return d.toISOString().split('T')[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
// update file in the target github repo with weekly data
|
||||||
async function updateFile() {
|
async function updateFile() {
|
||||||
const row = await BTCDaily()
|
const row = await BTCDaily()
|
||||||
//const row = []
|
//const row = []
|
||||||
@@ -54,8 +64,21 @@ async function updateFile() {
|
|||||||
//console.log("dirname", __dirname)
|
//console.log("dirname", __dirname)
|
||||||
const original = await fs.readFileSync(fileToRead)
|
const original = await fs.readFileSync(fileToRead)
|
||||||
let orig = JSON.parse(original)
|
let orig = JSON.parse(original)
|
||||||
//console.log(orig[0])
|
|
||||||
orig.push(row)
|
const newWeekStart = getWeekStart(row.date);
|
||||||
|
const lastEntry = orig[orig.length - 1];
|
||||||
|
const lastWeekStart = lastEntry ? getWeekStart(lastEntry.date) : null;
|
||||||
|
|
||||||
|
// If the new data is from the same week as the last entry, update it
|
||||||
|
// Otherwise, add a new entry for the new week
|
||||||
|
if (lastWeekStart === newWeekStart) {
|
||||||
|
console.log("Updating same week entry");
|
||||||
|
orig[orig.length - 1] = row;
|
||||||
|
} else {
|
||||||
|
console.log("Adding new week entry");
|
||||||
|
orig.push(row);
|
||||||
|
}
|
||||||
|
|
||||||
//console.log(orig[orig.length - 1])
|
//console.log(orig[orig.length - 1])
|
||||||
const new_content = JSON.stringify(orig)
|
const new_content = JSON.stringify(orig)
|
||||||
await fs.writeFileSync(fileToWrite, new_content);
|
await fs.writeFileSync(fileToWrite, new_content);
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user