MCPcopy Create free account
hub / github.com/Openpanel-dev/openpanel / postEvent

Function postEvent

apps/api/src/controllers/event.controller.ts:16–73  ·  view source on GitHub ↗
(
  request: FastifyRequest<{
    Body: DeprecatedPostEventPayload;
  }>,
  reply: FastifyReply
)

Source from the content-addressed store, hash-verified

14import { getDeviceId } from '@/utils/ids';
15
16export async function postEvent(
17 request: FastifyRequest<{
18 Body: DeprecatedPostEventPayload;
19 }>,
20 reply: FastifyReply
21) {
22 const { timestamp, isTimestampFromThePast } = getTimestamp(
23 request.timestamp,
24 request.body
25 );
26 const ip = request.clientIp;
27 const ua = request.headers['user-agent'] ?? 'unknown/1.0';
28 const projectId = request.client?.projectId;
29 const headers = getStringHeaders(request.headers);
30
31 if (!projectId) {
32 reply.status(400).send('missing origin');
33 return;
34 }
35
36 const [salts, geo] = await Promise.all([getSalts(), getGeoLocation(ip)]);
37 const { deviceId, sessionId } = await getDeviceId({
38 projectId,
39 ip,
40 ua,
41 salts,
42 });
43
44 const uaInfo = parseUserAgent(ua, request.body?.properties);
45 const groupId = uaInfo.isServer
46 ? `${projectId}:${request.body?.profileId ?? generateId()}`
47 : deviceId;
48 const queueData: EventsQueuePayloadIncomingEvent['payload'] = {
49 projectId,
50 headers,
51 event: {
52 ...request.body,
53 timestamp,
54 isTimestampFromThePast,
55 },
56 uaInfo,
57 geo,
58 deviceId,
59 sessionId: sessionId ?? '',
60 };
61
62 if (shouldUseKafka()) {
63 await produceIncomingEvent(queueData, groupId);
64 } else {
65 await getEventsGroupQueueShard(groupId).add({
66 orderMs: new Date(timestamp).getTime(),
67 data: queueData,
68 groupId,
69 });
70 }
71
72 reply.status(202).send('ok');
73}

Callers

nothing calls this directly

Calls 11

getTimestampFunction · 0.90
getStringHeadersFunction · 0.90
getGeoLocationFunction · 0.90
getDeviceIdFunction · 0.90
parseUserAgentFunction · 0.90
generateIdFunction · 0.90
shouldUseKafkaFunction · 0.90
produceIncomingEventFunction · 0.90
getEventsGroupQueueShardFunction · 0.90
sendMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected