mirror of
https://github.com/koala73/worldmonitor.git
synced 2026-04-25 17:14:57 +02:00
* feat(resilience): recovery capacity pillar — 6 new dimensions + 5 seeders (Phase 2 T2.2b) Add the recovery-capacity pillar with 6 new dimensions: - fiscalSpace: IMF GGR_G01_GDP_PT + GGXCNL_G01_GDP_PT + GGXWDG_NGDP_PT - reserveAdequacy: World Bank FI.RES.TOTL.MO - externalDebtCoverage: WB DT.DOD.DSTC.CD / FI.RES.TOTL.CD ratio - importConcentration: UN Comtrade HHI (stub seeder) - stateContinuity: derived from WGI + UCDP + displacement (no new fetch) - fuelStockDays: IEA/EIA (stub seeder, Enrichment tier) Each dimension has a scorer in _dimension-scorers.ts, registry entries in _indicator-registry.ts, methodology doc subsections, and fixture data. Seeders: fiscal-space (real, IMF WEO), reserve-adequacy (real, WB API), external-debt (real, WB API), import-hhi (stub), fuel-stocks (stub). Recovery domain weight is 0 until PR 4 (T2.3) ships the penalized weighted mean across pillars. The domain appears in responses structurally but does not affect the overall score. Bootstrap: STANDALONE_KEYS + SEED_META + EMPTY_DATA_OK_KEYS + ON_DEMAND_KEYS all updated in api/health.js. Source-failure mapping updated for stateContinuity (WGI adapter). Widget labels and LOCKED_PREVIEW updated. All 282 resilience tests pass, typecheck clean, methodology lint clean. * fix(resilience): ISO3→ISO2 normalization in WB recovery seeders (#2987 P1) Both seed-recovery-reserve-adequacy.mjs and seed-recovery-external-debt.mjs used countryiso3code from the World Bank API response then immediately rejected codes where length !== 2. WB returns ISO3 codes (USA, DEU, etc.), so all real rows were silently dropped and the feed was always empty. Fix: import scripts/shared/iso3-to-iso2.json and normalize before the length check. Also removed from EMPTY_DATA_OK_KEYS in health.js since empty results now indicate a real failure, not a structural absence. * fix(resilience): remove unused import + no-op overrides (#2987 review) * fix(test): update release-gate to expect 6 domains after recovery pillar
83 lines
2.8 KiB
JavaScript
83 lines
2.8 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
import { loadEnvFile, CHROME_UA, runSeed } from './_seed-utils.mjs';
|
|
import iso3ToIso2 from './shared/iso3-to-iso2.json' with { type: 'json' };
|
|
|
|
loadEnvFile(import.meta.url);
|
|
|
|
const WB_BASE = 'https://api.worldbank.org/v2';
|
|
const CANONICAL_KEY = 'resilience:recovery:external-debt:v1';
|
|
const CACHE_TTL = 35 * 24 * 3600;
|
|
|
|
const DEBT_INDICATOR = 'DT.DOD.DSTC.CD';
|
|
const RESERVES_INDICATOR = 'FI.RES.TOTL.CD';
|
|
|
|
async function fetchWbIndicator(indicator) {
|
|
const out = {};
|
|
let page = 1;
|
|
let totalPages = 1;
|
|
|
|
while (page <= totalPages) {
|
|
const url = `${WB_BASE}/country/all/indicator/${indicator}?format=json&per_page=500&page=${page}&mrnev=1`;
|
|
const resp = await fetch(url, {
|
|
headers: { 'User-Agent': CHROME_UA },
|
|
signal: AbortSignal.timeout(30_000),
|
|
});
|
|
if (!resp.ok) throw new Error(`World Bank ${indicator}: HTTP ${resp.status}`);
|
|
const json = await resp.json();
|
|
const meta = json[0];
|
|
const records = json[1] ?? [];
|
|
totalPages = meta?.pages ?? 1;
|
|
for (const record of records) {
|
|
const rawCode = record?.countryiso3code ?? record?.country?.id ?? '';
|
|
const iso2 = rawCode.length === 3 ? (iso3ToIso2[rawCode] ?? null) : (rawCode.length === 2 ? rawCode : null);
|
|
if (!iso2) continue;
|
|
const value = Number(record?.value);
|
|
if (!Number.isFinite(value)) continue;
|
|
out[iso2] = { value, year: Number(record?.date) || null };
|
|
}
|
|
page++;
|
|
}
|
|
return out;
|
|
}
|
|
|
|
async function fetchExternalDebt() {
|
|
const [debtMap, reservesMap] = await Promise.all([
|
|
fetchWbIndicator(DEBT_INDICATOR),
|
|
fetchWbIndicator(RESERVES_INDICATOR),
|
|
]);
|
|
|
|
const countries = {};
|
|
const allCodes = new Set([...Object.keys(debtMap), ...Object.keys(reservesMap)]);
|
|
|
|
for (const code of allCodes) {
|
|
const debt = debtMap[code];
|
|
const reserves = reservesMap[code];
|
|
if (!debt || !reserves || reserves.value <= 0) continue;
|
|
|
|
countries[code] = {
|
|
debtToReservesRatio: Math.round((debt.value / reserves.value) * 1000) / 1000,
|
|
year: debt.year ?? reserves.year ?? null,
|
|
};
|
|
}
|
|
|
|
return { countries, seededAt: new Date().toISOString() };
|
|
}
|
|
|
|
function validate(data) {
|
|
return typeof data?.countries === 'object' && Object.keys(data.countries).length >= 80;
|
|
}
|
|
|
|
if (process.argv[1]?.endsWith('seed-recovery-external-debt.mjs')) {
|
|
runSeed('resilience', 'recovery:external-debt', CANONICAL_KEY, fetchExternalDebt, {
|
|
validateFn: validate,
|
|
ttlSeconds: CACHE_TTL,
|
|
sourceVersion: `wb-debt-reserves-${new Date().getFullYear()}`,
|
|
recordCount: (data) => Object.keys(data?.countries ?? {}).length,
|
|
}).catch((err) => {
|
|
const _cause = err.cause ? ` (cause: ${err.cause.message || err.cause.code || err.cause})` : '';
|
|
console.error('FATAL:', (err.message || err) + _cause);
|
|
process.exit(1);
|
|
});
|
|
}
|