2022-08-29 19:47:01 +02:00
|
|
|
import { EVENT_NAME_LENGTH, URL_LENGTH } from 'lib/constants';
|
|
|
|
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
2022-08-26 07:23:19 +02:00
|
|
|
import kafka from 'lib/kafka';
|
2022-08-29 19:47:01 +02:00
|
|
|
import prisma from 'lib/prisma';
|
2022-11-01 07:42:37 +01:00
|
|
|
import { uuid } from 'lib/crypto';
|
2022-11-08 07:35:51 +01:00
|
|
|
import cache from 'lib/cache';
|
2022-07-16 01:47:38 +02:00
|
|
|
|
2022-07-22 23:43:19 +02:00
|
|
|
export async function saveEvent(...args) {
|
2022-08-28 06:38:35 +02:00
|
|
|
return runQuery({
|
|
|
|
[PRISMA]: () => relationalQuery(...args),
|
2022-07-25 18:47:11 +02:00
|
|
|
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
2022-07-22 23:43:19 +02:00
|
|
|
});
|
|
|
|
}
|
2022-07-12 23:14:36 +02:00
|
|
|
|
2022-11-08 07:35:51 +01:00
|
|
|
async function relationalQuery(data) {
|
|
|
|
const { websiteId, sessionId, url, eventName, eventData } = data;
|
|
|
|
const eventId = uuid();
|
|
|
|
|
|
|
|
const params = {
|
2022-11-01 07:42:37 +01:00
|
|
|
id: eventId,
|
2022-10-10 22:42:18 +02:00
|
|
|
websiteId,
|
|
|
|
sessionId,
|
2022-08-28 06:38:35 +02:00
|
|
|
url: url?.substring(0, URL_LENGTH),
|
2022-10-10 22:42:18 +02:00
|
|
|
eventName: eventName?.substring(0, EVENT_NAME_LENGTH),
|
2022-07-30 07:30:09 +02:00
|
|
|
};
|
|
|
|
|
2022-10-10 22:42:18 +02:00
|
|
|
if (eventData) {
|
2022-11-08 07:35:51 +01:00
|
|
|
params.eventData = {
|
2022-07-30 07:30:09 +02:00
|
|
|
create: {
|
2022-11-08 07:35:51 +01:00
|
|
|
id: eventId,
|
2022-10-10 22:42:18 +02:00
|
|
|
eventData: eventData,
|
2022-07-30 07:30:09 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-08-28 06:38:35 +02:00
|
|
|
return prisma.client.event.create({
|
2022-11-08 07:35:51 +01:00
|
|
|
data: params,
|
2022-08-28 06:38:35 +02:00
|
|
|
});
|
2022-07-12 23:14:36 +02:00
|
|
|
}
|
2022-07-22 23:43:19 +02:00
|
|
|
|
2022-11-08 07:35:51 +01:00
|
|
|
async function clickhouseQuery(data) {
|
2022-11-09 02:47:19 +01:00
|
|
|
const { websiteId, id: sessionId, url, eventName, eventData, country, ...args } = data;
|
2022-08-28 06:38:35 +02:00
|
|
|
const { getDateFormat, sendMessage } = kafka;
|
2022-11-08 07:35:51 +01:00
|
|
|
const website = await cache.fetchWebsite(websiteId);
|
2022-09-12 18:55:34 +02:00
|
|
|
|
2022-08-05 02:43:47 +02:00
|
|
|
const params = {
|
2022-10-10 22:42:18 +02:00
|
|
|
website_id: websiteId,
|
2022-11-08 07:35:51 +01:00
|
|
|
session_id: sessionId,
|
|
|
|
event_id: uuid(),
|
2022-08-28 06:38:35 +02:00
|
|
|
url: url?.substring(0, URL_LENGTH),
|
2022-10-10 22:42:18 +02:00
|
|
|
event_name: eventName?.substring(0, EVENT_NAME_LENGTH),
|
2022-10-22 06:33:23 +02:00
|
|
|
event_data: eventData ? JSON.stringify(eventData) : null,
|
2022-11-08 07:35:51 +01:00
|
|
|
rev_id: website?.revId || 0,
|
|
|
|
created_at: getDateFormat(new Date()),
|
2022-11-09 02:47:19 +01:00
|
|
|
country: country ? country : null,
|
|
|
|
...args,
|
2022-08-05 02:43:47 +02:00
|
|
|
};
|
|
|
|
|
2022-08-28 06:38:35 +02:00
|
|
|
await sendMessage(params, 'event');
|
2022-11-08 07:35:51 +01:00
|
|
|
|
|
|
|
return data;
|
2022-08-05 02:43:47 +02:00
|
|
|
}
|