import { pathToFileURL } from 'node:url'; import { monthRange, openReadOnlyStore } from './db.mjs'; export function readMetrics(args, env = process.env) { if (args.length !== 2 || args[0] !== '--month') throw new Error('Invalid arguments'); const month = args[1]; monthRange(month); const store = openReadOnlyStore(env.DB_PATH); try { return { month, monthlyVisitors: store.monthlyVisitors(month), arrivals: store.arrivalCount() }; } finally { store.close(); } } if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { try { process.stdout.write(`${JSON.stringify(readMetrics(process.argv.slice(2)))}\n`); } catch { process.stderr.write('Metrics unavailable; check --month YYYY-MM and existing DB_PATH\n'); process.exitCode = 1; } }