/** * Scores the datakit checks on the national series this site publishes: plants * known kinds of mistake into them and counts what the checks catch * (src/lib/datakit/benchmark.ts). * * pnpm run datakit:benchmark [directory of /api/v1/statistics/data answers] * * Without a directory the series are read from the production data API. */ import { readdirSync, readFileSync } from "node:fs"; import { placesConfig } from "@/lib/config/places"; import { runBenchmark, type BenchmarkSeries } from "@bitbaum/datakit"; const SITE = "https://solon.orangecat.ch"; async function main() { const dir = process.argv[2]; const metrics = placesConfig.metrics.filter((m) => m.statistic && !m.retired); const series: BenchmarkSeries[] = []; for (const metric of metrics) { let body: { rows?: { name: string; year: number; published: number }[] }; if (dir) { const file = `${dir}/${metric.key}.json`; if (!readdirSync(dir).includes(`${metric.key}.json`)) continue; body = JSON.parse(readFileSync(file, "utf8")); } else { body = await (await fetch(`${SITE}/api/v1/statistics/data/${metric.key}`)).json(); } const byPlace = new Map(); for (const r of body.rows ?? []) { byPlace.set(r.name, [...(byPlace.get(r.name) ?? []), { period: r.year, value: r.published }]); } for (const [place, observations] of byPlace) { series.push({ id: `${metric.key} ${place}`, observations, context: { band: metric.plausible, fixedByLaw: metric.statistic?.fixedByLaw === true }, }); } } const score = runBenchmark(series); const pct = (a: number, b: number) => b === 0 ? " n/a" : `${((a / b) * 100).toFixed(1).padStart(5)} %`; console.log(`${series.length} series, ${score.clean.figures} true figures`); console.log(`true figures wrongly held: ${score.clean.held}; marked: ${score.clean.marked}`); console.log("\nplanted mistake planted held held or marked"); for (const [kind, t] of Object.entries(score.planted)) { console.log( `${kind.padEnd(18)} ${String(t.planted).padStart(8)} ${pct(t.held, t.planted)} ${pct(t.marked, t.planted)}`, ); } for (const [name, t] of [ ["at the newest figure", score.atNewest], ["inside the series", score.inside], ] as const) { console.log( `${name.padEnd(20)} ${String(t.planted).padStart(6)} ${pct(t.held, t.planted)} ${pct(t.marked, t.planted)}`, ); } } main().catch((error: unknown) => { console.error(error); process.exit(1); });