Skill v1.0.0
currentLLM-judged scan90/100version: "1.0.0" name: obh-retrofit-analytics description: Use to assess or replace hand-rolled KPI/metric code with @obh/analytics. Finds live count(*)/group-by aggregation endpoints, rollup cron and tables, and third-party tracking (Mixpanel/Amplitude/GA) calls, then maps each KPI to an event-derived metric timeseries. Assessment-only by default; implements on request.
Purpose: derive KPIs from domain events instead of aggregating product tables on the fly. Each metric is defined once (event + aggregation); the worker rolls facts into time buckets; the API serves a queryable timeseries. Requires an event catalogue (see obh-add-events) — metrics are computed from event facts, not the product tables.
Assessment (read-only)
- Find the metric code. Grep for: live aggregation in request handlers (
count(*),sum(...),group by date_trunc(...)); rollup/summary tables and the cron that fills them; dashboard/KPI endpoints; and third-party tracking (mixpanel,amplitude,analytics.track,gtag).
- Inventory each KPI. For each: what it measures, the aggregation (count / sum-of-a-field / …), the bucket granularity (day/week/…), and which state change it counts. Flag expensive live aggregations and drift-prone rollup tables as the risks this move fixes.
- Map each KPI to a `defineMetric`. Give each a key and bind it to the event(s) it derives from:
defineMetric({ key: "notes_created", events: "note.created", aggregation: "count" })(or a sum over a payload field). If the counted state change has no event, record that obh-add-events must run first.
- Map query to the target grammar. The KPI endpoint becomes
analytics.timeseries({ workspaceId, metric, bucket })overcreateAnalyticsClient({ db, metrics }). Note that analytics rolls up events from now on, so historical KPIs need an event backfill/replay.
Produces the analytics retrofit plan. Nothing above mutates the repo — a valid stopping point when you only need the survey.
Implementation (only after the plan is agreed)
- Install.
forge add analytics(--dry-runfirst; auto-addsevents). Adds the migration, the client +GET /api/analytics/:metricroute inapps/api, and the worker side (analytics-metrics.ts,dispatch.d/analytics.tswithevents: ["*"],consumers.d/analytics.tsrolling facts into buckets each tick). Runpnpm migrate.
- Author the metrics. Replace the example
defineMetricwith the KPIs from the plan, keeping the api and worker copies in sync (the worker never imports fromapps/api). Confirm the source events are emitted (obh-add-events).
- Backfill and cut over. For KPIs that need history, replay the source events so past buckets fill. Point dashboards at the timeseries endpoint, then retire the live-aggregation queries and rollup cron/tables once the timeseries covers them.
- Validate.
forge doctor.
Output
Assessment → the analytics retrofit plan: a table of KPI → metric key → source event(s) → aggregation → bucket → replacement (timeseries), the events that must exist first, and a historical-backfill note. Implementation → the installed client/route/worker with authored metrics, the backfilled buckets, and the retired live-aggregation/rollup code.