Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

wip

Carl-Gerhard Lindesvärd6fae06770ce4
diff --git a/packages/db/src/buffers/event-buffer-psql.ts b/packages/db/src/buffers/event-buffer-psql.ts
index 292292aeb..45bca8d0d 100644
--- a/packages/db/src/buffers/event-buffer-psql.ts
+++ b/packages/db/src/buffers/event-buffer-psql.ts
@@ -1,4 +1,4 @@
-import { setSuperJson } from '@openpanel/common';
+import { getSafeJson, setSuperJson } from '@openpanel/common';
import { getRedisCache, getRedisPub, runEvery } from '@openpanel/redis';
import { Prisma } from '@prisma/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) {
this.publishEvent('event:received', event);
if (event.profile_id) {
@@ -245,22 +257,43 @@ export class EventBuffer extends BaseBuffer {
projectId: string;
profileId: string;
}): Promise<IServiceEvent | null> {
- const event = await db.$primary().eventBuffer.findFirst({
- where: {
- projectId,
- profileId,
- name: 'screen_view',
- },
- orderBy: { createdAt: 'desc' },
- select: {
- payload: true,
- },
- });
+ // const event = await db.$primary().eventBuffer.findFirst({
+ // where: {
+ // projectId,
+ // profileId,
+ // name: 'screen_view',
+ // },
+ // orderBy: { createdAt: 'desc' },
+ // select: {
+ // payload: true,
+ // },
+ // });
+
+ // if (event) {
+ // return transformEvent(event.payload);
+ // }
+
+ // return null;
+ const event = await getRedisCache().get(
+ this.getLastEventKey({ projectId, profileId }),
+ );
if (event) {
- return transformEvent(event.payload);
+ const parsed = getSafeJson<IClickhouseEvent>(event);
+ if (parsed) {
+ return transformEvent(parsed);
+ }
}
-
return null;
}
+
+ getLastEventKey({
+ projectId,
+ profileId,
+ }: {
+ projectId: string;
+ profileId: string;
+ }) {
+ return `session:last_screen_view:${projectId}:${profileId}`;
+ }
}