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;
}

View file

@ -0,0 +1,71 @@
import { EVENT_DATA_TYPE } from 'lib/constants';
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
import { flattenJSON } from 'lib/eventData';
import kafka from 'lib/kafka';
import { EventData } from 'lib/types';
export async function saveEventData(args: {
websiteId: string;
sessionId: string;
eventId: string;
revId: number;
eventName: string;
eventData: EventData;
createdAt: string;
}) {
return runQuery({
[PRISMA]: () => relationalQuery(args),
[CLICKHOUSE]: () => clickhouseQuery(args),
});
}
async function relationalQuery(data: {
websiteId: string;
sessionId: string;
eventId: string;
revId: number;
eventName: string;
eventData: EventData;
createdAt: string;
}) {
return data;
}
async function clickhouseQuery(data: {
websiteId: string;
sessionId: string;
eventId: string;
revId: number;
eventName: string;
eventData: EventData;
createdAt: string;
}) {
const { websiteId, sessionId, eventId, revId, eventName, eventData, createdAt } = data;
const { getDateFormat, sendMessages } = kafka;
const jsonKeys = flattenJSON(eventData);
const messages = jsonKeys.map(a => ({
website_id: websiteId,
session_id: sessionId,
event_id: eventId,
rev_id: revId,
event_name: eventName,
event_key: a.key,
event_string_value:
a.eventDataType === EVENT_DATA_TYPE.string ||
a.eventDataType === EVENT_DATA_TYPE.boolean ||
a.eventDataType === EVENT_DATA_TYPE.array
? a.value
: null,
event_numeric_value: a.eventDataType === EVENT_DATA_TYPE.number ? a.value : null,
event_date_value: a.eventDataType === EVENT_DATA_TYPE.date ? getDateFormat(a.value) : null,
event_data_type: a.eventDataType,
created_at: createdAt,
}));
await sendMessages(messages, 'event_data');
return data;
}

View file

@ -85,6 +85,7 @@ async function clickhouseQuery(data: {
const message = {
website_id: websiteId,
session_id: sessionId,
event_id: uuid(),
rev_id: website?.revId || 0,
country: country ? country : null,
subdivision1: subdivision1 ? subdivision1 : null,