OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.
fix(db): return the newest events, not an arbitrary slice (#476)
Avionic Harshit3060ca102136
diff --git a/packages/db/src/services/event.service.ts b/packages/db/src/services/event.service.tsindex 7116b8817..1e7301290 100644--- a/packages/db/src/services/event.service.ts+++ b/packages/db/src/services/event.service.ts@@ -1346,9 +1346,7 @@ export interface QueryEventsInput {limit?: number;}-export async function queryEventsCore(- input: QueryEventsInput-): Promise<IClickhouseEvent[]> {+export function buildQueryEventsQuery(input: QueryEventsInput) {const builder = clix(ch).select<IClickhouseEvent>([]).from(TABLE_NAMES.events)@@ -1449,5 +1447,13 @@ export async function queryEventsCore(}}- return builder.limit(input.limit ?? 20).execute();+ // Without an explicit order ClickHouse returns whatever it reads first, so a+ // bare LIMIT hands back an arbitrary slice of the window, not the newest.+ return builder.orderBy('created_at', 'DESC').limit(input.limit ?? 20);+}++export async function queryEventsCore(+ input: QueryEventsInput+): Promise<IClickhouseEvent[]> {+ return buildQueryEventsQuery(input).execute();}diff --git a/packages/db/src/services/query-events-sql.test.ts b/packages/db/src/services/query-events-sql.test.tsnew file mode 100644index 000000000..b1120367e--- /dev/null+++ b/packages/db/src/services/query-events-sql.test.ts@@ -0,0 +1,72 @@+/**+ * SQL-shape tests for the shared event query.+ *+ * Same strategy as profile-metrics-sql.test.ts: string assertions always run;+ * `EXPLAIN` validation runs against a locally reachable ClickHouse+ * (`pnpm dock:up`) and skips otherwise.+ */+import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';++import { ch } from '../clickhouse/client';+import { buildQueryEventsQuery } from './event.service';++const PROJECT_ID = 'test-sql-validation';++let chReachable = false;++beforeAll(async () => {+ vi.spyOn(console, 'log').mockImplementation(() => {});+ try {+ await ch.command({ query: 'SELECT 1' });+ chReachable = true;+ } catch {+ chReachable = false;+ }+});++afterAll(() => {+ vi.restoreAllMocks();+});++const itCH = (name: string, fn: () => Promise<void>) =>+ it(name, async (ctx) => {+ if (!chReachable) {+ ctx.skip('ClickHouse not reachable at CLICKHOUSE_URL');+ }+ await fn();+ });++describe('buildQueryEventsQuery', () => {+ // Callers label these rows `created_at desc` and call them recent events, so+ // the cut has to happen after the sort rather than wherever the scan starts.+ it('takes the newest rows, not an arbitrary slice', () => {+ const sql = buildQueryEventsQuery({ projectId: PROJECT_ID }).toSQL();+ expect(sql).toContain('ORDER BY created_at DESC');+ expect(sql.indexOf('ORDER BY')).toBeLessThan(sql.indexOf('LIMIT'));+ });++ it('keeps the default and the caller limit', () => {+ expect(buildQueryEventsQuery({ projectId: PROJECT_ID }).toSQL()).toContain(+ 'LIMIT 20',+ );+ expect(+ buildQueryEventsQuery({ projectId: PROJECT_ID, limit: 100 }).toSQL(),+ ).toContain('LIMIT 100');+ });++ it('still applies the filters', () => {+ const sql = buildQueryEventsQuery({+ projectId: PROJECT_ID,+ profileId: 'profile-1',+ eventNames: ['session_start'],+ }).toSQL();+ expect(sql).toContain('profile_id');+ expect(sql).toContain('session_start');+ });++ itCH('parses and resolves against ClickHouse', async () => {+ await ch.command({+ query: `EXPLAIN ${buildQueryEventsQuery({ projectId: PROJECT_ID }).toSQL()}`,+ });+ });+});