umami/queries/analytics/event/saveEvent.js

47 lines
1.2 KiB
JavaScript
Raw Normal View History

2022-07-22 23:43:19 +02:00
import { CLICKHOUSE, RELATIONAL, URL_LENGTH } from 'lib/constants';
import {
getDateFormatClickhouse,
prisma,
rawQueryClickhouse,
runAnalyticsQuery,
runQuery,
} 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({
[`${RELATIONAL}`]: () => relationalQuery(...args),
[`${CLICKHOUSE}`]: () => clickhouseQuery(...args),
});
}
2022-07-12 23:14:36 +02:00
2022-07-22 23:43:19 +02:00
async function relationalQuery(website_id, { session_id, url, event_type, event_value }) {
2022-07-12 23:14:36 +02:00
return runQuery(
prisma.event.create({
data: {
website_id,
session_id,
url: url?.substr(0, URL_LENGTH),
event_type: event_type?.substr(0, 50),
event_value: event_value?.substr(0, 50),
},
}),
);
}
2022-07-22 23:43:19 +02:00
async function clickhouseQuery(website_id, { session_uuid, url, event_type, event_value }) {
const params = [
website_id,
session_uuid,
url?.substr(0, URL_LENGTH),
event_type?.substr(0, 50),
event_value?.substr(0, 50),
];
return rawQueryClickhouse(
`
insert into umami_dev.event (created_at, website_id, session_uuid, url, event_type, event_value)
values (${getDateFormatClickhouse(new Date())}, $1, $2, $3, $4, $5);`,
params,
);
}