Add event_data base.

This commit is contained in:
Brian Cao 2023-03-20 13:21:16 -07:00
parent 72af76a417
commit 15c5cc065e
19 changed files with 395 additions and 45 deletions

View file

@ -4,6 +4,7 @@ import kafka from 'lib/kafka';
import prisma from 'lib/prisma';
import { uuid } from 'lib/crypto';
import cache from 'lib/cache';
import { saveEventData } from '../eventData/saveEventData';
export async function saveEvent(args: {
id: string;
@ -12,6 +13,7 @@ export async function saveEvent(args: {
referrer?: string;
pageTitle?: string;
eventName?: string;
eventData?: any;
hostname?: string;
browser?: string;
os?: string;
@ -36,6 +38,7 @@ async function relationalQuery(data: {
referrer?: string;
pageTitle?: string;
eventName?: string;
eventData?: any;
}) {
const { websiteId, id: sessionId, url, eventName, referrer, pageTitle } = data;
@ -60,6 +63,7 @@ async function clickhouseQuery(data: {
referrer?: string;
pageTitle?: string;
eventName?: string;
eventData?: any;
hostname?: string;
browser?: string;
os?: string;
@ -77,6 +81,7 @@ async function clickhouseQuery(data: {
url,
pageTitle,
eventName,
eventData,
country,
subdivision1,
subdivision2,
@ -85,11 +90,13 @@ async function clickhouseQuery(data: {
} = data;
const { getDateFormat, sendMessage } = kafka;
const website = await cache.fetchWebsite(websiteId);
const eventId = uuid();
const createdAt = getDateFormat(new Date());
const message = {
website_id: websiteId,
session_id: sessionId,
event_id: uuid(),
event_id: eventId,
country: country ? country : null,
subdivision1: subdivision1 ? subdivision1 : null,
subdivision2: subdivision2 ? subdivision2 : null,
@ -99,11 +106,23 @@ async function clickhouseQuery(data: {
event_type: EVENT_TYPE.customEvent,
event_name: eventName?.substring(0, EVENT_NAME_LENGTH),
rev_id: website?.revId || 0,
created_at: getDateFormat(new Date()),
created_at: createdAt,
...args,
};
await sendMessage(message, 'event');
if (eventData) {
await saveEventData({
websiteId,
sessionId,
eventId,
revId: website?.revId,
eventName: eventName?.substring(0, EVENT_NAME_LENGTH),
eventData,
createdAt,
});
}
return data;
}