Skip to content

Commit 4cc0c98

Browse files
committed
Optimize loading performance: parallel API calls, reduced timeouts, progress indicators
1 parent e6afbe5 commit 4cc0c98

File tree

3 files changed

+45
-48
lines changed

3 files changed

+45
-48
lines changed

images/macro-dashboard1.png

-354 KB
Loading

images/macro-dashboard2.png

-327 KB
Loading

index.html

Lines changed: 45 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -503,12 +503,12 @@ <h2 class="text-base font-medium mb-2 text-slate-900 dark:text-white">Treasury A
503503

504504
console.log(`🔍 Fetching FRED series: ${seriesId}`);
505505

506-
// Optimized approach - try fastest proxy first with reduced data limit
506+
// Fast approach - use only the most reliable proxy with minimal data
507507
const approaches = [
508-
// Approach 1: AllOrigins (most reliable) - reduced data for speed
508+
// Primary: AllOrigins (fastest and most reliable) - minimal data for speed
509509
async () => {
510510
const proxyUrl = 'https://api.allorigins.win/get?url=';
511-
const fredUrl = `https://api.stlouisfed.org/fred/series/observations?series_id=${seriesId}&api_key=${FRED_API_KEY}&file_type=json&limit=120&sort_order=desc`;
511+
const fredUrl = `https://api.stlouisfed.org/fred/series/observations?series_id=${seriesId}&api_key=${FRED_API_KEY}&file_type=json&limit=90&sort_order=desc`;
512512

513513
const response = await fetch(proxyUrl + encodeURIComponent(fredUrl), {
514514
method: 'GET',
@@ -528,26 +528,6 @@ <h2 class="text-base font-medium mb-2 text-slate-900 dark:text-white">Treasury A
528528
return data;
529529
},
530530

531-
// Approach 2: CORS Proxy
532-
async () => {
533-
const proxyUrl = 'https://corsproxy.io/?';
534-
const fredUrl = `https://api.stlouisfed.org/fred/series/observations?series_id=${seriesId}&api_key=${FRED_API_KEY}&file_type=json&limit=300&sort_order=desc`;
535-
console.log(`📡 Trying CORS Proxy for ${seriesId}...`);
536-
537-
const response = await fetch(proxyUrl + encodeURIComponent(fredUrl), {
538-
method: 'GET',
539-
headers: { 'Accept': 'application/json' }
540-
});
541-
542-
if (!response.ok) {
543-
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
544-
}
545-
546-
const data = await response.json();
547-
console.log(`📊 CORS Proxy response for ${seriesId}:`, data.observations?.length || 0, 'observations');
548-
return data;
549-
},
550-
551531
// Approach 3: Try without proxy (might work in some environments)
552532
async () => {
553533
const fredUrl = `https://api.stlouisfed.org/fred/series/observations?series_id=${seriesId}&api_key=${FRED_API_KEY}&file_type=json&limit=300&sort_order=desc`;
@@ -630,35 +610,24 @@ <h2 class="text-base font-medium mb-2 text-slate-900 dark:text-white">Treasury A
630610

631611
const apis = [
632612
{
633-
name: 'CoinGecko',
634-
fetch: async () => {
635-
const response = await fetch('https://api.coingecko.com/api/v3/coins/bitcoin/market_chart?vs_currency=usd&days=180&interval=daily');
636-
const data = await response.json();
637-
return data.prices.map(([timestamp, price]) => ({
638-
date: new Date(timestamp).toISOString().split('T')[0],
639-
value: price
640-
}));
641-
}
642-
},
643-
{
644-
name: 'CoinCap',
613+
name: 'Binance',
645614
fetch: async () => {
646-
const response = await fetch('https://api.coincap.io/v2/assets/bitcoin/history?interval=d1&start=' + (Date.now() - 180 * 24 * 60 * 60 * 1000));
615+
const response = await fetch('https://api.binance.com/api/v3/klines?symbol=BTCUSDT&interval=1d&limit=120');
647616
const data = await response.json();
648-
return data.data.map(item => ({
649-
date: new Date(item.time).toISOString().split('T')[0],
650-
value: parseFloat(item.priceUsd)
617+
return data.map(item => ({
618+
date: new Date(item[0]).toISOString().split('T')[0],
619+
value: parseFloat(item[4]) // Close price
651620
}));
652621
}
653622
},
654623
{
655-
name: 'Binance',
624+
name: 'CoinGecko',
656625
fetch: async () => {
657-
const response = await fetch('https://api.binance.com/api/v3/klines?symbol=BTCUSDT&interval=1d&limit=180');
626+
const response = await fetch('https://api.coingecko.com/api/v3/coins/bitcoin/market_chart?vs_currency=usd&days=120&interval=daily');
658627
const data = await response.json();
659-
return data.map(item => ({
660-
date: new Date(item[0]).toISOString().split('T')[0],
661-
value: parseFloat(item[4]) // Close price
628+
return data.prices.map(([timestamp, price]) => ({
629+
date: new Date(timestamp).toISOString().split('T')[0],
630+
value: price
662631
}));
663632
}
664633
}
@@ -1353,7 +1322,7 @@ <h2 class="text-base font-medium mb-2 text-slate-900 dark:text-white">Treasury A
13531322

13541323
const data = await loadDataWithRetry(chartId, () => {
13551324
const timeoutPromise = new Promise((_, reject) =>
1356-
setTimeout(() => reject(new Error('Timeout')), 5000)
1325+
setTimeout(() => reject(new Error('Timeout')), 3000)
13571326
);
13581327
const dataPromise = fetchFRED(series.id);
13591328
return Promise.race([dataPromise, timeoutPromise]);
@@ -1368,10 +1337,38 @@ <h2 class="text-base font-medium mb-2 text-slate-900 dark:text-white">Treasury A
13681337
}
13691338
});
13701339

1371-
// Wait for both Bitcoin and FRED data in parallel
1340+
// Wait for both Bitcoin and FRED data in parallel with progress updates
1341+
let completedCount = 0;
1342+
const totalCount = 6; // 1 Bitcoin + 5 FRED series
1343+
1344+
// Update progress as each API completes
1345+
const updateProgress = () => {
1346+
completedCount++;
1347+
const percentage = Math.round((completedCount / totalCount) * 100);
1348+
statusEl.innerHTML = `
1349+
<div class="flex items-center space-x-2">
1350+
<div class="animate-spin rounded-full h-4 w-4 border-b-2 border-blue-600"></div>
1351+
<p class="${isDark ? 'text-blue-300' : 'text-blue-700'}">⚡ Loading data... ${percentage}% (${completedCount}/${totalCount})</p>
1352+
</div>
1353+
`;
1354+
};
1355+
1356+
// Add progress tracking to promises
1357+
const bitcoinWithProgress = bitcoinPromise.then(result => {
1358+
updateProgress();
1359+
return result;
1360+
});
1361+
1362+
const fredWithProgress = fredPromises.map(promise =>
1363+
promise.then(result => {
1364+
updateProgress();
1365+
return result;
1366+
})
1367+
);
1368+
13721369
const [bitcoinResult, fredResults] = await Promise.all([
1373-
bitcoinPromise,
1374-
Promise.all(fredPromises)
1370+
bitcoinWithProgress,
1371+
Promise.all(fredWithProgress)
13751372
]);
13761373

13771374
fredResults.forEach(result => {

0 commit comments

Comments
 (0)