50 lines
6.5 KiB
Markdown

# A2 open-source record: insights in the console
Package A2 of `docs/analytics/PLAN.md` (section 9.2). Notice file: `apps/product-analytics/src/insights/NOTICE`.
## Copied unmodified
| Source | Licence | Where it is now | Check |
|---|---|---|---|
| Umami v3.4.0 Postgres SQL, read from this repository's fork `apps/insights/src/queries/sql/{funnels/getFunnel.ts, retention/getRetention.ts, journeys/getJourney.ts, events/getEventMetrics.ts}` (never edited there) | MIT, Copyright (c) 2022 Umami Software, Inc. | `apps/product-analytics/src/insights/ported/umami/*.ts.upstream` with `LICENSE` (the `.upstream` suffix keeps them out of the type check; they import Umami's own modules) | sha256 of the sources at copy time: getFunnel `cb8d4639...`, getRetention `f22bb016...`, getJourney `eaffcc16...`, getEventMetrics `988182f4...` |
| node-sql-parser 5.4.0, `build/postgresql.js` and `LICENSE` only (the npm package is 92 MB; this build is 308 KB) | Apache-2.0 | `apps/product-analytics/src/insights/vendor/node-sql-parser/` | sha256 `64e4a66876d510d233dd109d56a6a0ea937c9e601c64a9870ef89682026479d0`; loaded by `sandbox-parser.ts` with a `require` limited to its one dependency |
| big-integer 1.6.52 (`BigInteger.js`, `LICENSE`), the one runtime dependency of that build | The Unlicense (public domain) | `apps/product-analytics/src/insights/vendor/big-integer/` | sha256 `9157657de3b1e92a2b75a8bc2e2dbecde1cdd0db414bb729c165528962ab7647` |
Why vendored and not installed: the plan's disk rule is "stay above 10 GB free"; the Mac had 6.7 GiB free when A2 started, so no package was installed. The two vendored files add 360 KB.
## Ported (semantics and tests re-expressed in our code)
Source: PostHog/posthog, MIT (Expat) outside `ee/`, commit `cbb1e9253027`, read with `gh api` (nothing cloned).
| Upstream | Our file | What was taken |
|---|---|---|
| `products/product_analytics/backend/hogql_queries/trends/*` | `src/insights/trends.ts`, `formula.ts` | Math kinds (total, unique people per interval, weekly and monthly active over trailing 7 and 30 days, unique groups, sum/avg/min/max/median/percentiles), breakdown with top values and an other bucket, formula over series letters (division by zero is 0), compare to the previous period, buckets aligned to the interval start |
| `.../funnels/*` | `src/insights/funnels.ts` | Ordered, strict and unordered steps; conversion window from the first step; first-touch breakdown; people counted at their furthest step; time between steps; drop-off people |
| `.../retention/*` | `src/insights/retention.ts` | First-time vs recurring cohorts; period 0 is the cohort size; returns counted in later periods |
| `.../paths/paths_query_runner.py` | `src/insights/paths.ts` | 30-minute session split, repeats collapsed, start point, end point, step limit, `i_item -> (i+1)_item` edges with average time |
| `.../stickiness/stickiness_query_runner.py` | `src/insights/lifecycle.ts` | People by number of active intervals |
| `.../lifecycle/lifecycle_query_runner.py` | `src/insights/lifecycle.ts` | new / returning / resurrecting / dormant rules (first period equals the created period = new; previous period active and not first = returning; otherwise resurrecting; the period after an active one without activity = dormant, negative); events from one period before to one after the range; people without a profile left out |
| `posthog/models/event/event.py` (`Selector`, `SelectorPart`), `posthog/models/property/util.py` (`build_selector_regex`, `_chain_escaped_value`), `posthog/hogql/property.py` (`steps_to_expr`, `selector_to_expr`, `tag_name_to_expr`), `posthog/models/event/sql.py` (`elements_chain_href`, `elements_chain_texts` patterns) | `src/insights/elements.ts`, `filters.ts` (`stepSql`) | CSS selector parsing (classes split before ids, attribute and `nth-child` parts, `>` direct descendants), the chain regex, tag, text and link matching, OR across action steps |
Deliberate differences, each tested:
- The funnel window is measured from the first step (the vendored Umami SQL measured it from the previous step).
- A per-person event sequence number keeps one event from filling two steps.
- Funnel attempts that a later attempt always beats are pruned before the joins, and ties go to the latest attempt (upstream does not document its tie-break; counts are unchanged).
- Postgres picks regex greediness per expression, so the chain's text and link are extracted with `"([^"]*)"` instead of `"(.*?)"` (same stopping point).
- Weeks start on Monday.
- A missing breakdown value always keeps its own "(none)" line; the top-N limit applies to present values.
Test cases re-expressed from upstream behaviour as exact fixtures: `test/a2/funnels.test.ts` (several tries, window from the first step, strict, unordered, repeated step), `test/a2/grids.test.ts` (lifecycle four states on exact days, stickiness, day retention first time and recurring, paths with the 30-minute split, start and end points).
## Written here (glue)
Query validation (`validate.ts`), the runner, cohorts recompute and CSV import, the result cache, saved insights and dashboards, rollups, the SQL sandbox runner and its roles and views (`db/migrations/0011_sql_sandbox.sql`), the console API, the console screens and the SVG charts (no chart library: the console's manifest is not ours, and the charts are small).
## Hand-offs (outside A2's paths; not done)
1. `apps/console/app/(app)/product-analytics/_components/Shell.tsx` (A1): add `{ href: "/product-analytics/insights", label: "Insights" }` to `SCREENS`, so the insights screens are one click from the product analytics frame. Until then they are reached at `/product-analytics/insights` and link to each other.
2. `apps/product-analytics/src/app.ts` (A1): add `...insightsTasks(deps)` (from `src/insights/jobs.ts`) to the service scheduler. Until then `insightsRoutes` starts its own scheduler in the server process (not under `node --test`, not with `PA_INSIGHTS_JOBS=off`).
3. `db/migrations/0002_events.sql` (A1): `events_project_event_ts_idx` is made redundant by A2's covering index `events_project_event_ts_person_idx`; dropping it saves index bytes per event (see `CAPACITY.md`).
4. `policy/copy-denylist.json` (shared lane): rule `railyard-slot-replay` treats "cohorts" as unlaunched, so the console and the API say "segments". When the owner turns on `RAILYARD_REPLAY_LIVE` or narrows the rule, the wording can change back.
5. Production: the SQL screen needs the migration owner to be allowed to `alter role pa_sql_login ... password` (CREATEROLE, already needed by migration 0001) and `pg_hba.conf` to allow password logins for `pa_sql_login` from the service host.