Files
website/scripts/stats-fetch.ts

93 lines
3.5 KiB
TypeScript

// /stats collector. Pulls open statistics into the `stats` schema; every job is logged in
// stats.fetch_run. Run on the host by systemd user timers (deploy/stats-collector), with DB_URL
// read from the app container (app role personal_app).
//
// bun scripts/stats-fetch.ts cbs [--force] [--only=86141NED,85592NED]
// bun scripts/stats-fetch.ts eurostat [--only=nrg_pc_204]
// bun scripts/stats-fetch.ts fred [--only=DCOILBRENTEU]
// bun scripts/stats-fetch.ts bis
// bun scripts/stats-fetch.ts worldbank|imf|owid [--only=…]
// bun scripts/stats-fetch.ts energycharts|rijksfinancien|ustreasury [--backfill]
// bun scripts/stats-fetch.ts usaspending
// bun scripts/stats-fetch.ts eponline [--force] (needs EP_ONLINE_API_KEY)
// bun scripts/stats-fetch.ts cbscw [--force] [--only=cpi,gdp,…] CBS Curaçao files
// bun scripts/stats-fetch.ts cwintl [--only=ilostat,owid,unsdg,comtrade] Curaçao via international APIs
// bun scripts/stats-fetch.ts normalise-cbs rebuild CBS chart series from the raw rows
import { ensureMeta, sql } from './stats/common';
import { cbs, normaliseCbs } from './stats/cbs';
import { eurostat } from './stats/eurostat';
import { fred } from './stats/fred';
import { bis } from './stats/bis';
import { worldbank } from './stats/worldbank';
import { imf } from './stats/imf';
import { owid } from './stats/owid';
import { energycharts } from './stats/energycharts';
import { rijksfinancien } from './stats/rijksfinancien';
import { ustreasury } from './stats/ustreasury';
import { usaspending } from './stats/usaspending';
import { eponline } from './stats/eponline';
import { cwintl } from './stats/cwintl';
import { cwcbs } from './stats/cwcbs';
const [source, ...args] = process.argv.slice(2);
const flag = (name: string) => args.includes(`--${name}`);
const value = (name: string) => args.find((a) => a.startsWith(`--${name}=`))?.split('=')[1];
const job = flag('backfill') ? 'backfill' : value('only') ? `only:${value('only')}` : 'refresh';
try {
await ensureMeta();
switch (source) {
case 'cbs':
await cbs({ force: flag('force') || flag('backfill'), only: value('only'), job });
break;
case 'normalise-cbs':
console.log(`${await normaliseCbs()} CBS observations normalised`);
break;
case 'eurostat':
await eurostat({ only: value('only'), job });
break;
case 'fred':
await fred({ only: value('only'), job });
break;
case 'bis':
await bis({ job });
break;
case 'worldbank':
await worldbank({ only: value('only'), job });
break;
case 'imf':
await imf({ only: value('only'), job });
break;
case 'owid':
await owid({ only: value('only'), job });
break;
case 'energycharts':
await energycharts({ backfill: flag('backfill'), job });
break;
case 'rijksfinancien':
await rijksfinancien({ backfill: flag('backfill'), job });
break;
case 'ustreasury':
await ustreasury({ backfill: flag('backfill'), job });
break;
case 'usaspending':
await usaspending({ job });
break;
case 'cbscw':
await cwcbs({ force: flag('force') || flag('backfill'), only: value('only'), job });
break;
case 'cwintl':
await cwintl({ only: value('only'), job });
break;
case 'eponline':
await eponline({ force: flag('force') || flag('backfill'), job });
break;
default:
throw new Error(
`Unknown source "${source}" (cbs, eurostat, fred, bis, worldbank, imf, owid, energycharts, rijksfinancien, ustreasury, usaspending, eponline, cbscw, cwintl, normalise-cbs)`
);
}
} finally {
await sql.end({ timeout: 5 });
}