perf(worker): try to improve perf of worker

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-09-24 09:19:30 +02:00
parent a1278ca371
commit 25a7365569
6 changed files with 425 additions and 359 deletions

View File

@@ -1,7 +1,7 @@
import { groupBy, omit } from 'ramda';
import SuperJSON from 'superjson';
import { deepMergeObjects } from '@openpanel/common';
import { deepMergeObjects, getSafeJson } from '@openpanel/common';
import { getRedisCache, getRedisPub } from '@openpanel/redis';
import {
@@ -25,6 +25,51 @@ export class EventBuffer extends RedisBuffer<BufferType> {
super(TABLE_NAMES.events, null);
}
getLastEventKey({
projectId,
profileId,
}: {
projectId: string;
profileId: string;
}) {
return `session:last_screen_view:${projectId}:${profileId}`;
}
public async getLastScreenView({
projectId,
profileId,
}: {
projectId: string;
profileId: string;
}): Promise<IServiceEvent | null> {
const event = await getRedisCache().get(
this.getLastEventKey({ projectId, profileId }),
);
if (event) {
const parsed = getSafeJson<BufferType>(event);
if (parsed) {
return transformEvent(parsed);
}
}
return null;
}
public async add(event: BufferType) {
await super.add(event);
if (event.name === 'screen_view') {
await getRedisCache().set(
this.getLastEventKey({
projectId: event.project_id,
profileId: event.profile_id,
}),
JSON.stringify(event),
'EX',
60 * 31,
);
}
}
public onAdd(event: BufferType) {
getRedisPub().publish(
'event:received',

View File

@@ -219,27 +219,4 @@ export class ProfileBuffer extends RedisBuffer<BufferType> {
});
await multi.exec();
}
public findMany: FindMany<IClickhouseProfile, IServiceProfile> = async (
callback,
) => {
return this.getQueue(-1)
.then((queue) => {
return queue.filter(callback).map(transformProfile);
})
.catch(() => {
return [];
});
};
public find: Find<IClickhouseProfile, IServiceProfile> = async (callback) => {
return this.getQueue(-1)
.then((queue) => {
const match = queue.find(callback);
return match ? transformProfile(match) : null;
})
.catch(() => {
return null;
});
};
}

View File

@@ -588,9 +588,10 @@ export async function getLastScreenViewFromProfileId({
return null;
}
const eventInBuffer = await eventBuffer.find(
(item) => item.profile_id === profileId,
);
const eventInBuffer = await eventBuffer.getLastScreenView({
projectId,
profileId,
});
if (eventInBuffer) {
return eventInBuffer;