This commit is contained in:
Carl-Gerhard Lindesvärd
2025-02-04 17:02:10 +01:00
parent c2d2f64fc3
commit 6fae06770c

View File

@@ -1,4 +1,4 @@
import { setSuperJson } from '@openpanel/common'; import { getSafeJson, setSuperJson } from '@openpanel/common';
import { getRedisCache, getRedisPub, runEvery } from '@openpanel/redis'; import { getRedisCache, getRedisPub, runEvery } from '@openpanel/redis';
import { Prisma } from '@prisma/client'; import { Prisma } from '@prisma/client';
import { ch } from '../clickhouse-client'; import { ch } from '../clickhouse-client';
@@ -44,6 +44,18 @@ export class EventBuffer extends BaseBuffer {
}, },
}); });
if (event.name === 'screen_view') {
await getRedisCache().set(
this.getLastEventKey({
projectId: event.project_id,
profileId: event.profile_id,
}),
JSON.stringify(event),
'EX',
60 * 31,
);
}
if (!process.env.TEST_NEW_BUFFER) { if (!process.env.TEST_NEW_BUFFER) {
this.publishEvent('event:received', event); this.publishEvent('event:received', event);
if (event.profile_id) { if (event.profile_id) {
@@ -245,22 +257,43 @@ export class EventBuffer extends BaseBuffer {
projectId: string; projectId: string;
profileId: string; profileId: string;
}): Promise<IServiceEvent | null> { }): Promise<IServiceEvent | null> {
const event = await db.$primary().eventBuffer.findFirst({ // const event = await db.$primary().eventBuffer.findFirst({
where: { // where: {
projectId, // projectId,
profileId, // profileId,
name: 'screen_view', // name: 'screen_view',
}, // },
orderBy: { createdAt: 'desc' }, // orderBy: { createdAt: 'desc' },
select: { // select: {
payload: true, // payload: true,
}, // },
}); // });
// if (event) {
// return transformEvent(event.payload);
// }
// return null;
const event = await getRedisCache().get(
this.getLastEventKey({ projectId, profileId }),
);
if (event) { if (event) {
return transformEvent(event.payload); const parsed = getSafeJson<IClickhouseEvent>(event);
if (parsed) {
return transformEvent(parsed);
}
} }
return null; return null;
} }
getLastEventKey({
projectId,
profileId,
}: {
projectId: string;
profileId: string;
}) {
return `session:last_screen_view:${projectId}:${profileId}`;
}
} }