OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.
ralph(M39-002): The six sign-weighted aggregates: drop FINAL, change nothing else
ralph6ced689b444a
diff --git a/.m39-full.log b/.m39-full.lognew file mode 100644index 000000000..29b544556--- /dev/null+++ b/.m39-full.log@@ -0,0 +1,14 @@+OK: no forbidden patterns+== bun install --frozen-lockfile (/home/deploy/openpanel) ==+[0.05ms] ".env"+bun install v1.4.0 (34cbb9a40)++Checked 3036 installs across 3405 packages (no changes) [133.00ms]+OK: bun install+== build api: skipped (V1 fastify boot deleted; apps/api runs from source) ==+== typecheck api (/home/deploy/openpanel/apps/api) ==+$ tsc --noEmit+OK: typecheck api+== typecheck start (/home/deploy/openpanel/apps/start) ==+$ tsc --noEmit+error: script "typecheck" was terminated by signal SIGTERM (Polite quit request)diff --git a/packages/core/src/modules/insight/src/referrer-spikes.ts b/packages/core/src/modules/insight/src/referrer-spikes.tsindex 7e7d12658..46835a02d 100644--- a/packages/core/src/modules/insight/src/referrer-spikes.ts+++ b/packages/core/src/modules/insight/src/referrer-spikes.ts@@ -132,7 +132,7 @@ export async function getReferrerSpikes(total: number;}>(deps,- sql`SELECT referrer_name, sum(sign) AS total FROM ${sessionsTable} FINAL WHERE ${window} AND referrer_name != '' AND referrer_name IS NOT NULL${filtersWhere} GROUP BY referrer_name HAVING sum(sign) >= ${sql.uint64(MIN_SESSIONS_FLOOR)} ORDER BY total DESC LIMIT ${sql.uint64(MAX_REFERRERS)}`,+ sql`SELECT referrer_name, sum(sign) AS total FROM ${sessionsTable} WHERE ${window} AND referrer_name != '' AND referrer_name IS NOT NULL${filtersWhere} GROUP BY referrer_name HAVING sum(sign) >= ${sql.uint64(MIN_SESSIONS_FLOOR)} ORDER BY total DESC LIMIT ${sql.uint64(MAX_REFERRERS)}`,settings);@@ -147,12 +147,12 @@ export async function getReferrerSpikes(const [spikeRowsRaw, bucketTotalRowsRaw] = await Promise.all([chQuery<{ date: string; referrer_name: string; sessions: number }>(deps,- sql`SELECT ${bucket} AS date, referrer_name, sum(sign) AS sessions FROM ${sessionsTable} FINAL WHERE ${window} AND referrer_name IN ${sql.array('String', referrerNames)}${filtersWhere} GROUP BY date, referrer_name HAVING sum(sign) > 0 ORDER BY date ASC`,+ sql`SELECT ${bucket} AS date, referrer_name, sum(sign) AS sessions FROM ${sessionsTable} WHERE ${window} AND referrer_name IN ${sql.array('String', referrerNames)}${filtersWhere} GROUP BY date, referrer_name HAVING sum(sign) > 0 ORDER BY date ASC`,settings),chQuery<{ date: string; total: number }>(deps,- sql`SELECT ${bucket} AS date, sum(sign) AS total FROM ${sessionsTable} FINAL WHERE ${window}${filtersWhere} GROUP BY date HAVING sum(sign) > 0`,+ sql`SELECT ${bucket} AS date, sum(sign) AS total FROM ${sessionsTable} WHERE ${window}${filtersWhere} GROUP BY date HAVING sum(sign) > 0`,settings),]);diff --git a/packages/core/src/modules/overview/src/overview.sql.proof.md b/packages/core/src/modules/overview/src/overview.sql.proof.mdindex 3a7ca0dbd..90c39cc4a 100644--- a/packages/core/src/modules/overview/src/overview.sql.proof.md+++ b/packages/core/src/modules/overview/src/overview.sql.proof.md@@ -80,6 +80,33 @@131,039 rows**. Its row in the per-query table further down is M7-005's original measurementand is left as the historical record.+- **Amended by M39-002 (2026-09-16): `FINAL` dropped from the sign-weighted `sessions` aggregates**+ `topEntryExitQuery`, `topGenericQuery` and `topGenericSeriesTimeSeriesQuery` (plus the three+ `insight/src/referrer-spikes.ts` statements). Every output column is a grouping key or a sum that is+ linear in `sign`. A collapsible pair is `(+1, V)` and `(-1, V)` with identical columns+ (`session-buffer.ts` builds the `-1` as `{ ...existing, sign: -1 }`), so it sums to 0 in the same+ group, under the same filter, and `HAVING sum(sign) > 0` sees the same sums. **`bounceRate`+ (`countIf(sign = 1)`) and `pages.sql.ts` are not linear and keep `FINAL`.**+ Proof, run 2026-09-16 21:48–21:52 UTC on the local prod copy, read-only, `wait_end_of_query=1`,+ `use_query_cache=0`, `use_query_condition_cache=0`. Statements were rendered by the real builders+ (the referrer-spike ones were captured from `getReferrerSpikes` through a recording `deps.ch`), and+ the old form is the same text with ` FINAL` put back. Result-set hash `count(), sum(sipHash64(formatRow('TSV', *)))`:+ **312 of 312 statements identical.** The anchors were `chatpaper`, `verdict`, `bayse`,+ `earlysalary-production` and `website-8103` over 2026-08-01..08-30, plus `e2e-sessions` over+ 2026-09-01..09-15, which holds the box's 21 unmerged `-1` rows. The cases covered entry/exit,+ six `topGeneric` columns (with and without a prefix), four series intervals, no filter, a+ `country` filter and a page filter (the `distinct_sessions` CTE), and spikes at+ hour/day/week, UTC and `Europe/Stockholm`. The spike cases include a forced step 1, because every+ `e2e-sessions` referrer is `''`. A non-linear control (`count()` without `FINAL`) **did** differ on+ `e2e-sessions`, which shows the probe can detect the unmerged rows. Row *order* differs only+ within ties: the sort-key column sequence (`sessions` / `date` / `total`) was identical in every+ ordered case.+ Cost, warm, 3 runs, shipped → no `FINAL`: `chatpaper` `topGenericQuery` 169–182 → 53–57 ms,+ `topEntryExitQuery` 610–635 → 376–381 ms, series (day) 191–195 → 76–80 ms, spikes steps 1/2/3+ 139–141 / 139–140 / 128–136 → 41–43 / 43–45 / 41–43 ms (3,571,254 → 3,448,379 rows read);+ `verdict` `topGenericQuery` 47–51 → 18 ms, `topEntryExitQuery` 61–86 → 26–30 ms, series+ 52–56 → 24 ms.+- **Date**: 2026-09-04. **Data**: local prod-copy `openpanel` (319,499,000 events, static).Project: `skills-directory` (293,597 events, 75,727 sessions, 2026-07-01..2026-08-25).Window used throughout: `2026-08-01 00:00:00`..`2026-08-08 00:00:00`, UTC.diff --git a/packages/core/src/modules/overview/src/overview.sql.ts b/packages/core/src/modules/overview/src/overview.sql.tsindex 6ab65dd3b..36fe7964b 100644--- a/packages/core/src/modules/overview/src/overview.sql.ts+++ b/packages/core/src/modules/overview/src/overview.sql.ts@@ -422,7 +422,7 @@ export function topEntryExitQuery(input: TopEntryExitQueryInput): SqlFragment {sum(sign) as sessions,sum(sign * screen_view_count) as pageviews,sum(revenue * sign) as revenue- FROM sessions FINAL+ FROM sessionsWHERE project_id = ${sql.string(input.projectId)}AND ${dateRangeWhere('created_at', input.startDate, input.endDate)}${@@ -507,7 +507,7 @@ export function topGenericQuery(input: TopGenericQueryInput): SqlFragment {return sql`${distinctSessionsCteHeader(input.distinctSessionsCte)}SELECT ${topGenericSelect(input.column, input.prefixColumn)}- FROM sessions FINAL+ FROM sessionsWHERE project_id = ${sql.string(input.projectId)}AND ${dateRangeWhere('created_at', input.startDate, input.endDate)}${@@ -552,7 +552,7 @@ export function topGenericSeriesTimeSeriesQuery(SELECT${toStartOf('created_at', input.interval)} AS date,${topGenericSelect(input.column, input.prefixColumn)}- FROM sessions FINAL+ FROM sessionsWHERE project_id = ${sql.string(input.projectId)}AND ${dateRangeWhere('created_at', input.startDate, input.endDate)}${rawWhere(input.rawFilterWhere)}