improve(buffer): consistent loggic

This commit is contained in:
Carl-Gerhard Lindesvärd
2025-02-22 10:00:16 +01:00
parent 2022a82f03
commit b96af6c21b

View File

@@ -1,4 +1,5 @@
import { deepMergeObjects } from '@openpanel/common'; import { deepMergeObjects } from '@openpanel/common';
import type { ILogger } from '@openpanel/logger';
// import { getSafeJson } from '@openpanel/json'; // import { getSafeJson } from '@openpanel/json';
import { type Redis, getRedisCache } from '@openpanel/redis'; import { type Redis, getRedisCache } from '@openpanel/redis';
import shallowEqual from 'fast-deep-equal'; import shallowEqual from 'fast-deep-equal';
@@ -53,13 +54,15 @@ export class ProfileBuffer extends BaseBuffer {
} }
async add(profile: IClickhouseProfile) { async add(profile: IClickhouseProfile) {
try { const logger = this.logger.child({
this.logger.debug('Adding profile', { projectId: profile.project_id,
projectId: profile.project_id, profileId: profile.id,
profileId: profile.id, });
});
const existingProfile = await this.fetchFromCache(profile); try {
logger.debug('Adding profile');
const existingProfile = await this.fetchFromCache(profile, logger);
const mergedProfile: IClickhouseProfile = existingProfile const mergedProfile: IClickhouseProfile = existingProfile
? deepMergeObjects(existingProfile, profile) ? deepMergeObjects(existingProfile, profile)
@@ -129,11 +132,8 @@ export class ProfileBuffer extends BaseBuffer {
private async fetchFromCache( private async fetchFromCache(
profile: IClickhouseProfile, profile: IClickhouseProfile,
logger: ILogger,
): Promise<IClickhouseProfile | null> { ): Promise<IClickhouseProfile | null> {
this.logger.debug('Fetching profile from Redis', {
projectId: profile.project_id,
profileId: profile.id,
});
const cacheKey = this.getProfileCacheKey({ const cacheKey = this.getProfileCacheKey({
profileId: profile.id, profileId: profile.id,
projectId: profile.project_id, projectId: profile.project_id,
@@ -143,24 +143,19 @@ export class ProfileBuffer extends BaseBuffer {
if (existingProfile) { if (existingProfile) {
const parsedProfile = getSafeJson<IClickhouseProfile>(existingProfile); const parsedProfile = getSafeJson<IClickhouseProfile>(existingProfile);
if (parsedProfile) { if (parsedProfile) {
this.logger.debug('Profile found in Redis', { logger.debug('Profile found in Redis');
projectId: profile.project_id,
profileId: profile.id,
});
return parsedProfile; return parsedProfile;
} }
} }
return this.fetchFromClickhouse(profile); return this.fetchFromClickhouse(profile, logger);
} }
private async fetchFromClickhouse( private async fetchFromClickhouse(
profile: IClickhouseProfile, profile: IClickhouseProfile,
logger: ILogger,
): Promise<IClickhouseProfile | null> { ): Promise<IClickhouseProfile | null> {
this.logger.debug('Fetching profile from Clickhouse', { logger.debug('Fetching profile from Clickhouse');
projectId: profile.project_id,
profileId: profile.id,
});
const result = await chQuery<IClickhouseProfile>( const result = await chQuery<IClickhouseProfile>(
`SELECT * `SELECT *
FROM ${TABLE_NAMES.profiles} FROM ${TABLE_NAMES.profiles}
@@ -175,10 +170,8 @@ export class ProfileBuffer extends BaseBuffer {
LIMIT 1`, LIMIT 1`,
); );
this.logger.debug('Clickhouse fetch result', { logger.debug('Clickhouse fetch result', {
found: !!result[0], found: !!result[0],
projectId: profile.project_id,
profileId: profile.id,
}); });
return result[0] || null; return result[0] || null;
} }