mirror of
https://github.com/koala73/worldmonitor.git
synced 2026-04-25 17:14:57 +02:00
Greptile P1 review on the merged PR #3289: World Bank EG.ELC.RNEW.ZS explicitly excludes hydroelectric. The v2 lowCarbonGenerationShare composite was summing only nuclear + renew-ex-hydro, which would collapse to ~0 for hydro-dominant economies the moment the RESILIENCE_ENERGY_V2_ENABLED flag flipped: Norway ~95% hydro → score near 0 on a 0.20-weight indicator Paraguay ~99% hydro → same Brazil ~65% hydro → same Canada ~60% hydro → same Directly contradicts the plan §3.3 intent of crediting "firm low-carbon generation" and would produce rankings that contradict the power-system security framing. PR #3289 merged before the review landed. This branch applies the fix against main. Fix: add EG.ELC.HYRO.ZS as a third series in the composite. seed-low-carbon-generation.mjs: - INDICATORS: ['EG.ELC.NUCL.ZS', 'EG.ELC.RNEW.ZS'] + 'EG.ELC.HYRO.ZS' - fetchLowCarbonGeneration(): sum three series, track latest year across all three, same cap-at-100 guard - File header comment names the three-series sum with the hydro- exclusion rationale + the country list that would break. _indicator-registry.ts lowCarbonGenerationShare.description: rewritten to name all three WB codes + explain the hydro exclusion. country-resilience-index.mdx: - Known-limitations item 3 names all three WB codes + country list - Energy domain v2 table row names all three WB codes - v2.1 changelog Indicators-added bullet names all three WB codes - v2.1 changelog New-seeders bullet names all three WB codes on seed-low-carbon-generation No scorer code change (composite lives in the seeder; scorer reads the pre-summed value from resilience:low-carbon-generation:v1). No weight change. Flag-off path remains byte-identical. 25 resilience tests pass, typecheck + typecheck:api clean.
118 lines
4.7 KiB
JavaScript
118 lines
4.7 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
// PR 1 of the resilience repair plan (§3.3). Writes the per-country
|
|
// low-carbon share of electricity generation (nuclear + renewables
|
|
// + hydroelectric). Read by scoreEnergy v2 via
|
|
// `resilience:low-carbon-generation:v1`.
|
|
//
|
|
// Source: World Bank WDI. THREE indicators summed per country:
|
|
// - EG.ELC.NUCL.ZS: electricity production from nuclear (% of total)
|
|
// - EG.ELC.RNEW.ZS: electricity production from renewable sources
|
|
// EXCLUDING hydroelectric (% of total)
|
|
// - EG.ELC.HYRO.ZS: electricity production from hydroelectric
|
|
// sources (% of total)
|
|
//
|
|
// Hydro is included alongside RNEW because the WB RNEW series
|
|
// explicitly excludes hydroelectric — omitting HYRO would collapse
|
|
// this indicator to ~0 for Norway (~95% hydro), Paraguay (~99%),
|
|
// Brazil (~65%), Canada (~60%) and produce rankings that contradict
|
|
// the power-system security intent.
|
|
//
|
|
// All three series are annual; WDI reports latest observed year per
|
|
// country. We fetch the most-recent value (mrv=1) and sum by ISO2.
|
|
// Missing any of the three (e.g. a country with no nuclear filing)
|
|
// is treated as 0 for that slice — the scorer's 0..80 saturating
|
|
// goalpost tolerates partial coverage without dropping the indicator
|
|
// to null.
|
|
|
|
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:low-carbon-generation:v1';
|
|
const CACHE_TTL = 35 * 24 * 3600;
|
|
const INDICATORS = ['EG.ELC.NUCL.ZS', 'EG.ELC.RNEW.ZS', 'EG.ELC.HYRO.ZS'];
|
|
|
|
async function fetchIndicator(indicatorId) {
|
|
const pages = [];
|
|
let page = 1;
|
|
let totalPages = 1;
|
|
while (page <= totalPages) {
|
|
const url = `${WB_BASE}/country/all/indicator/${indicatorId}?format=json&per_page=500&page=${page}&mrv=1`;
|
|
const resp = await fetch(url, {
|
|
headers: { 'User-Agent': CHROME_UA },
|
|
signal: AbortSignal.timeout(30_000),
|
|
});
|
|
if (!resp.ok) throw new Error(`World Bank ${indicatorId}: HTTP ${resp.status}`);
|
|
const json = await resp.json();
|
|
totalPages = json[0]?.pages ?? 1;
|
|
pages.push(...(json[1] ?? []));
|
|
page++;
|
|
}
|
|
return pages;
|
|
}
|
|
|
|
function collectByIso2(records) {
|
|
const out = new Map();
|
|
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;
|
|
const year = Number(record?.date);
|
|
out.set(iso2, { value, year: Number.isFinite(year) ? year : null });
|
|
}
|
|
return out;
|
|
}
|
|
|
|
async function fetchLowCarbonGeneration() {
|
|
const [nuclearRecords, renewRecords, hydroRecords] = await Promise.all(INDICATORS.map(fetchIndicator));
|
|
const nuclearByIso = collectByIso2(nuclearRecords);
|
|
const renewByIso = collectByIso2(renewRecords);
|
|
const hydroByIso = collectByIso2(hydroRecords);
|
|
|
|
const allIso = new Set([...nuclearByIso.keys(), ...renewByIso.keys(), ...hydroByIso.keys()]);
|
|
const countries = {};
|
|
for (const iso2 of allIso) {
|
|
const nuc = nuclearByIso.get(iso2);
|
|
const ren = renewByIso.get(iso2);
|
|
const hyd = hydroByIso.get(iso2);
|
|
const sum = (nuc?.value ?? 0) + (ren?.value ?? 0) + (hyd?.value ?? 0);
|
|
// Year: most-recent of the three (they can diverge by a year or two
|
|
// between filings). Use the MAX so freshness reflects newest input.
|
|
const years = [nuc?.year, ren?.year, hyd?.year].filter((y) => y != null);
|
|
countries[iso2] = {
|
|
value: Math.min(sum, 100), // guard against impossible sums from revised filings
|
|
year: years.length > 0 ? Math.max(...years) : null,
|
|
};
|
|
}
|
|
return { countries, seededAt: new Date().toISOString() };
|
|
}
|
|
|
|
function validate(data) {
|
|
return typeof data?.countries === 'object' && Object.keys(data.countries).length >= 150;
|
|
}
|
|
|
|
export function declareRecords(data) {
|
|
return Object.keys(data?.countries || {}).length;
|
|
}
|
|
|
|
if (process.argv[1]?.endsWith('seed-low-carbon-generation.mjs')) {
|
|
runSeed('resilience', 'low-carbon-generation', CANONICAL_KEY, fetchLowCarbonGeneration, {
|
|
validateFn: validate,
|
|
ttlSeconds: CACHE_TTL,
|
|
sourceVersion: `wb-low-carbon-${new Date().getFullYear()}`,
|
|
recordCount: (data) => Object.keys(data?.countries ?? {}).length,
|
|
declareRecords,
|
|
schemaVersion: 1,
|
|
maxStaleMin: 8 * 24 * 60, // weekly cadence + 1 day slack
|
|
}).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);
|
|
});
|
|
}
|