mirror of
https://github.com/koala73/worldmonitor.git
synced 2026-04-25 17:14:57 +02:00
* feat(seeds): Eurostat house prices + quarterly debt + industrial production Adds three new Eurostat overlay seeders covering all 27 EU members plus EA20 and EU27_2020 aggregates (issue #3028): - prc_hpi_a (annual house price index, 10y sparkline, TTL 35d) key: economic:eurostat:house-prices:v1 complements BIS WS_SPP (#3026) for the Housing cycle tile - gov_10q_ggdebt (quarterly gov debt %GDP, 8q sparkline, TTL 14d) key: economic:eurostat:gov-debt-q:v1 upgrades National Debt card cadence from annual IMF to quarterly for EU - sts_inpr_m (monthly industrial production, 12m sparkline, TTL 5d) key: economic:eurostat:industrial-production:v1 feeds "Real economy pulse" sparkline on Economic Indicators card Shared JSON-stat parser in scripts/_eurostat-utils.mjs handles the EL/GR and EA20 geo quirks and returns full time series for sparklines. Wires each seeder into bootstrap (SLOW_KEYS), health registries (keys + seed-meta thresholds matched to cadence), macro seed bundle, cache-keys shared module, and the MCP tool registry (get_eu_housing_cycle, get_eu_quarterly_gov_debt, get_eu_industrial_production). MCP tool count updated to 31. Tests cover JSON-stat parsing, sparkline ordering, EU-only coverage gating (non-EU geos return null so panels never render blank tiles), validator thresholds, and registry wiring across all surfaces. https://claude.ai/code/session_01Tgm6gG5yUMRoc2LRAKvmza * fix(bootstrap): register new Eurostat keys in tiers, defer consumers Adds eurostatHousePrices/GovDebtQ/IndProd to BOOTSTRAP_TIERS ('slow') to match SLOW_KEYS in api/bootstrap.js, and lists them as PENDING_CONSUMERS in the hydration coverage test (panel wiring lands in follow-up). * fix(eurostat): raise seeder coverage thresholds to catch partial publishes The three Eurostat overlay seeders (house prices, quarterly gov debt, monthly industrial production) all validated with makeValidator(10) against a fixed 29-geo universe (EU27 + EA20 + EU27_2020). A bad run returning only 10-15 geos would pass validation and silently publish a snapshot missing most of the EU. Raise thresholds to near-complete coverage, with a small margin for geos with patchy reporting: - house prices (annual): 10 -> 24 - gov debt (quarterly): 10 -> 24 - industrial prod (monthly): 10 -> 22 (monthly is slightly patchier) Add a guard test that asserts every overlay seeder keeps its threshold >=22 so this regression can't reappear. * fix(seed-health): register 3 Eurostat seed-meta entries house-prices, gov-debt-q, industrial-production were wired in api/health.js SEED_META but missing from api/seed-health.js SEED_DOMAINS, so /api/seed-health would not surface their freshness. intervalMin = health.js maxStaleMin / 2 per convention. --------- Co-authored-by: Claude <noreply@anthropic.com>
262 lines
11 KiB
JavaScript
262 lines
11 KiB
JavaScript
import { getCorsHeaders, getPublicCorsHeaders, isDisallowedOrigin } from './_cors.js';
|
|
import { validateApiKey } from './_api-key.js';
|
|
import { jsonResponse } from './_json-response.js';
|
|
// @ts-expect-error — JS module, no declaration file
|
|
import { redisPipeline } from './_upstash-json.js';
|
|
|
|
export const config = { runtime: 'edge' };
|
|
|
|
const BOOTSTRAP_CACHE_KEYS = {
|
|
earthquakes: 'seismology:earthquakes:v1',
|
|
outages: 'infra:outages:v1',
|
|
serviceStatuses: 'infra:service-statuses:v1',
|
|
ddosAttacks: 'cf:radar:ddos:v1',
|
|
trafficAnomalies: 'cf:radar:traffic-anomalies:v1',
|
|
marketQuotes: 'market:stocks-bootstrap:v1',
|
|
commodityQuotes: 'market:commodities-bootstrap:v1',
|
|
sectors: 'market:sectors:v2',
|
|
etfFlows: 'market:etf-flows:v1',
|
|
macroSignals: 'economic:macro-signals:v1',
|
|
bisPolicy: 'economic:bis:policy:v1',
|
|
bisExchange: 'economic:bis:eer:v1',
|
|
bisCredit: 'economic:bis:credit:v1',
|
|
imfMacro: 'economic:imf:macro:v2',
|
|
imfGrowth: 'economic:imf:growth:v1',
|
|
imfLabor: 'economic:imf:labor:v1',
|
|
imfExternal: 'economic:imf:external:v1',
|
|
shippingRates: 'supply_chain:shipping:v2',
|
|
chokepoints: 'supply_chain:chokepoints:v4',
|
|
minerals: 'supply_chain:minerals:v2',
|
|
giving: 'giving:summary:v1',
|
|
climateAnomalies: 'climate:anomalies:v2',
|
|
climateDisasters: 'climate:disasters:v1',
|
|
co2Monitoring: 'climate:co2-monitoring:v1',
|
|
oceanIce: 'climate:ocean-ice:v1',
|
|
climateNews: 'climate:news-intelligence:v1',
|
|
radiationWatch: 'radiation:observations:v1',
|
|
thermalEscalation: 'thermal:escalation:v1',
|
|
crossSourceSignals: 'intelligence:cross-source-signals:v1',
|
|
wildfires: 'wildfire:fires:v1',
|
|
cyberThreats: 'cyber:threats-bootstrap:v2',
|
|
techReadiness: 'economic:worldbank-techreadiness:v1',
|
|
progressData: 'economic:worldbank-progress:v1',
|
|
renewableEnergy: 'economic:worldbank-renewable:v1',
|
|
positiveGeoEvents: 'positive_events:geo-bootstrap:v1',
|
|
theaterPosture: 'theater_posture:sebuf:stale:v1',
|
|
riskScores: 'risk:scores:sebuf:stale:v1',
|
|
naturalEvents: 'natural:events:v1',
|
|
flightDelays: 'aviation:delays-bootstrap:v1',
|
|
insights: 'news:insights:v1',
|
|
predictions: 'prediction:markets-bootstrap:v1',
|
|
cryptoQuotes: 'market:crypto:v1',
|
|
cryptoSectors: 'market:crypto-sectors:v1',
|
|
defiTokens: 'market:defi-tokens:v1',
|
|
aiTokens: 'market:ai-tokens:v1',
|
|
otherTokens: 'market:other-tokens:v1',
|
|
gulfQuotes: 'market:gulf-quotes:v1',
|
|
stablecoinMarkets: 'market:stablecoins:v1',
|
|
unrestEvents: 'unrest:events:v1',
|
|
iranEvents: 'conflict:iran-events:v1',
|
|
ucdpEvents: 'conflict:ucdp-events:v1',
|
|
temporalAnomalies: 'temporal:anomalies:v1',
|
|
weatherAlerts: 'weather:alerts:v1',
|
|
spending: 'economic:spending:v1',
|
|
techEvents: 'research:tech-events-bootstrap:v1',
|
|
gdeltIntel: 'intelligence:gdelt-intel:v1',
|
|
correlationCards: 'correlation:cards-bootstrap:v1',
|
|
forecasts: 'forecast:predictions:v2',
|
|
securityAdvisories: 'intelligence:advisories-bootstrap:v1',
|
|
customsRevenue: 'trade:customs-revenue:v1',
|
|
sanctionsPressure: 'sanctions:pressure:v1',
|
|
consumerPricesOverview: 'consumer-prices:overview:ae',
|
|
consumerPricesCategories: 'consumer-prices:categories:ae:30d',
|
|
consumerPricesMovers: 'consumer-prices:movers:ae:30d',
|
|
consumerPricesSpread: 'consumer-prices:retailer-spread:ae:essentials-ae',
|
|
groceryBasket: 'economic:grocery-basket:v1',
|
|
bigmac: 'economic:bigmac:v1',
|
|
fuelPrices: 'economic:fuel-prices:v1',
|
|
faoFoodPriceIndex: 'economic:fao-ffpi:v1',
|
|
nationalDebt: 'economic:national-debt:v1',
|
|
euGasStorage: 'economic:eu-gas-storage:v1',
|
|
eurostatCountryData: 'economic:eurostat-country-data:v1',
|
|
eurostatHousePrices: 'economic:eurostat:house-prices:v1',
|
|
eurostatGovDebtQ: 'economic:eurostat:gov-debt-q:v1',
|
|
eurostatIndProd: 'economic:eurostat:industrial-production:v1',
|
|
marketImplications: 'intelligence:market-implications:v1',
|
|
fearGreedIndex: 'market:fear-greed:v1',
|
|
crudeInventories: 'economic:crude-inventories:v1',
|
|
natGasStorage: 'economic:nat-gas-storage:v1',
|
|
ecbFxRates: 'economic:ecb-fx-rates:v1',
|
|
euFsi: 'economic:fsi-eu:v1',
|
|
shippingStress: 'supply_chain:shipping_stress:v1',
|
|
socialVelocity: 'intelligence:social:reddit:v1',
|
|
wsbTickers: 'intelligence:wsb-tickers:v1',
|
|
pizzint: 'intelligence:pizzint:seed:v1',
|
|
diseaseOutbreaks: 'health:disease-outbreaks:v1',
|
|
economicStress: 'economic:stress-index:v1',
|
|
electricityPrices: 'energy:electricity:v1:index',
|
|
jodiOil: 'energy:jodi-oil:v1:_countries',
|
|
chokepointBaselines: 'energy:chokepoint-baselines:v1',
|
|
portwatchChokepointsRef: 'portwatch:chokepoints:ref:v1',
|
|
portwatchPortActivity: 'supply_chain:portwatch-ports:v1:_countries',
|
|
oilStocksAnalysis: 'energy:oil-stocks-analysis:v1',
|
|
lngVulnerability: 'energy:lng-vulnerability:v1',
|
|
sprPolicies: 'energy:spr-policies:v1',
|
|
energyCrisisPolicies: 'energy:crisis-policies:v1',
|
|
aaiiSentiment: 'market:aaii-sentiment:v1',
|
|
breadthHistory: 'market:breadth-history:v1',
|
|
};
|
|
|
|
const SLOW_KEYS = new Set([
|
|
'bisPolicy', 'bisExchange', 'bisCredit', 'imfMacro', 'imfGrowth', 'imfLabor', 'imfExternal', 'minerals', 'giving',
|
|
'sectors', 'etfFlows', 'wildfires', 'climateAnomalies', 'climateDisasters', 'co2Monitoring', 'oceanIce', 'climateNews',
|
|
'radiationWatch', 'thermalEscalation', 'crossSourceSignals',
|
|
'cyberThreats', 'techReadiness', 'progressData', 'renewableEnergy',
|
|
'naturalEvents',
|
|
'cryptoQuotes', 'cryptoSectors', 'defiTokens', 'aiTokens', 'otherTokens',
|
|
'gulfQuotes', 'stablecoinMarkets', 'unrestEvents', 'ucdpEvents',
|
|
'techEvents',
|
|
'securityAdvisories',
|
|
'customsRevenue',
|
|
'sanctionsPressure',
|
|
'consumerPricesOverview', 'consumerPricesCategories', 'consumerPricesMovers', 'consumerPricesSpread',
|
|
'groceryBasket',
|
|
'bigmac',
|
|
'fuelPrices',
|
|
'faoFoodPriceIndex',
|
|
'nationalDebt',
|
|
'euGasStorage',
|
|
'eurostatCountryData',
|
|
'eurostatHousePrices',
|
|
'eurostatGovDebtQ',
|
|
'eurostatIndProd',
|
|
'marketImplications',
|
|
'fearGreedIndex',
|
|
'crudeInventories',
|
|
'natGasStorage',
|
|
'ecbFxRates',
|
|
'euFsi',
|
|
'diseaseOutbreaks',
|
|
'economicStress',
|
|
'pizzint',
|
|
'electricityPrices',
|
|
'jodiOil',
|
|
'chokepointBaselines',
|
|
'portwatchChokepointsRef',
|
|
'portwatchPortActivity',
|
|
'oilStocksAnalysis',
|
|
'lngVulnerability',
|
|
'sprPolicies',
|
|
'energyCrisisPolicies',
|
|
'aaiiSentiment',
|
|
'breadthHistory',
|
|
]);
|
|
const FAST_KEYS = new Set([
|
|
'earthquakes', 'outages', 'serviceStatuses', 'ddosAttacks', 'trafficAnomalies', 'macroSignals', 'chokepoints',
|
|
'marketQuotes', 'commodityQuotes', 'positiveGeoEvents', 'riskScores', 'flightDelays','insights', 'predictions',
|
|
'iranEvents', 'temporalAnomalies', 'weatherAlerts', 'spending', 'theaterPosture', 'gdeltIntel',
|
|
'correlationCards', 'forecasts', 'shippingRates', 'shippingStress', 'socialVelocity', 'wsbTickers',
|
|
]);
|
|
|
|
// No public/s-maxage: CF (in front of api.worldmonitor.app) ignores Vary: Origin and would
|
|
// pin ACAO: worldmonitor.app on cached responses, breaking CORS for preview deployments.
|
|
// Vercel CDN caching is handled by TIER_CDN_CACHE via CDN-Cache-Control below.
|
|
const TIER_CACHE = {
|
|
slow: 'max-age=300, stale-while-revalidate=600, stale-if-error=3600',
|
|
fast: 'max-age=60, stale-while-revalidate=120, stale-if-error=900',
|
|
};
|
|
const TIER_CDN_CACHE = {
|
|
slow: 'public, s-maxage=7200, stale-while-revalidate=1800, stale-if-error=7200',
|
|
fast: 'public, s-maxage=600, stale-while-revalidate=120, stale-if-error=900',
|
|
};
|
|
|
|
const NEG_SENTINEL = '__WM_NEG__';
|
|
|
|
async function getCachedJsonBatch(keys) {
|
|
const result = new Map();
|
|
if (keys.length === 0) return result;
|
|
|
|
// Always read unprefixed keys — bootstrap is a read-only consumer of
|
|
// production cache data. Preview/branch deploys don't run handlers that
|
|
// populate prefixed keys, so prefixing would always miss.
|
|
const pipeline = keys.map((k) => ['GET', k]);
|
|
const data = await redisPipeline(pipeline, 3000);
|
|
if (!data) return result;
|
|
|
|
for (let i = 0; i < keys.length; i++) {
|
|
const raw = data[i]?.result;
|
|
if (raw) {
|
|
try {
|
|
const parsed = JSON.parse(raw);
|
|
if (parsed !== NEG_SENTINEL) result.set(keys[i], parsed);
|
|
} catch { /* skip malformed */ }
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
export default async function handler(req) {
|
|
if (isDisallowedOrigin(req))
|
|
return new Response('Forbidden', { status: 403 });
|
|
|
|
const cors = getCorsHeaders(req);
|
|
if (req.method === 'OPTIONS')
|
|
return new Response(null, { status: 204, headers: cors });
|
|
|
|
const apiKeyResult = validateApiKey(req);
|
|
if (apiKeyResult.required && !apiKeyResult.valid)
|
|
return jsonResponse({ error: apiKeyResult.error }, 401, cors);
|
|
|
|
const url = new URL(req.url);
|
|
const tier = url.searchParams.get('tier');
|
|
let registry;
|
|
if (tier === 'slow' || tier === 'fast') {
|
|
const tierSet = tier === 'slow' ? SLOW_KEYS : FAST_KEYS;
|
|
registry = Object.fromEntries(Object.entries(BOOTSTRAP_CACHE_KEYS).filter(([k]) => tierSet.has(k)));
|
|
} else {
|
|
const requested = url.searchParams.get('keys')?.split(',').filter(Boolean).sort();
|
|
registry = requested
|
|
? Object.fromEntries(Object.entries(BOOTSTRAP_CACHE_KEYS).filter(([k]) => requested.includes(k)))
|
|
: BOOTSTRAP_CACHE_KEYS;
|
|
}
|
|
|
|
const keys = Object.values(registry);
|
|
const names = Object.keys(registry);
|
|
|
|
let cached;
|
|
try {
|
|
cached = await getCachedJsonBatch(keys);
|
|
} catch {
|
|
return jsonResponse({ data: {}, missing: names }, 200, { ...cors, 'Cache-Control': 'no-cache' });
|
|
}
|
|
|
|
const data = {};
|
|
const missing = [];
|
|
for (let i = 0; i < names.length; i++) {
|
|
const val = cached.get(keys[i]);
|
|
if (val !== undefined) {
|
|
// Strip seed-internal metadata not intended for API clients
|
|
if (names[i] === 'forecasts' && val != null && 'enrichmentMeta' in val) {
|
|
const { enrichmentMeta: _stripped, ...rest } = val;
|
|
data[names[i]] = rest;
|
|
} else {
|
|
data[names[i]] = val;
|
|
}
|
|
} else {
|
|
missing.push(names[i]);
|
|
}
|
|
}
|
|
|
|
const cacheControl = (tier && TIER_CACHE[tier]) || 'public, s-maxage=600, stale-while-revalidate=120, stale-if-error=900';
|
|
|
|
// Bootstrap data is fully public (world events, market prices, seismic data).
|
|
// Use ACAO: * so CF caches one entry valid for all origins, including Vercel
|
|
// preview deployments. Per-origin ACAO with Vary: Origin causes CF to pin the
|
|
// first origin's ACAO on the cached response, breaking CORS for other origins.
|
|
return jsonResponse({ data, missing }, 200, {
|
|
...getPublicCorsHeaders(),
|
|
'Cache-Control': cacheControl,
|
|
'CDN-Cache-Control': (tier && TIER_CDN_CACHE[tier]) || TIER_CDN_CACHE.fast,
|
|
});
|
|
}
|