umami/queries/analytics/event/saveEvent.js

72 lines
1.6 KiB
JavaScript
Raw Normal View History

2022-08-05 02:43:47 +02:00
import { CLICKHOUSE, RELATIONAL, KAFKA, URL_LENGTH } from 'lib/constants';
2022-07-22 23:43:19 +02:00
import {
getDateFormatClickhouse,
2022-08-09 07:09:18 +02:00
getDateFormatKafka,
2022-07-22 23:43:19 +02:00
prisma,
rawQueryClickhouse,
runAnalyticsQuery,
runQuery,
2022-08-05 02:43:47 +02:00
kafkaProducer,
2022-07-22 23:43:19 +02:00
} from 'lib/db';
2022-07-16 01:47:38 +02:00
2022-07-22 23:43:19 +02:00
export async function saveEvent(...args) {
return runAnalyticsQuery({
2022-07-25 18:47:11 +02:00
[RELATIONAL]: () => relationalQuery(...args),
[CLICKHOUSE]: () => clickhouseQuery(...args),
2022-08-05 02:43:47 +02:00
[KAFKA]: () => kafkaQuery(...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,
url: url?.substr(0, URL_LENGTH),
event_name: event_name?.substr(0, 50),
};
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-07-12 23:14:36 +02:00
return runQuery(
prisma.event.create({
2022-07-30 07:30:09 +02:00
data,
2022-07-12 23:14:36 +02:00
}),
);
}
2022-07-22 23:43:19 +02:00
async function clickhouseQuery(website_id, { event_uuid, session_uuid, url, event_name }) {
const params = [
website_id,
event_uuid,
session_uuid,
url?.substr(0, URL_LENGTH),
event_name?.substr(0, 50),
];
2022-07-22 23:43:19 +02:00
return rawQueryClickhouse(
`
2022-08-09 07:09:18 +02:00
insert into umami.event (created_at, website_id, session_uuid, url, event_name)
2022-07-30 07:30:09 +02:00
values (${getDateFormatClickhouse(new Date())}, $1, $2, $3, $4);`,
2022-07-22 23:43:19 +02:00
params,
);
}
2022-08-05 02:43:47 +02:00
async function kafkaQuery(website_id, { event_uuid, session_uuid, url, event_name }) {
2022-08-05 02:43:47 +02:00
const params = {
event_uuid: event_uuid,
2022-08-05 02:43:47 +02:00
website_id: website_id,
session_uuid: session_uuid,
2022-08-09 07:09:18 +02:00
created_at: getDateFormatKafka(new Date()),
2022-08-05 02:43:47 +02:00
url: url?.substr(0, URL_LENGTH),
event_name: event_name?.substr(0, 50),
2022-08-05 02:43:47 +02:00
};
await kafkaProducer(params, 'event');
}