Fix 1 day timer & handle failure

This commit is contained in:
Wizzard 2024-03-26 11:17:04 -04:00
parent 3b794d89ef
commit cddc77e520
1 changed files with 8 additions and 3 deletions

11
main.js
View File

@ -38,26 +38,29 @@ async function fetchSolanaPrice() {
try {
const fetch = (await import('node-fetch')).default;
const response = await fetch(cryptocompareApiUrl);
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
const data = await response.json();
return parseFloat(data.USD).toFixed(2);
} catch (error) {
console.error('Error fetching Solana price:', error);
setTimeout(fetchSolanaPriceAndUpdateHistory, 5 * 60 * 1000);
return null;
}
}
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) {
if (priceHistory.prices.length < (sixHoursInMilliseconds / (60 * 1000))) {
console.warn("Warning: Not enough data points for accurate 6-hour calculations.");
}
if (priceHistory.prices.length < (oneDayInMilliseconds / (60 * 1000))) {
console.warn("Warning: Not enough data points for accurate 1-day calculations.");
}
}
async function fetchSolanaPriceAndUpdateHistory() {
@ -121,6 +124,8 @@ function immediatePriceCheckAndAnnounce() {
const oneDayAgoPrice = parseFloat(findPriceAgo(1440).price);
oneDayChange.percent = ((latestPrice - oneDayAgoPrice) / oneDayAgoPrice) * 100;
oneDayChange.dollar = latestPrice - oneDayAgoPrice;
} else {
console.log("Insufficient data for 1-day change calculation.");
}
if (priceHistory.prices.length >= 360) {