Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 | 1x 1x 1x 1x 1x 1x 1x 23x 23x 23x 23x 115x 23x 1x 1x 36x 36x 36x 36x 36x 36x 23x 2x 21x 21x 21x 21x 1x 21x 13x 13x 36x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 5x 4x 4x 3x 1x 4x 4x 4x 4x 2x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 2x 4x 2x 2x 2x 2x 4x 4x 4x 4x 4x 4x 2x 11x 11x 11x 11x 11x 11x 5x 6x 6x 11x 2x 4x 4x 20x 20x 7x 7x 6x 4x 1x 3x 8x 8x 8x 8x 8x 16x 16x 16x 16x 8x 16x 16x 10x 10x 10x 10x 10x 8x 8x 10x 4x 8x 4x 4x 8x 6x 2x 7x 3x 4x 4x 4x 4x 7x 3x 1x 7x 7x 7x 7x 7x 7x 8x 7x 4x 4x 4x 4x 2x 2x 4x 12x 12x 12x 3x 3x 3x 2x 2x 3x 3x 9x 1x | #!/usr/bin/env node
// SPDX-FileCopyrightText: 2024-2026 Hack23 AB
// SPDX-License-Identifier: Apache-2.0
/**
* @module scripts/imf-fallback-ladder
* @description IMF economic data fallback ladder for EU Parliament Monitor.
*
* Implements a four-rung fallback strategy for IMF economic-context data:
*
* Rung 1 — IMF SDMX 3.0 REST API (`api.imf.org/external/sdmx/3.0`)
* Rung 2 — IMF WEO DataMapper REST API (`imf.org/external/datamapper/api/v1`)
* Rung 3 — World Bank API country-level aggregation (EU27 proxy sum)
* Rung 4 — Cached IMF WEO vintage snapshot from prior run
*
* Each rung is tried in order. The first rung that returns usable data wins.
* The output JSON includes a `provenance` map recording which rung supplied
* each field, so downstream analysis can cite the data source accurately.
*
* Output: `<outputDir>/economic-context-data.json`
*
* Invocation:
* node scripts/imf-fallback-ladder.js \
* --output-dir analysis/daily/2026-05-14/breaking/data \
* [--run-id breaking-run-1234] \
* [--cache-dir analysis/daily/2026-05-14/breaking/cache/imf] \
* [--vintage-file path/to/cached-weo.json]
*
* Exports (for unit testing):
* tryImfSdmx(options)
* tryImfDataMapper(options)
* tryWorldBank(options)
* tryCache(cacheFile)
* runFallbackLadder(outputDir, options)
* buildEconomicContextPayload(data, provenance, rung)
*/
import fs from 'node:fs';
import path from 'node:path';
import process from 'node:process';
import { fileURLToPath } from 'node:url';
// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------
/** Default IMF SDMX 3.0 base URL. */
const IMF_SDMX_BASE = 'https://api.imf.org/external/sdmx/3.0';
/** IMF DataMapper API base URL (lighter alternative to SDMX 3.0). */
const IMF_DATAMAPPER_BASE = 'https://www.imf.org/external/datamapper/api/v1';
/** World Bank API base URL for country-level indicators. */
const WORLD_BANK_API_BASE = 'https://api.worldbank.org/v2';
/** All EU27 member state ISO-3166-1 alpha-3 codes for World Bank proxy aggregation. */
const EU_PROXY_COUNTRIES = [
'AUT', 'BEL', 'BGR', 'HRV', 'CYP', 'CZE', 'DNK', 'EST', 'FIN', 'FRA',
'DEU', 'GRC', 'HUN', 'IRL', 'ITA', 'LVA', 'LTU', 'LUX', 'MLT', 'NLD',
'POL', 'PRT', 'ROU', 'SVK', 'SVN', 'ESP', 'SWE',
];
/** World Bank indicator → IMF indicator mapping. */
const WB_TO_IMF_INDICATOR = {
'NY.GDP.MKTP.KD.ZG': 'NGDP_RPCH',
'FP.CPI.TOTL.ZG': 'PCPIPCH',
};
/** IMF DataMapper indicators to fetch. */
const IMF_DATAMAPPER_INDICATORS = ['NGDP_RPCH', 'PCPIPCH', 'GGXCNL_NGDP', 'LUR', 'BCA_NGDPD'];
/** Target IMF area code for Euro Area. */
const IMF_EURO_AREA = 'EAQ';
/**
* Compute the dynamic [startYear, endYear] window for IMF queries.
* IMF WEO publishes ~5 forecast years from any given vintage; we request
* one historical year + the run year + 3 forecast years so the window
* advances automatically as `runYear` advances.
*
* @param {number} [runYear] - Reference year (defaults to current UTC year)
* @returns {{ startYear: number, endYear: number, years: number[] }}
*/
export function computeImfPeriodWindow(runYear) {
const y = runYear ?? new Date().getUTCFullYear();
const startYear = y - 1;
const endYear = y + 3;
const years = [];
for (let yr = startYear; yr <= endYear; yr++) years.push(yr);
return { startYear, endYear, years };
}
/** Request timeout in milliseconds. */
const FETCH_TIMEOUT_MS = 30_000;
/** Output filename. */
const OUTPUT_FILENAME = 'economic-context-data.json';
// ---------------------------------------------------------------------------
// HTTP helpers
// ---------------------------------------------------------------------------
/**
* Fetch a URL with a timeout and return the JSON-parsed body.
*
* @param {string} url
* @param {{ fetchImpl?: Function, timeoutMs?: number, headers?: Record<string,string> }} [options]
* @returns {Promise<{ok: boolean, status: number, data: unknown}>}
*/
async function fetchJson(url, options = {}) {
const fetchFn = options.fetchImpl ?? fetch;
const timeoutMs = options.timeoutMs ?? FETCH_TIMEOUT_MS;
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), timeoutMs);
try {
const resp = await fetchFn(url, {
signal: controller.signal,
headers: {
Accept: 'application/json, */*;q=0.8',
'User-Agent': 'euparliamentmonitor/0.9.0 (+https://github.com/Hack23/euparliamentmonitor)',
...options.headers,
},
});
if (!resp.ok) {
return { ok: false, status: resp.status, data: null };
}
const text = await resp.text();
let data = null;
try {
data = JSON.parse(text);
} catch {
data = null;
}
return { ok: true, status: resp.status, data };
} catch (err) {
const isAbort = err && err.name === 'AbortError';
return { ok: false, status: isAbort ? 408 : 0, data: null };
} finally {
clearTimeout(timer);
}
}
// ---------------------------------------------------------------------------
// Rung 1 — IMF SDMX 3.0
// ---------------------------------------------------------------------------
/**
* Try to fetch GDP growth and inflation data for the Euro Area from IMF SDMX 3.0.
*
* @param {{ fetchImpl?: Function, baseUrl?: string }} [options]
* @returns {Promise<{data: EconomicData|null, provenance: string}>}
*/
export async function tryImfSdmx(options = {}) {
const baseUrl = options.baseUrl ?? IMF_SDMX_BASE;
const { startYear, endYear } = computeImfPeriodWindow(options.runYear);
const query = `data/dataflow/IMF.RES/WEO/+/${IMF_EURO_AREA}.NGDP_RPCH+PCPIPCH+GGXCNL_NGDP.A?startPeriod=${startYear}&endPeriod=${endYear}&format=jsondata`;
const url = `${baseUrl}/${query}`;
// IMF SDMX 3.0 requires Ocp-Apim-Subscription-Key since 2025
const headers = {};
const primaryKey = process.env.IMF_API_PRIMARY_KEY;
const secondaryKey = process.env.IMF_API_SECONDARY_KEY;
Iif (primaryKey) {
headers['Ocp-Apim-Subscription-Key'] = primaryKey;
} else Iif (secondaryKey) {
headers['Ocp-Apim-Subscription-Key'] = secondaryKey;
}
const result = await fetchJson(url, { fetchImpl: options.fetchImpl, headers });
if (!result.ok || !result.data) {
return { data: null, provenance: `imf-sdmx:unavailable(${result.status})` };
}
const extracted = _extractSdmxObservations(result.data);
if (!extracted || Object.keys(extracted).length === 0) {
return { data: null, provenance: 'imf-sdmx:no-observations' };
}
return { data: extracted, provenance: 'imf-sdmx-3.0' };
}
/**
* Extract observations from an SDMX JSON data response into a flat map.
*
* @param {unknown} payload - Parsed SDMX JSON
* @returns {Record<string, number|null>|null}
*/
function _extractSdmxObservations(payload) {
Iif (!payload || typeof payload !== 'object') return null;
const result = {};
const sdmxData = /** @type {any} */ (payload).data;
if (!sdmxData) return null;
const dataSets = Array.isArray(sdmxData.dataSets) ? sdmxData.dataSets : [];
const structure = sdmxData.structure;
const dimensions = structure?.dimensions ?? {};
// Build dimension value lookups from structure metadata
const seriesDims = Array.isArray(dimensions.series) ? dimensions.series : [];
const obsDims = Array.isArray(dimensions.observation) ? dimensions.observation : [];
// Find the INDICATOR dimension (could be in series or observation dims)
const indicatorDim = seriesDims.find((d) => d.id === 'INDICATOR') ??
obsDims.find((d) => d.id === 'INDICATOR');
const indicatorValues = indicatorDim?.values ?? [];
// Find the TIME_PERIOD dimension (typically in observation dims)
const timeDim = obsDims.find((d) => d.id === 'TIME_PERIOD') ??
seriesDims.find((d) => d.id === 'TIME_PERIOD');
const timeValues = timeDim?.values ?? [];
for (const dataSet of dataSets) {
// Handle flat observations format (key = colon-separated dimension indices)
const observations = dataSet?.observations ?? {};
for (const [key, value] of Object.entries(observations)) {
const parts = key.split(':');
const indicatorIdx = parseInt(parts[2] ?? '0', 10);
const periodIdx = parseInt(parts[parts.length - 1] ?? '0', 10);
const obsValue = Array.isArray(value) ? value[0] : null;
const indicatorName = indicatorValues[indicatorIdx]?.id ?? `indicator_${indicatorIdx}`;
const periodName = timeValues[periodIdx]?.id ?? `period_${periodIdx}`;
if (typeof obsValue === 'number') {
if (!result[indicatorName]) result[indicatorName] = {};
result[indicatorName][periodName] = obsValue;
}
}
// Handle series-based format (series key encodes dimension indices)
const series = dataSet?.series ?? {};
for (const [seriesKey, seriesData] of Object.entries(series)) {
const seriesParts = seriesKey.split(':');
// Find indicator index — typically the position matching the INDICATOR dim
const indicatorDimPos = seriesDims.findIndex((d) => d.id === 'INDICATOR');
const indicatorIdx = indicatorDimPos >= 0
? parseInt(seriesParts[indicatorDimPos] ?? '0', 10)
: parseInt(seriesParts[0] ?? '0', 10);
const indicatorName = indicatorValues[indicatorIdx]?.id ?? `series_${seriesKey}`;
const seriesObs = /** @type {any} */ (seriesData)?.observations ?? {};
for (const [periodKey, obsVal] of Object.entries(seriesObs)) {
const periodIdx = parseInt(periodKey, 10);
const periodName = timeValues[periodIdx]?.id ?? periodKey;
const val = Array.isArray(obsVal) ? obsVal[0] : null;
Eif (typeof val === 'number') {
if (!result[indicatorName]) result[indicatorName] = {};
result[indicatorName][periodName] = val;
}
}
}
}
return Object.keys(result).length > 0 ? result : null;
}
// ---------------------------------------------------------------------------
// Rung 2 — IMF DataMapper API (lighter / no auth required)
// ---------------------------------------------------------------------------
/**
* Try to fetch economic data from the IMF DataMapper REST API.
* This endpoint requires no authentication and returns data in a simpler format.
*
* @param {{ fetchImpl?: Function, baseUrl?: string }} [options]
* @returns {Promise<{data: object|null, provenance: string}>}
*/
export async function tryImfDataMapper(options = {}) {
const baseUrl = options.baseUrl ?? IMF_DATAMAPPER_BASE;
const indicators = IMF_DATAMAPPER_INDICATORS.join('+');
const { years } = computeImfPeriodWindow(options.runYear);
const url = `${baseUrl}/${indicators}/${IMF_EURO_AREA}?periods=${years.join(',')}`;
const result = await fetchJson(url, { fetchImpl: options.fetchImpl });
if (!result.ok || !result.data) {
return { data: null, provenance: `imf-datamapper:unavailable(${result.status})` };
}
const payload = /** @type {any} */ (result.data);
const values = payload?.values;
if (!values || typeof values !== 'object') {
return { data: null, provenance: 'imf-datamapper:no-values' };
}
const extracted = {};
for (const indicator of IMF_DATAMAPPER_INDICATORS) {
const indValues = values[indicator];
if (indValues && typeof indValues === 'object') {
const areaValues = indValues[IMF_EURO_AREA];
if (areaValues && typeof areaValues === 'object') {
extracted[indicator] = areaValues;
}
}
}
if (Object.keys(extracted).length === 0) {
return { data: null, provenance: 'imf-datamapper:no-euro-area-data' };
}
return { data: extracted, provenance: 'imf-datamapper-v1' };
}
// ---------------------------------------------------------------------------
// Rung 3 — World Bank country-level aggregation (EU proxy)
// ---------------------------------------------------------------------------
/**
* Try World Bank API for key EU country indicators, then compute a simple
* unweighted average as a proxy for Euro-area figures.
*
* @param {{ fetchImpl?: Function, baseUrl?: string, countries?: string[] }} [options]
* @returns {Promise<{data: object|null, provenance: string}>}
*/
export async function tryWorldBank(options = {}) {
const baseUrl = options.baseUrl ?? WORLD_BANK_API_BASE;
const countries = options.countries ?? EU_PROXY_COUNTRIES;
const results = {};
let fetchedCount = 0;
for (const [wbIndicator, imfName] of Object.entries(WB_TO_IMF_INDICATOR)) {
const countryStr = countries.join(';');
const url = `${baseUrl}/country/${countryStr}/indicator/${wbIndicator}?format=json&mrv=3&per_page=100`;
const resp = await fetchJson(url, { fetchImpl: options.fetchImpl });
if (!resp.ok || !Array.isArray(resp.data)) continue;
const items = resp.data[1] ?? [];
const byYear = {};
for (const item of items) {
const year = item?.date;
const val = item?.value;
Eif (year && typeof val === 'number') {
if (!byYear[year]) byYear[year] = [];
byYear[year].push(val);
}
}
const averaged = {};
for (const [year, vals] of Object.entries(byYear)) {
const sum = vals.reduce((a, b) => a + b, 0);
averaged[year] = sum / vals.length;
}
if (Object.keys(averaged).length > 0) {
results[imfName] = averaged;
fetchedCount += 1;
}
}
if (fetchedCount === 0) {
return { data: null, provenance: 'worldbank:no-data' };
}
return {
data: results,
provenance: `worldbank-country-avg(${countries.slice(0, 5).join(',')})`,
};
}
// ---------------------------------------------------------------------------
// Rung 4 — Cached vintage snapshot
// ---------------------------------------------------------------------------
/**
* Try to load a previously-cached `economic-context-data.json` file.
*
* @param {string} cacheFile - Path to the cached file
* @returns {Promise<{data: object|null, provenance: string}>}
*/
export async function tryCache(cacheFile) {
if (!cacheFile || !fs.existsSync(cacheFile)) {
return { data: null, provenance: 'cache:file-not-found' };
}
try {
const raw = fs.readFileSync(cacheFile, 'utf8');
const parsed = JSON.parse(raw);
const dataField = parsed.data ?? parsed;
Iif (!dataField || typeof dataField !== 'object') {
return { data: null, provenance: 'cache:malformed' };
}
return { data: dataField, provenance: `cache:${cacheFile}` };
} catch {
return { data: null, provenance: 'cache:read-error' };
}
}
// ---------------------------------------------------------------------------
// Payload builder
// ---------------------------------------------------------------------------
/**
* Build the canonical economic-context-data.json payload.
*
* @param {object} data - The economic data object
* @param {string} provenance - Human-readable provenance string
* @param {number} rung - Which fallback rung succeeded (1–4)
* @returns {object}
*/
export function buildEconomicContextPayload(data, provenance, rung) {
/** @type {Record<number, string>} */
const RUNG_LABELS = {
1: 'imf-sdmx-3.0',
2: 'imf-datamapper',
3: 'worldbank-proxy',
4: 'cached-vintage',
};
const rungLabel = RUNG_LABELS[rung] ?? 'unknown';
const quality = rung === 1 ? 'authoritative' : rung === 2 ? 'good' : rung === 3 ? 'proxy' : 'stale';
// Attach per-field provenance so downstream agents can cite sources accurately
const fieldProvenance = {};
Eif (data && typeof data === 'object') {
for (const key of Object.keys(data)) {
fieldProvenance[key] = { rung, rungLabel, quality, source: provenance };
}
}
return {
generatedAt: new Date().toISOString(),
provenance,
rung,
rungLabel,
data,
quality,
fieldProvenance,
};
}
// ---------------------------------------------------------------------------
// Ladder runner
// ---------------------------------------------------------------------------
/**
* Run the full IMF fallback ladder, trying each rung in order.
*
* @param {string} outputDir - Directory to write the output file into
* @param {{
* fetchImpl?: Function,
* cacheFile?: string,
* dryRun?: boolean,
* imfSdmxBase?: string,
* imfDataMapperBase?: string,
* worldBankBase?: string,
* wbCountries?: string[]
* }} [options]
* @returns {Promise<{
* success: boolean,
* rung: number,
* provenance: string,
* outputFile: string|null,
* error?: string
* }>}
*/
export async function runFallbackLadder(outputDir, options = {}) {
const { dryRun = false, fetchImpl, cacheFile, runYear } = options;
const rungs = [
{
n: 1,
label: 'IMF SDMX 3.0',
fn: () =>
tryImfSdmx({
fetchImpl,
baseUrl: options.imfSdmxBase,
runYear,
}),
},
{
n: 2,
label: 'IMF DataMapper',
fn: () =>
tryImfDataMapper({
fetchImpl,
baseUrl: options.imfDataMapperBase,
runYear,
}),
},
{
n: 3,
label: 'World Bank country proxy',
fn: () =>
tryWorldBank({
fetchImpl,
baseUrl: options.worldBankBase,
countries: options.wbCountries,
}),
},
{
n: 4,
label: 'Cached vintage',
fn: () => tryCache(cacheFile ?? ''),
},
];
for (const rung of rungs) {
let result;
try {
result = await rung.fn();
} catch (err) {
process.stderr.write(`⚠️ Rung ${rung.n} (${rung.label}) threw: ${err}\n`);
continue;
}
if (result.data !== null) {
const payload = buildEconomicContextPayload(result.data, result.provenance, rung.n);
const outputFile = path.join(outputDir, OUTPUT_FILENAME);
if (!dryRun) {
fs.mkdirSync(outputDir, { recursive: true });
fs.writeFileSync(outputFile, JSON.stringify(payload, null, 2), 'utf8');
}
process.stderr.write(
`✅ Economic context data from rung ${rung.n} (${rung.label}): ${result.provenance}\n`,
);
return {
success: true,
rung: rung.n,
provenance: result.provenance,
outputFile: dryRun ? null : outputFile,
};
}
process.stderr.write(`⚠️ Rung ${rung.n} (${rung.label}): ${result.provenance}\n`);
}
return {
success: false,
rung: 0,
provenance: 'all-rungs-failed',
outputFile: null,
error: 'All four fallback rungs returned no usable data',
};
}
// ---------------------------------------------------------------------------
// CLI entry point
// ---------------------------------------------------------------------------
/**
* Parse minimalist `--key value` CLI args.
*
* @param {string[]} argv
* @returns {Record<string, string|boolean>}
*/
/* c8 ignore start */
function parseArgs(argv) {
const out = {};
let i = 0;
while (i < argv.length) {
const arg = argv[i];
if (arg.startsWith('--')) {
const key = arg.slice(2);
const next = argv[i + 1];
if (next === undefined || next.startsWith('--')) {
out[key] = true;
i += 1;
} else {
out[key] = next;
i += 2;
}
} else {
i += 1;
}
}
return out;
}
/**
* CLI main entry point.
*
* @param {string[]} [argv]
* @returns {Promise<void>}
*/
export async function main(argv = process.argv.slice(2)) {
const args = parseArgs(argv);
const outputDir = args['output-dir']
? String(args['output-dir'])
: path.join(process.cwd(), 'analysis', 'cache', 'imf');
const cacheFile = args['vintage-file'] ? String(args['vintage-file']) : undefined;
const dryRun = args['dry-run'] === true;
const result = await runFallbackLadder(outputDir, { cacheFile, dryRun });
if (!result.success) {
process.stderr.write(`❌ IMF fallback ladder exhausted: ${result.error ?? 'unknown'}\n`);
process.exit(1);
}
process.stdout.write(
JSON.stringify({
status: 'ok',
rung: result.rung,
provenance: result.provenance,
outputFile: result.outputFile,
}) + '\n',
);
}
// Standard ESM CLI guard
const isMain =
typeof process !== 'undefined' &&
process.argv[1] !== undefined &&
(process.argv[1] === fileURLToPath(import.meta.url) ||
process.argv[1].endsWith('/imf-fallback-ladder.js'));
if (isMain) {
main().catch((err) => {
process.stderr.write(`Fatal: ${err}\n`);
process.exit(1);
});
}
/* c8 ignore stop */
|