prep events partition

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-07-19 22:08:22 +02:00
parent ddc2ce338f
commit 3993b493e3
27 changed files with 136 additions and 71 deletions

View File

@@ -1,7 +1,12 @@
import type { Job } from 'bullmq';
import { getTime } from '@openpanel/common';
import { createEvent, eventBuffer, getEvents } from '@openpanel/db';
import {
createEvent,
eventBuffer,
getEvents,
TABLE_NAMES,
} from '@openpanel/db';
import type { EventsQueuePayloadCreateSessionEnd } from '@openpanel/queue';
export async function createSessionEnd(
@@ -13,12 +18,12 @@ export async function createSessionEnd(
);
const sql = `
SELECT * FROM events
SELECT * FROM ${TABLE_NAMES.events}
WHERE
session_id = '${payload.sessionId}'
AND created_at >= (
SELECT created_at
FROM events
FROM ${TABLE_NAMES.events}
WHERE
session_id = '${payload.sessionId}'
AND name = 'session_start'

View File

@@ -1,12 +1,13 @@
import type { Job } from 'bullmq';
import { escape } from 'sqlstring';
import { chQuery, db } from '@openpanel/db';
import { chQuery, db, TABLE_NAMES } from '@openpanel/db';
import type {
EventsQueuePayload,
EventsQueuePayloadCreateSessionEnd,
EventsQueuePayloadIncomingEvent,
} from '@openpanel/queue';
import { redis } from '@openpanel/redis';
import { createSessionEnd } from './events.create-session-end';
import { incomingEvent } from './events.incoming-event';
@@ -26,7 +27,7 @@ export async function eventsJob(job: Job<EventsQueuePayload>) {
async function updateEventsCount(projectId: string) {
const res = await chQuery<{ count: number }>(
`SELECT count(*) as count FROM events WHERE project_id = ${escape(projectId)}`
`SELECT count(*) as count FROM ${TABLE_NAMES.events} WHERE project_id = ${escape(projectId)}`
);
const count = res[0]?.count;
if (count) {