OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.
chart: count events in the picker, not materialized-view rows
Carl-Gerhard Lindesvärd048177e9c266
diff --git a/packages/core/src/modules/chart/chart.service.test.ts b/packages/core/src/modules/chart/chart.service.test.tsindex b4b3399d6..9d0ea04fc 100644--- a/packages/core/src/modules/chart/chart.service.test.ts+++ b/packages/core/src/modules/chart/chart.service.test.ts@@ -264,8 +264,8 @@ describe('picker queries', () => {it('lists event names with counts, `*` first', async () => {const events = await service.listChartEvents(TEST_PROJECT_ID);- // Counts come from distinct_event_names_mv (V1): one row per name and- // insert part, not one per event.+ // Counts are `sum(event_count)` over distinct_event_names_mv, so they are+ // real event totals. `count(name)` would count insert parts (ISSUES.md M1).expect(events[0]?.name).toBe('*');expect(events[0]?.count).toBe(events.slice(1).reduce((total, event) => total + event.count, 0)@@ -284,6 +284,34 @@ describe('picker queries', () => {]);});+ // The picker used to show `count(name)` over distinct_event_names_mv, which+ // counts one row per name PER INSERT PART, not per event (ISSUES.md M1). On+ // the large seed that showed 52 for an event with 974,387 occurrences. The+ // only assertion that catches it is one against the events table itself.+ it('counts real events, not materialized-view rows', async () => {+ const events = await service.listChartEvents(TEST_PROJECT_ID);+ const actual = await ch.query({+ query:+ 'SELECT name, count() AS count FROM events WHERE project_id = {projectId:String} GROUP BY name',+ query_params: { projectId: TEST_PROJECT_ID },+ format: 'JSONEachRow',+ });+ const truth = Object.fromEntries(+ (await actual.json<{ name: string; count: string }>()).map((row) => [+ row.name,+ Number(row.count),+ ])+ );+ const fromPicker = Object.fromEntries(+ events+ .filter((event) => event.name !== '*')+ .map((event) => [event.name, event.count])+ );++ expect(Object.keys(truth).length).toBeGreaterThan(0);+ expect(fromPicker).toEqual(truth);+ });+it('lists filterable properties, `name` only for the wildcard event', async () => {const all = await service.listChartProperties({projectId: TEST_PROJECT_ID,diff --git a/packages/core/src/modules/chart/src/sql.proof.md b/packages/core/src/modules/chart/src/sql.proof.mdindex 855d9e88f..fca0f4ba1 100644--- a/packages/core/src/modules/chart/src/sql.proof.md+++ b/packages/core/src/modules/chart/src/sql.proof.md@@ -611,16 +611,30 @@ SELECT uniqHLL12(if(created_at >= (now() - toIntervalMonth(3)), profile_id, null### eventNamesWithCountQuery — events (verdict)-**statement** — IDENTICAL; rows V1/V2 = 85/85; rows_read V1/V2 = 221184/221184; wall V1/V2 = 11 ms / 11 ms; clickhouse_settings: session_timezone=UTC (both).+**Amended 2026-09-27 (ISSUES.md M1): V2 now DIVERGES from V1 deliberately.**++The parity recorded below was real — both statements returned the same numbers because both+were wrong in the same way. `count(name)` counts rows of `distinct_event_names_mv`, and that+MV holds one row per name per insert part, so the report builder's event picker showed the+number of insert parts rather than the number of events. On the `large` seed acme-web showed+**52** for `screen_view`, which has **974,387** occurrences.++`sum(event_count)` reads the total the MV already stores. Verified against the seeded data:+it equals `SELECT count() FROM events GROUP BY name` exactly for every name (974387 / 659361 /+659361 / 95582 / 46334 / 34533 across the top six on acme-web).```sql--- V1+-- V1 (and V2 before this amendment) — counts MV rows, not eventsSELECT name, count(name) as count FROM distinct_event_names_mv WHERE project_id = 'verdict' GROUP BY name ORDER BY count DESC, name ASC--- V2-SELECT name, count(name) as count FROM distinct_event_names_mv WHERE project_id = {p1:String} GROUP BY name ORDER BY count DESC, name ASC+-- V2 now+SELECT name, sum(event_count) as count FROM distinct_event_names_mv WHERE project_id = {p1:String} GROUP BY name ORDER BY count DESC, name ASC-- V2 params: {"p1":"verdict"}```+Original record, kept for the paper trail:++**statement** — IDENTICAL; rows V1/V2 = 85/85; rows_read V1/V2 = 221184/221184; wall V1/V2 = 11 ms / 11 ms; clickhouse_settings: session_timezone=UTC (both).+### eventPropertyKeysQuery — properties (secure-privacy, event=undefined)**statement** — IDENTICAL; rows V1/V2 = 174/174; rows_read V1/V2 = 128264/128264; wall V1/V2 = 18 ms / 11 ms; clickhouse_settings: session_timezone=UTC (both).diff --git a/packages/core/src/modules/chart/src/sql.ts b/packages/core/src/modules/chart/src/sql.tsindex 0f5113c80..f1fa6ab65 100644--- a/packages/core/src/modules/chart/src/sql.ts+++ b/packages/core/src/modules/chart/src/sql.ts@@ -615,7 +615,9 @@ export function projectCardMetricsQuery(projectId: string): SqlFragment {}export function eventNamesWithCountQuery(projectId: string): SqlFragment {- return sql`SELECT name, count(name) as count FROM ${sql.id(CHART_TABLE.eventNamesMv)} WHERE project_id = ${sql.string(projectId)} GROUP BY name ORDER BY count DESC, name ASC`;+ // `count(name)` counts MV ROWS — one per name per insert part — not events.+ // The table carries the real total in `event_count`.+ return sql`SELECT name, sum(event_count) as count FROM ${sql.id(CHART_TABLE.eventNamesMv)} WHERE project_id = ${sql.string(projectId)} GROUP BY name ORDER BY count DESC, name ASC`;}function optionalEventName(event: string | undefined): SqlFragment {