fix(quotes): add logging + parallel fetch for external API diagnostics

This commit is contained in:
Jose Selesan
2026-08-27 11:51:21 -03:00
parent d8aed4970c
commit b78fcbc0e9

View File

@@ -113,14 +113,21 @@ export async function getMonthlyLast(c: Context) {
const externalData = new Map<string, Map<string, number>>();
for (const [type, casa] of Object.entries(CASA_MAP)) {
if (!missingByType.has(type)) continue;
const fetches = Object.entries(CASA_MAP).map(async ([type, casa]) => {
if (!missingByType.has(type)) return;
try {
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), 30_000);
const res = await fetch(
`https://api.argentinadatos.com/v1/cotizaciones/dolares/${casa}`,
{ signal: controller.signal },
);
if (!res.ok) continue;
clearTimeout(timeout);
if (!res.ok) {
console.error(`[monthlyLast] ${casa} responded ${res.status}`);
return;
}
const quotes: ExternalQuote[] = await res.json();
const monthLast = new Map<string, { fecha: string; compra: number }>();
@@ -143,10 +150,15 @@ export async function getMonthlyLast(c: Context) {
typeMap.set(month, data.compra);
}
externalData.set(type, typeMap);
} catch {
// If external API fails, continue without those values
console.error(
`[monthlyLast] ${casa}: fetched ${quotes.length} quotes, ${monthLast.size} months`,
);
} catch (e) {
console.error(`[monthlyLast] ${casa} fetch failed:`, e);
}
}
});
await Promise.all(fetches);
const result: MonthlyLastRow[] = [];
for (const month of expectedMonths) {
@@ -164,6 +176,10 @@ export async function getMonthlyLast(c: Context) {
}
}
console.error(
`[monthlyLast] result: ${result.length} rows, months: ${[...new Set(result.map((r) => r.month))].sort().join(", ")}`,
);
cache.set(cacheKey, result, 300);
return c.json(result);
}