From 3a61276859f3f0bd177920c7483a27d899e4dee0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carl-Gerhard=20Lindesva=CC=88rd?= Date: Tue, 4 Feb 2025 14:10:38 +0100 Subject: [PATCH] fix(buffer): better checksum for profile buffer --- .../db/src/buffers/profile-buffer-psql.ts | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/packages/db/src/buffers/profile-buffer-psql.ts b/packages/db/src/buffers/profile-buffer-psql.ts index 0ad130c0..6129d135 100644 --- a/packages/db/src/buffers/profile-buffer-psql.ts +++ b/packages/db/src/buffers/profile-buffer-psql.ts @@ -1,6 +1,6 @@ import { createHash } from 'node:crypto'; import { runEvery } from '@openpanel/redis'; -import { mergeDeepRight } from 'ramda'; +import { assocPath, dissocPath, mergeDeepRight } from 'ramda'; import { TABLE_NAMES, ch, chQuery } from '../clickhouse-client'; import { db } from '../prisma-client'; @@ -28,7 +28,7 @@ export class ProfileBuffer extends BaseBuffer { }); } - private sortObjectKeys(obj: any): any { + private sortObjectKeys(obj: any, exclude: string[][] = []): any { // Cache typeof check result const type = typeof obj; @@ -63,9 +63,27 @@ export class ProfileBuffer extends BaseBuffer { return result; } + private excludeKeys( + profile: IClickhouseProfile, + exclude: string[][], + ): IClickhouseProfile { + let filtered = profile; + for (const path of exclude) { + filtered = dissocPath(path, filtered); + } + return filtered; + } + private stringify(profile: IClickhouseProfile): string { - const { created_at, ...rest } = profile; - const sorted = this.sortObjectKeys(rest); + const exclude = [ + ['created_at'], + ['properties', 'path'], + ['properties', 'referrer'], + ['properties', 'referrer_name'], + ['properties', 'referrer_type'], + ]; + const excluded = this.excludeKeys(profile, exclude); + const sorted = this.sortObjectKeys(excluded); return JSON.stringify(sorted); }