umami/queries/analytics/event/saveEvent.js

61 lines
1.5 KiB
JavaScript
Raw Normal View History

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