OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.
funnel: keep the lower steps when the breakdown limit is reached
Carl-Gerhard Lindesvärdb264f3ea1d8f
diff --git a/packages/core/src/modules/chart/funnel.service.test.ts b/packages/core/src/modules/chart/funnel.service.test.tsnew file mode 100644index 000000000..1b9a5621e--- /dev/null+++ b/packages/core/src/modules/chart/funnel.service.test.ts@@ -0,0 +1,93 @@+// Ported from packages/db/src/services/funnel.service.test.ts (main #497).+// toSeries is pure — it only reshapes rows — so nothing here touches a client.+import { describe, expect, it } from 'bun:test';+import { toSeries } from './funnel.service';++const BREAKDOWNS = [{ name: 'path' }];++/**+ * The funnel query is ordered by `level DESC`, so every level-5 row arrives+ * before any level-4 row, and so on. Rows are shaped as the query returns them.+ */+function rowsOrderedByLevelDesc(+ paths: string[],+ perLevel: Record<number, number>+) {+ return [5, 4, 3, 2, 1].flatMap((level) =>+ paths.map((path) => ({+ level,+ count: perLevel[level] as number,+ b_0: path,+ }))+ );+}++describe('toSeries', () => {+ it('keeps every level of a series once the limit is reached', () => {+ // 3 paths but a limit of 2: the third is dropped, the first two must still+ // carry all five of their levels.+ const rows = rowsOrderedByLevelDesc(['/a', '/b', '/c'], {+ 5: 128,+ 4: 22,+ 3: 1,+ 2: 105,+ 1: 67,+ });++ const series = toSeries(rows, BREAKDOWNS, 2);++ expect(series).toHaveLength(2);+ for (const one of series) {+ expect(one.map((row) => row.level).sort()).toEqual([1, 2, 3, 4, 5]);+ }+ });++ it('caps the number of series at the limit', () => {+ const rows = rowsOrderedByLevelDesc(['/a', '/b', '/c', '/d'], {+ 5: 1,+ 4: 1,+ 3: 1,+ 2: 1,+ 1: 1,+ });++ expect(toSeries(rows, BREAKDOWNS, 2)).toHaveLength(2);+ expect(toSeries(rows, BREAKDOWNS, 4)).toHaveLength(4);+ expect(toSeries(rows, BREAKDOWNS, undefined)).toHaveLength(4);+ });++ it('does not flatten a funnel into an all-100% series when limited', () => {+ // Regression: dropping the lower-level rows left each series holding only+ // its deepest level, which fillFunnel then accumulated into every step —+ // rendering an identical count at 100% for all steps.+ const rows = rowsOrderedByLevelDesc(['/a', '/b'], {+ 5: 128,+ 4: 22,+ 3: 1,+ 2: 105,+ 1: 67,+ });++ const [first] = toSeries(rows, BREAKDOWNS, 1);++ expect(first).toBeDefined();+ const counts = (first ?? [])+ .sort((a, b) => a.level - b.level)+ .map((row) => row.count);+ // 67, 105, 1, 22, 128 — distinct per level, not five copies of 128.+ expect(counts).toEqual([67, 105, 1, 22, 128]);+ expect(new Set(counts).size).toBeGreaterThan(1);+ });++ it('returns a single series when there are no breakdowns', () => {+ const rows = [+ { level: 2, count: 5 },+ { level: 1, count: 9 },+ ];++ const series = toSeries(rows, [], 1);++ expect(series).toHaveLength(1);+ expect(series[0]).toHaveLength(2);+ });+});diff --git a/packages/core/src/modules/chart/funnel.service.ts b/packages/core/src/modules/chart/funnel.service.tsindex 465d526eb..7206a5ff8 100644--- a/packages/core/src/modules/chart/funnel.service.ts+++ b/packages/core/src/modules/chart/funnel.service.ts@@ -185,14 +185,20 @@ export function toSeries(// Group by breakdown values (normalize empty/null to "Not set").const series = funnel.reduce((acc, row) => {- if (limit && Object.keys(acc).length >= limit) {- return acc;- }-const key = breakdowns.map((_, index) => normalizeBreakdownValue(row[`b_${index}`])).join('|');if (!acc[key]) {+ // The limit caps how many breakdown series come back, so it must only+ // reject NEW keys. Bailing out of the whole reduce dropped the+ // remaining rows of series already accepted: the query is ordered by+ // level DESC, so those rows are the lower funnel steps, and losing+ // them leaves each series holding only its deepest level. fillFunnel+ // then accumulates that one row into every step, so every step+ // reports an identical count at 100%.+ if (limit && Object.keys(acc).length >= limit) {+ return acc;+ }acc[key] = [];}(acc[key] as FunnelSerieRow[]).push({