mirror of
https://github.com/aljazceru/satshkd-vercel.git
synced 2025-12-17 05:04:24 +01:00
Fix price table display by using closest-match dates and API fallback
The price table was not displaying because of two issues: 1. The Bitfinex API was returning 403 errors, causing the entire get10yr() function to fail 2. The code was looking for exact date matches in weekly historical data Changes: - Add try-catch around API call with fallback to latest historical data - Replace exact date matching with closest-date algorithm to handle weekly data - Table now shows 10 years of historical price data with percent changes
This commit is contained in:
38
calculate.js
38
calculate.js
@@ -30,18 +30,40 @@ module.exports = {
|
||||
|
||||
// get all the years you need from 1 - 10
|
||||
for (let i = 1; i < 11; i++) {
|
||||
const y = new Date(new Date().setFullYear(new Date().getFullYear() - i)).toISOString().slice(0, 10)
|
||||
datelist.push(y)
|
||||
}
|
||||
for (let j = 0; j < historical.length; j++) {
|
||||
const hdate = historical[j]['date']
|
||||
if (datelist.includes(hdate)) {
|
||||
hist_entries.push(historical[j])
|
||||
const targetDate = new Date(new Date().setFullYear(new Date().getFullYear() - i))
|
||||
const targetDateStr = targetDate.toISOString().slice(0, 10)
|
||||
|
||||
// Find the closest date in historical data
|
||||
let closestEntry = null
|
||||
let minDiff = Infinity
|
||||
|
||||
for (let j = 0; j < historical.length; j++) {
|
||||
const entryDate = new Date(historical[j]['date'])
|
||||
const diff = Math.abs(entryDate - targetDate)
|
||||
|
||||
if (diff < minDiff) {
|
||||
minDiff = diff
|
||||
closestEntry = historical[j]
|
||||
}
|
||||
}
|
||||
|
||||
if (closestEntry) {
|
||||
hist_entries.push(closestEntry)
|
||||
}
|
||||
}
|
||||
// console.log(hist_entries)
|
||||
let final_list = []
|
||||
let today_sats = await this.bfx()
|
||||
|
||||
// Try to get current price from API, fallback to latest historical data if API fails
|
||||
let today_sats
|
||||
try {
|
||||
today_sats = await this.bfx()
|
||||
} catch (apiError) {
|
||||
console.log("API failed, using latest historical data as fallback")
|
||||
// Use the most recent entry in historical data as today's price
|
||||
today_sats = historical[historical.length - 1]['sateur_rate']
|
||||
}
|
||||
|
||||
for (var v = 0; v < hist_entries.length; v++) {
|
||||
const date = new Date(hist_entries[v]['date'])
|
||||
year = date.getFullYear();
|
||||
|
||||
Reference in New Issue
Block a user