Attempt to fix 6 & 1 day timers

This commit is contained in:
Wizzard 2024-03-24 18:03:37 -04:00
parent 7cc8d7b16a
commit 3b794d89ef
1 changed files with 18 additions and 7 deletions

25
main.js
View File

@ -46,18 +46,29 @@ async function fetchSolanaPrice() {
}
}
function pruneOldData() {
const sixHoursInMilliseconds = 6 * 60 * 60 * 1000;
const oneDayInMilliseconds = 24 * 60 * 60 * 1000;
const cutoffTime6Hours = Date.now() - sixHoursInMilliseconds;
const cutoffTime1Day = Date.now() - oneDayInMilliseconds;
priceHistory.prices = priceHistory.prices.filter(pricePoint => pricePoint.time > cutoffTime1Day);
if (priceHistory.prices.length < 360) {
console.warn("Warning: Not enough data points for accurate 6-hour calculations.");
}
}
async function fetchSolanaPriceAndUpdateHistory() {
const currentPrice = await fetchSolanaPrice();
if (currentPrice) {
pruneOldData();
priceHistory.prices.push({ time: Date.now(), price: currentPrice });
priceHistory.currentPrice = currentPrice;
priceHistory.prices.push({ time: Date.now(), price: currentPrice });
priceHistory.currentPrice = currentPrice;
if (priceHistory.prices.length > 61) {
priceHistory.prices.shift();
}
saveSolanaData(priceHistory);
saveSolanaData(priceHistory);
}
}