Skip to content

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

fix(session): negative duration #2

Carl-Gerhard Lindesvärd8d50213ed91e
diff --git a/packages/db/src/buffers/session-buffer.ts b/packages/db/src/buffers/session-buffer.ts
index 0fc221add..701ee81c8 100644
--- a/packages/db/src/buffers/session-buffer.ts
+++ b/packages/db/src/buffers/session-buffer.ts
@@ -191,14 +191,23 @@ export class SessionBuffer extends BaseBuffer {
if (events.length === 0) return;
- const sessions = events.map((e) => getSafeJson<IClickhouseSession>(e));
+ const sessions = events
+ .map((e) => getSafeJson<IClickhouseSession>(e))
+ .map((session) => {
+ return {
+ ...session,
+ duration: Math.max(0, session?.duration || 0),
+ };
+ });
- // Insert to ClickHouse
- await ch.insert({
- table: TABLE_NAMES.sessions,
- values: sessions,
- format: 'JSONEachRow',
- });
+ for (const chunk of this.chunks(sessions, 1000)) {
+ // Insert to ClickHouse
+ await ch.insert({
+ table: TABLE_NAMES.sessions,
+ values: chunk,
+ format: 'JSONEachRow',
+ });
+ }
// Only remove events after successful insert
await this.redis.ltrim(this.redisKey, events.length, -1);