fix: how we fetch profiles in the buffer

This commit is contained in:
Carl-Gerhard Lindesvärd
2026-02-06 12:41:41 +00:00
parent fc3b6fb891
commit ed8b5c667e

View File

@@ -4,6 +4,7 @@ import type { ILogger } from '@openpanel/logger';
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';
import { omit } from 'ramda'; import { omit } from 'ramda';
import sqlstring from 'sqlstring';
import { TABLE_NAMES, ch, chQuery } from '../clickhouse/client'; import { TABLE_NAMES, ch, chQuery } from '../clickhouse/client';
import type { IClickhouseProfile } from '../services/profile.service'; import type { IClickhouseProfile } from '../services/profile.service';
import { BaseBuffer } from './base-buffer'; import { BaseBuffer } from './base-buffer';
@@ -152,11 +153,6 @@ export class ProfileBuffer extends BaseBuffer {
profile: IClickhouseProfile, profile: IClickhouseProfile,
logger: ILogger, logger: ILogger,
): Promise<IClickhouseProfile | null> { ): Promise<IClickhouseProfile | null> {
const cacheKey = this.getProfileCacheKey({
profileId: profile.id,
projectId: profile.project_id,
});
const existingProfile = await this.fetchFromCache( const existingProfile = await this.fetchFromCache(
profile.id, profile.id,
profile.project_id, profile.project_id,
@@ -190,19 +186,29 @@ export class ProfileBuffer extends BaseBuffer {
): Promise<IClickhouseProfile | null> { ): Promise<IClickhouseProfile | null> {
logger.debug('Fetching profile from Clickhouse'); logger.debug('Fetching profile from Clickhouse');
const result = await chQuery<IClickhouseProfile>( const result = await chQuery<IClickhouseProfile>(
`SELECT * `SELECT
FROM ${TABLE_NAMES.profiles} id,
WHERE project_id = '${profile.project_id}' project_id,
AND id = '${profile.id}' last_value(nullIf(first_name, '')) as first_name,
${ last_value(nullIf(last_name, '')) as last_name,
profile.is_external === false last_value(nullIf(email, '')) as email,
? 'AND created_at > now() - INTERVAL 2 DAY' last_value(nullIf(avatar, '')) as avatar,
: '' last_value(is_external) as is_external,
} last_value(properties) as properties,
ORDER BY created_at DESC last_value(created_at) as created_at
LIMIT 1`, FROM ${TABLE_NAMES.profiles}
WHERE
id = ${sqlstring.escape(String(profile.id))} AND
project_id = ${sqlstring.escape(profile.project_id)}
${
profile.is_external === false
? ' AND profiles.created_at > now() - INTERVAL 2 DAY'
: ''
}
GROUP BY id, project_id
ORDER BY created_at DESC
LIMIT 1`,
); );
logger.debug('Clickhouse fetch result', { logger.debug('Clickhouse fetch result', {
found: !!result[0], found: !!result[0],
}); });