2022-11-15 22:21:14 +01:00
|
|
|
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';
|
|
|
|
import { uuid } from 'lib/crypto';
|
|
|
|
import cache from 'lib/cache';
|
2022-11-19 03:49:58 +01:00
|
|
|
import { UmamiApi } from 'lib/constants';
|
2022-11-15 22:21:14 +01:00
|
|
|
|
|
|
|
export async function saveEvent(args: {
|
|
|
|
id: string;
|
|
|
|
websiteId: string;
|
|
|
|
url: string;
|
|
|
|
referrer?: string;
|
|
|
|
eventName?: string;
|
|
|
|
eventData?: any;
|
|
|
|
hostname?: string;
|
|
|
|
browser?: string;
|
|
|
|
os?: string;
|
|
|
|
device?: string;
|
|
|
|
screen?: string;
|
|
|
|
language?: string;
|
|
|
|
country?: string;
|
|
|
|
}) {
|
|
|
|
return runQuery({
|
|
|
|
[PRISMA]: () => relationalQuery(args),
|
|
|
|
[CLICKHOUSE]: () => clickhouseQuery(args),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function relationalQuery(data: {
|
|
|
|
id: string;
|
|
|
|
websiteId: string;
|
|
|
|
url: string;
|
|
|
|
referrer?: string;
|
|
|
|
eventName?: string;
|
|
|
|
eventData?: any;
|
|
|
|
}) {
|
|
|
|
const { websiteId, id: sessionId, url, eventName, eventData, referrer } = data;
|
|
|
|
|
|
|
|
const params = {
|
|
|
|
id: uuid(),
|
|
|
|
websiteId,
|
|
|
|
sessionId,
|
|
|
|
url: url?.substring(0, URL_LENGTH),
|
|
|
|
referrer: referrer?.substring(0, URL_LENGTH),
|
|
|
|
eventType: UmamiApi.EventType.Event,
|
|
|
|
eventName: eventName?.substring(0, EVENT_NAME_LENGTH),
|
|
|
|
eventData,
|
|
|
|
};
|
|
|
|
|
|
|
|
return prisma.client.websiteEvent.create({
|
|
|
|
data: params,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function clickhouseQuery(data: {
|
|
|
|
id: string;
|
|
|
|
websiteId: string;
|
|
|
|
url: string;
|
|
|
|
referrer?: string;
|
|
|
|
eventName?: string;
|
|
|
|
eventData?: any;
|
|
|
|
hostname?: string;
|
|
|
|
browser?: string;
|
|
|
|
os?: string;
|
|
|
|
device?: string;
|
|
|
|
screen?: string;
|
|
|
|
language?: string;
|
|
|
|
country?: string;
|
|
|
|
}) {
|
|
|
|
const { websiteId, id: sessionId, url, eventName, eventData, country, ...args } = data;
|
|
|
|
const { getDateFormat, sendMessage } = kafka;
|
|
|
|
const website = await cache.fetchWebsite(websiteId);
|
|
|
|
|
2022-11-23 00:06:52 +01:00
|
|
|
const message = {
|
2022-11-15 22:21:14 +01:00
|
|
|
website_id: websiteId,
|
|
|
|
session_id: sessionId,
|
|
|
|
event_id: uuid(),
|
|
|
|
url: url?.substring(0, URL_LENGTH),
|
2022-11-23 00:06:52 +01:00
|
|
|
event_type: UmamiApi.EventType.Event,
|
2022-11-15 22:21:14 +01:00
|
|
|
event_name: eventName?.substring(0, EVENT_NAME_LENGTH),
|
|
|
|
event_data: eventData ? JSON.stringify(eventData) : null,
|
|
|
|
rev_id: website?.revId || 0,
|
|
|
|
created_at: getDateFormat(new Date()),
|
|
|
|
country: country ? country : null,
|
|
|
|
...args,
|
|
|
|
};
|
|
|
|
|
2022-11-23 00:06:52 +01:00
|
|
|
await sendMessage(message, 'event');
|
2022-11-15 22:21:14 +01:00
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|