45 lines
1.7 KiB
TypeScript
45 lines
1.7 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 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';
|
|
|
|
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;
|
|
default:
|
|
throw new Error(`Unknown source "${source}" (cbs, eurostat, fred, bis, normalise-cbs)`);
|
|
}
|
|
} finally {
|
|
await sql.end({ timeout: 5 });
|
|
}
|