umami/queries/analytics/event/saveEvent.js

53 lines
1.3 KiB
JavaScript
Raw Normal View History

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-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-07-30 07:30:09 +02:00
async function relationalQuery(website_id, { session_id, url, event_name, event_data }) {
const data = {
website_id,
session_id,
2022-08-28 06:38:35 +02:00
url: url?.substring(0, URL_LENGTH),
event_name: event_name?.substring(0, EVENT_NAME_LENGTH),
2022-07-30 07:30:09 +02:00
};
if (event_data) {
data.event_data = {
2022-07-30 07:30:09 +02:00
create: {
event_data: event_data,
2022-07-30 07:30:09 +02:00
},
};
}
2022-08-28 06:38:35 +02:00
return prisma.client.event.create({
data,
});
2022-07-12 23:14:36 +02:00
}
2022-07-22 23:43:19 +02:00
2022-08-31 02:29:30 +02:00
async function clickhouseQuery(
website_id,
2022-09-12 18:55:34 +02:00
{ session: { country, ...sessionArgs }, event_uuid, url, event_name, event_data },
2022-08-31 02:29:30 +02:00
) {
2022-08-28 06:38:35 +02:00
const { getDateFormat, sendMessage } = kafka;
2022-09-12 18:55:34 +02:00
2022-08-05 02:43:47 +02:00
const params = {
2022-08-31 02:29:30 +02:00
event_uuid,
website_id,
2022-08-28 06:38:35 +02:00
created_at: getDateFormat(new Date()),
url: url?.substring(0, URL_LENGTH),
event_name: event_name?.substring(0, EVENT_NAME_LENGTH),
2022-08-31 02:29:30 +02:00
event_data: JSON.stringify(event_data),
2022-09-12 18:55:34 +02:00
...sessionArgs,
country: country ? country : null,
2022-08-05 02:43:47 +02:00
};
2022-08-28 06:38:35 +02:00
await sendMessage(params, 'event');
2022-08-05 02:43:47 +02:00
}