fix: handle event batch data in clickhouseQuery

This commit is contained in:
harry 2024-11-26 17:25:51 +07:00
parent 1a908b86fe
commit bc22ddec02
2 changed files with 85 additions and 36 deletions

2
next-env.d.ts vendored
View file

@ -3,4 +3,4 @@
/// <reference types="next/navigation-types/compat/navigation" /> /// <reference types="next/navigation-types/compat/navigation" />
// NOTE: This file should not be edited // NOTE: This file should not be edited
// see https://nextjs.org/docs/app/building-your-application/configuring/typescript for more information. // see https://nextjs.org/docs/basic-features/typescript for more information.

View file

@ -189,15 +189,21 @@ async function clickhouseQuery(data: {
} = data; } = data;
const { insert, getUTCString } = clickhouse; const { insert, getUTCString } = clickhouse;
const { sendMessage } = kafka; const { sendMessage } = kafka;
const eventId = uuid();
const createdAt = getUTCString(); const createdAt = getUTCString();
const message = { const websiteEventData = [];
const eventsData = [];
if (eventBatchData) {
for (const eventData of eventBatchData) {
const websiteEventId = uuid();
websiteEventData.push({
...args, ...args,
website_id: websiteId, website_id: websiteId,
session_id: sessionId, session_id: sessionId,
visit_id: visitId, visit_id: visitId,
event_id: eventId, event_id: websiteEventId,
country: country, country: country,
subdivision1: subdivision1:
country && subdivision1 country && subdivision1
@ -217,27 +223,70 @@ async function clickhouseQuery(data: {
event_name: eventName ? eventName?.substring(0, EVENT_NAME_LENGTH) : null, event_name: eventName ? eventName?.substring(0, EVENT_NAME_LENGTH) : null,
tag: tag, tag: tag,
created_at: createdAt, created_at: createdAt,
}; });
if (kafka.enabled) { eventsData.push({
await sendMessage('event', message);
} else {
await insert('website_event', [message]);
}
if (eventData || eventBatchData) {
await saveEventData({
websiteId, websiteId,
sessionId, sessionId,
visitId, visitId,
eventId, eventId: websiteEventId,
urlPath: urlPath?.substring(0, URL_LENGTH),
eventName: eventName?.substring(0, EVENT_NAME_LENGTH),
eventData,
createdAt,
});
}
} else {
const websiteEventId = uuid();
websiteEventData.push({
...args,
website_id: websiteId,
session_id: sessionId,
visit_id: visitId,
event_id: websiteEventId,
country: country,
subdivision1:
country && subdivision1
? subdivision1.includes('-')
? subdivision1
: `${country}-${subdivision1}`
: null,
subdivision2: subdivision2,
city: city,
url_path: urlPath?.substring(0, URL_LENGTH),
url_query: urlQuery?.substring(0, URL_LENGTH),
referrer_path: referrerPath?.substring(0, URL_LENGTH),
referrer_query: referrerQuery?.substring(0, URL_LENGTH),
referrer_domain: referrerDomain?.substring(0, URL_LENGTH),
page_title: pageTitle?.substring(0, PAGE_TITLE_LENGTH),
event_type: eventName ? EVENT_TYPE.customEvent : EVENT_TYPE.pageView,
event_name: eventName ? eventName?.substring(0, EVENT_NAME_LENGTH) : null,
tag: tag,
created_at: createdAt,
});
eventsData.push({
websiteId,
sessionId,
visitId,
eventId: websiteEventId,
urlPath: urlPath?.substring(0, URL_LENGTH), urlPath: urlPath?.substring(0, URL_LENGTH),
eventName: eventName?.substring(0, EVENT_NAME_LENGTH), eventName: eventName?.substring(0, EVENT_NAME_LENGTH),
eventData, eventData,
eventBatchData,
createdAt, createdAt,
}); });
} }
if (kafka.enabled) {
await sendMessage('event', websiteEventData);
} else {
await insert('website_event', websiteEventData);
}
if (eventData || eventBatchData) {
await saveEventData(eventsData);
}
return data; return data;
} }