OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.
improve(buffer): consistent loggic
Carl-Gerhard Lindesvärdb96af6c21b9e
diff --git a/packages/db/src/buffers/profile-buffer-redis.ts b/packages/db/src/buffers/profile-buffer-redis.tsindex 4fe64a8cd..50ca9d357 100644--- a/packages/db/src/buffers/profile-buffer-redis.ts+++ b/packages/db/src/buffers/profile-buffer-redis.ts@@ -1,4 +1,5 @@import { deepMergeObjects } from '@openpanel/common';+import type { ILogger } from '@openpanel/logger';// import { getSafeJson } from '@openpanel/json';import { type Redis, getRedisCache } from '@openpanel/redis';import shallowEqual from 'fast-deep-equal';@@ -53,13 +54,15 @@ export class ProfileBuffer extends BaseBuffer {}async add(profile: IClickhouseProfile) {+ const logger = this.logger.child({+ projectId: profile.project_id,+ profileId: profile.id,+ });+try {- this.logger.debug('Adding profile', {- projectId: profile.project_id,- profileId: profile.id,- });+ logger.debug('Adding profile');- const existingProfile = await this.fetchFromCache(profile);+ const existingProfile = await this.fetchFromCache(profile, logger);const mergedProfile: IClickhouseProfile = existingProfile? deepMergeObjects(existingProfile, profile)@@ -129,11 +132,8 @@ export class ProfileBuffer extends BaseBuffer {private async fetchFromCache(profile: IClickhouseProfile,+ logger: ILogger,): Promise<IClickhouseProfile | null> {- this.logger.debug('Fetching profile from Redis', {- projectId: profile.project_id,- profileId: profile.id,- });const cacheKey = this.getProfileCacheKey({profileId: profile.id,projectId: profile.project_id,@@ -143,24 +143,19 @@ export class ProfileBuffer extends BaseBuffer {if (existingProfile) {const parsedProfile = getSafeJson<IClickhouseProfile>(existingProfile);if (parsedProfile) {- this.logger.debug('Profile found in Redis', {- projectId: profile.project_id,- profileId: profile.id,- });+ logger.debug('Profile found in Redis');return parsedProfile;}}- return this.fetchFromClickhouse(profile);+ return this.fetchFromClickhouse(profile, logger);}private async fetchFromClickhouse(profile: IClickhouseProfile,+ logger: ILogger,): Promise<IClickhouseProfile | null> {- this.logger.debug('Fetching profile from Clickhouse', {- projectId: profile.project_id,- profileId: profile.id,- });+ logger.debug('Fetching profile from Clickhouse');const result = await chQuery<IClickhouseProfile>(`SELECT *FROM ${TABLE_NAMES.profiles}@@ -175,10 +170,8 @@ export class ProfileBuffer extends BaseBuffer {LIMIT 1`,);- this.logger.debug('Clickhouse fetch result', {+ logger.debug('Clickhouse fetch result', {found: !!result[0],- projectId: profile.project_id,- profileId: profile.id,});return result[0] || null;}