diff --git a/main.js b/main.js index c9cb562..a5d41da 100644 --- a/main.js +++ b/main.js @@ -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); } }