chore(buffer): iron out the buffer issues

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-09-19 21:38:40 +02:00
parent e18ac4d79e
commit 25f329a4cd
11 changed files with 164 additions and 54 deletions

View File

@@ -8,11 +8,19 @@ import {
eventBuffer,
getEvents,
} from '@openpanel/db';
import { createLogger } from '@openpanel/logger';
import type { EventsQueuePayloadCreateSessionEnd } from '@openpanel/queue';
export async function createSessionEnd(
job: Job<EventsQueuePayloadCreateSessionEnd>,
) {
const logger = createLogger({
name: 'job:create-session-end',
}).child({
payload: job.data.payload,
jobId: job.id,
});
const payload = job.data.payload;
const eventsInBuffer = await eventBuffer.findMany(
(item) => item.session_id === payload.sessionId,
@@ -71,7 +79,7 @@ export async function createSessionEnd(
throw new Error('Could not found session_start or any screen_view');
}
job.log('Creating session_start since it was not found');
logger.warn('Creating session_start since it was not found');
sessionStart = {
...firstScreenView,

View File

@@ -8,6 +8,7 @@ import type {
EventsQueuePayloadIncomingEvent,
} from '@openpanel/queue';
import { cacheable } from '@openpanel/redis';
import { createSessionEnd } from './events.create-session-end';
import { incomingEvent } from './events.incoming-event';
@@ -30,11 +31,17 @@ export async function eventsJob(job: Job<EventsQueuePayload>) {
}
}
async function updateEventsCount(projectId: string) {
const getProjectEventsCount = cacheable(async function getProjectEventsCount(
projectId: string,
) {
const res = await chQuery<{ count: number }>(
`SELECT count(*) as count FROM ${TABLE_NAMES.events} WHERE project_id = ${escape(projectId)}`,
);
const count = res[0]?.count;
return res[0]?.count;
}, 60 * 60);
async function updateEventsCount(projectId: string) {
const count = await getProjectEventsCount(projectId);
if (count) {
await db.project.update({
where: {