Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

ralph(M15-135): The findings no rule covers: packages/core/src/buffers

ralph6d7916642474
diff --git a/packages/core/src/buffers/event-buffer.test.ts b/packages/core/src/buffers/event-buffer.test.ts
index 14a36874f..4c8052a21 100644
--- a/packages/core/src/buffers/event-buffer.test.ts
+++ b/packages/core/src/buffers/event-buffer.test.ts
@@ -1,5 +1,6 @@
import { afterAll, beforeEach, describe, expect, it, mock } from 'bun:test';
import type { Readable } from 'node:stream';
+import type { SqlFragment } from '@openpanel/db/src/clickhouse/sql';
import { getRedisCache } from '@openpanel/redis';
import { bufferDepsWithCh } from '../../test/buffer-fixtures';
import { testCoreConfig } from '../../test/config-fixture';
@@ -7,7 +8,9 @@ import { testCoreConfig } from '../../test/config-fixture';
const realChQuery = { ...(await import('../ch-query')) };
const chInsert = mock(async (_options: unknown): Promise<unknown> => undefined);
-const chQuery = mock(async (_sql: string): Promise<unknown[]> => []);
+const chQuery = mock(
+ async (_query: string | SqlFragment): Promise<unknown[]> => []
+);
// M10-009: the client comes in as `BufferDeps.ch` and reads go through core's
// own `chQuery` (ch-query.ts) — so the insert path needs no module mock
@@ -16,7 +19,7 @@ const chQuery = mock(async (_sql: string): Promise<unknown[]> => []);
// a pino transport worker thread per test file).
mock.module('../ch-query', () => ({
...realChQuery,
- chQuery: (_scope: unknown, sql: string) => chQuery(sql),
+ chQuery: (_scope: unknown, query: string | SqlFragment) => chQuery(query),
}));
const { EventBuffer, extractProjectId } = await import('./event-buffer');
@@ -263,7 +266,11 @@ describe('EventBuffer', () => {
const count = await eventBuffer.getActiveVisitorCount('p9');
expect(count).toBe(2);
expect(chQuery).toHaveBeenCalledTimes(1);
- expect(chQuery.mock.calls[0]![0]).toContain("project_id = 'p9'");
+ const { query, query_params } = (
+ chQuery.mock.calls[0]![0] as SqlFragment
+ ).toStatement();
+ expect(query).toContain('project_id = {p1:String}');
+ expect(query_params.p1).toBe('p9');
});
it('handles multiple sessions independently — all events go to buffer', async () => {
diff --git a/packages/core/src/buffers/event-buffer.ts b/packages/core/src/buffers/event-buffer.ts
index 7ee9bc260..379455967 100644
--- a/packages/core/src/buffers/event-buffer.ts
+++ b/packages/core/src/buffers/event-buffer.ts
@@ -1,3 +1,4 @@
+import { sql } from '@openpanel/db/src/clickhouse/sql';
import { getRedisCache, publishEvent } from '@openpanel/redis';
import type { IClickhouseEvent } from '../modules/event/event.service';
import { BaseBuffer, type BufferDeps } from './base-buffer';
@@ -257,13 +258,13 @@ export class EventBuffer extends BaseBuffer {
}
public async getActiveVisitorCount(projectId: string): Promise<number> {
- const rows = await this.chQuery<{ count: number }>(
- `SELECT uniq(profile_id) AS count
- FROM events
- WHERE project_id = '${projectId}'
- AND profile_id != ''
- AND created_at >= now() - INTERVAL 5 MINUTE`
- );
+ const rows = await this.chQuery<{ count: number }>(sql`
+ SELECT uniq(profile_id) AS count
+ FROM events
+ WHERE project_id = ${sql.string(projectId)}
+ AND profile_id != ''
+ AND created_at >= now() - INTERVAL 5 MINUTE
+ `);
return rows[0]?.count ?? 0;
}
}
diff --git a/packages/core/src/buffers/partial-json-match.ts b/packages/core/src/buffers/partial-json-match.ts
deleted file mode 100644
index 8be454410..000000000
--- a/packages/core/src/buffers/partial-json-match.ts
+++ /dev/null
@@ -1,39 +0,0 @@
-/**
- * Checks if an object partially matches another object, including nested properties
- * @param source The object to check against
- * @param partial The partial object to match
- * @returns boolean indicating if the partial object matches the source
- */
-export function isPartialMatch(source: any, partial: any): boolean {
- // Handle null/undefined cases
- if (partial === null || partial === undefined) {
- return source === partial;
- }
-
- // If partial is not an object, do direct comparison
- if (typeof partial !== 'object') {
- return source === partial;
- }
-
- // If source is null/undefined but partial is an object, no match
- if (source === null || source === undefined) {
- return false;
- }
-
- // Check each property in partial
- for (const key in partial) {
- if (Object.hasOwn(partial, key) && partial[key] !== undefined) {
- // If property doesn't exist in source, no match
- if (!(key in source)) {
- return false;
- }
-
- // Recursively check nested objects
- if (!isPartialMatch(source[key], partial[key])) {
- return false;
- }
- }
- }
-
- return true;
-}