From 3b794d89efb9ed84e6f9b0b10f2107a190e8d437 Mon Sep 17 00:00:00 2001 From: Wizzard Date: Sun, 24 Mar 2024 18:03:37 -0400 Subject: [PATCH] Attempt to fix 6 & 1 day timers --- main.js | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) 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); } }