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,27 +212,33 @@ export async function createProfileAlias({
}); });
} }
export async function upsertProfile({ export async function upsertProfile(
id, {
firstName,
lastName,
email,
avatar,
properties,
projectId,
isExternal,
}: IServiceUpsertProfile) {
return profileBuffer.add({
id, id,
first_name: firstName!, firstName,
last_name: lastName!, lastName,
email: email!, email,
avatar: avatar!, avatar,
properties: properties as Record<string, string | undefined>, properties,
project_id: projectId, projectId,
created_at: formatClickhouseDate(new Date()), isExternal,
is_external: isExternal, }: IServiceUpsertProfile,
}); isFromEvent = false,
) {
return profileBuffer.add(
{
id,
first_name: firstName!,
last_name: lastName!,
email: email!,
avatar: avatar!,
properties: properties as Record<string, string | undefined>,
project_id: projectId,
created_at: formatClickhouseDate(new Date()),
is_external: isExternal,
},
isFromEvent,
);
} }
export async function getProfileId({ export async function getProfileId({