Merge pull request #6 from aljazceru/claude/add-price-table-chart-011CUxoDYiavjyuvicvGcFQr

Fix price table display by using closest-match dates and API fallback
This commit is contained in:
2025-11-09 20:46:00 +01:00
committed by GitHub

View File

@@ -30,18 +30,40 @@ module.exports = {
// get all the years you need from 1 - 10 // get all the years you need from 1 - 10
for (let i = 1; i < 11; i++) { for (let i = 1; i < 11; i++) {
const y = new Date(new Date().setFullYear(new Date().getFullYear() - i)).toISOString().slice(0, 10) const targetDate = new Date(new Date().setFullYear(new Date().getFullYear() - i))
datelist.push(y) 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++) { for (let j = 0; j < historical.length; j++) {
const hdate = historical[j]['date'] const entryDate = new Date(historical[j]['date'])
if (datelist.includes(hdate)) { const diff = Math.abs(entryDate - targetDate)
hist_entries.push(historical[j])
if (diff < minDiff) {
minDiff = diff
closestEntry = historical[j]
}
}
if (closestEntry) {
hist_entries.push(closestEntry)
} }
} }
// console.log(hist_entries) // console.log(hist_entries)
let final_list = [] 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++) { for (var v = 0; v < hist_entries.length; v++) {
const date = new Date(hist_entries[v]['date']) const date = new Date(hist_entries[v]['date'])
year = date.getFullYear(); year = date.getFullYear();