chore(buffer): final adjustments to buffer before deploy

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-09-18 22:48:52 +02:00
parent 13a83155cf
commit dfe00040de
12 changed files with 21972 additions and 54 deletions

View File

@@ -38,31 +38,43 @@ export async function postEvent(
ua,
});
const isScreenView = request.body.name === 'screen_view';
// this will ensure that we don't have multiple events creating sessions
const locked = await getRedisCache().set(
`request:priority:${currentDeviceId}-${previousDeviceId}`,
`request:priority:${currentDeviceId}-${previousDeviceId}:${isScreenView ? 'screen_view' : 'other'}`,
'locked',
'EX',
10,
5,
'NX',
);
eventsQueue.add('event', {
type: 'incomingEvent',
payload: {
projectId: request.projectId,
headers: getStringHeaders(request.headers),
event: {
...request.body,
// Dont rely on the client for the timestamp
timestamp: new Date().toISOString(),
eventsQueue.add(
'event',
{
type: 'incomingEvent',
payload: {
projectId: request.projectId,
headers: getStringHeaders(request.headers),
event: {
...request.body,
// Dont rely on the client for the timestamp
timestamp: request.timestamp
? new Date(request.timestamp).toISOString()
: new Date().toISOString(),
},
geo,
currentDeviceId,
previousDeviceId,
priority: locked === 'OK',
},
geo,
currentDeviceId,
previousDeviceId,
priority: locked === 'OK',
},
});
{
// Prioritize 'screen_view' events by setting no delay
// This ensures that session starts are created from 'screen_view' events
// rather than other events, maintaining accurate session tracking
delay: request.body.name === 'screen_view' ? 0 : 1000,
},
);
reply.status(202).send('ok');
}

View File

@@ -4,6 +4,7 @@ import { parseUserAgent } from '@/utils/parseUserAgent';
import type { FastifyReply, FastifyRequest } from 'fastify';
import { path, assocPath, pathOr, pick } from 'ramda';
import { toISOString } from '@openpanel/common';
import { generateDeviceId } from '@openpanel/common/server';
import {
createProfileAlias,
@@ -117,6 +118,9 @@ export async function handler(
projectId,
geo,
headers: getStringHeaders(request.headers),
timestamp: request.timestamp
? new Date(request.timestamp).toISOString()
: new Date().toISOString(),
}),
];
@@ -182,6 +186,7 @@ async function track({
projectId,
geo,
headers,
timestamp,
}: {
payload: TrackPayload;
currentDeviceId: string;
@@ -189,32 +194,43 @@ async function track({
projectId: string;
geo: GeoLocation;
headers: Record<string, string | undefined>;
timestamp: string;
}) {
const isScreenView = payload.name === 'screen_view';
// this will ensure that we don't have multiple events creating sessions
const locked = await getRedisCache().set(
`request:priority:${currentDeviceId}-${previousDeviceId}`,
`request:priority:${currentDeviceId}-${previousDeviceId}:${isScreenView ? 'screen_view' : 'other'}`,
'locked',
'EX',
10,
5,
'NX',
);
eventsQueue.add('event', {
type: 'incomingEvent',
payload: {
projectId,
headers,
event: {
...payload,
// Dont rely on the client for the timestamp
timestamp: new Date().toISOString(),
eventsQueue.add(
'event',
{
type: 'incomingEvent',
payload: {
projectId,
headers,
event: {
...payload,
// Dont rely on the client for the timestamp
timestamp,
},
geo,
currentDeviceId,
previousDeviceId,
priority: locked === 'OK',
},
geo,
currentDeviceId,
previousDeviceId,
priority: locked === 'OK',
},
});
{
// Prioritize 'screen_view' events by setting no delay
// This ensures that session starts are created from 'screen_view' events
// rather than other events, maintaining accurate session tracking
delay: payload.name === 'screen_view' ? 0 : 1000,
},
);
}
async function identify({

View File

@@ -32,6 +32,7 @@ declare module 'fastify' {
interface FastifyRequest {
projectId: string;
client: IServiceClient | null;
timestamp?: number;
}
}
@@ -75,6 +76,11 @@ const startServer = async () => {
}
};
fastify.addHook('preHandler', (request, reply, done) => {
request.timestamp = Date.now();
done();
});
// add header to request if it does not exist
fastify.addHook('onRequest', (request, reply, done) => {
if (!request.headers['request-id']) {