mirror of
https://github.com/kremalicious/umami.git
synced 2024-11-15 17:55:08 +01:00
47 lines
1.2 KiB
JavaScript
47 lines
1.2 KiB
JavaScript
import { EVENT_NAME_LENGTH, URL_LENGTH } from 'lib/constants';
|
|
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
|
import kafka from 'lib/kafka';
|
|
import prisma from 'lib/prisma';
|
|
|
|
export async function saveEvent(...args) {
|
|
return runQuery({
|
|
[PRISMA]: () => relationalQuery(...args),
|
|
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
|
});
|
|
}
|
|
|
|
async function relationalQuery(website_id, { session_id, url, event_name, event_data }) {
|
|
const data = {
|
|
website_id,
|
|
session_id,
|
|
url: url?.substring(0, URL_LENGTH),
|
|
event_name: event_name?.substring(0, EVENT_NAME_LENGTH),
|
|
};
|
|
|
|
if (event_data) {
|
|
data.event_data = {
|
|
create: {
|
|
event_data: event_data,
|
|
},
|
|
};
|
|
}
|
|
|
|
return prisma.client.event.create({
|
|
data,
|
|
});
|
|
}
|
|
|
|
async function clickhouseQuery(website_id, { event_uuid, session_uuid, url, event_name }) {
|
|
const { getDateFormat, sendMessage } = kafka;
|
|
const params = {
|
|
event_uuid: event_uuid,
|
|
website_id: website_id,
|
|
session_uuid: session_uuid,
|
|
created_at: getDateFormat(new Date()),
|
|
url: url?.substring(0, URL_LENGTH),
|
|
event_name: event_name?.substring(0, EVENT_NAME_LENGTH),
|
|
};
|
|
|
|
await sendMessage(params, 'event');
|
|
}
|