improve(buffer): don't add profile if its from an event and already created

This commit is contained in:
Carl-Gerhard Lindesvärd
2025-02-24 22:55:30 +01:00
parent bc3b9ae7bf
commit 5863dd8c2b
3 changed files with 33 additions and 22 deletions

View File

@@ -53,7 +53,7 @@ export class ProfileBuffer extends BaseBuffer {
return `${this.redisProfilePrefix}${projectId}:${profileId}`; return `${this.redisProfilePrefix}${projectId}:${profileId}`;
} }
async add(profile: IClickhouseProfile) { async add(profile: IClickhouseProfile, isFromEvent = false) {
const logger = this.logger.child({ const logger = this.logger.child({
projectId: profile.project_id, projectId: profile.project_id,
profileId: profile.id, profileId: profile.id,
@@ -64,6 +64,11 @@ export class ProfileBuffer extends BaseBuffer {
const existingProfile = await this.fetchFromCache(profile, logger); const existingProfile = await this.fetchFromCache(profile, logger);
if (isFromEvent && existingProfile) {
logger.debug('Profile already created, skipping');
return;
}
const mergedProfile: IClickhouseProfile = existingProfile const mergedProfile: IClickhouseProfile = existingProfile
? deepMergeObjects(existingProfile, profile) ? deepMergeObjects(existingProfile, profile)
: profile; : profile;

View File

@@ -303,7 +303,7 @@ export async function createEvent(payload: IServiceCreateEventPayload) {
profile.isExternal || profile.isExternal ||
(profile.isExternal === false && payload.name === 'session_start') (profile.isExternal === false && payload.name === 'session_start')
) { ) {
await upsertProfile(profile); await upsertProfile(profile, true);
} }
} }

View File

@@ -212,7 +212,8 @@ export async function createProfileAlias({
}); });
} }
export async function upsertProfile({ export async function upsertProfile(
{
id, id,
firstName, firstName,
lastName, lastName,
@@ -221,8 +222,11 @@ export async function upsertProfile({
properties, properties,
projectId, projectId,
isExternal, isExternal,
}: IServiceUpsertProfile) { }: IServiceUpsertProfile,
return profileBuffer.add({ isFromEvent = false,
) {
return profileBuffer.add(
{
id, id,
first_name: firstName!, first_name: firstName!,
last_name: lastName!, last_name: lastName!,
@@ -232,7 +236,9 @@ export async function upsertProfile({
project_id: projectId, project_id: projectId,
created_at: formatClickhouseDate(new Date()), created_at: formatClickhouseDate(new Date()),
is_external: isExternal, is_external: isExternal,
}); },
isFromEvent,
);
} }
export async function getProfileId({ export async function getProfileId({