mirror of
https://github.com/kremalicious/umami.git
synced 2024-11-15 17:55:08 +01:00
59 lines
1.5 KiB
JavaScript
59 lines
1.5 KiB
JavaScript
import { CLICKHOUSE, RELATIONAL, KAFKA, URL_LENGTH } from 'lib/constants';
|
|
import {
|
|
getDateFormatClickhouse,
|
|
prisma,
|
|
rawQueryClickhouse,
|
|
runAnalyticsQuery,
|
|
runQuery,
|
|
} from 'lib/db';
|
|
import { kafkaProducer, getDateFormatKafka } from 'lib/kafka';
|
|
|
|
export async function savePageView(...args) {
|
|
return runAnalyticsQuery({
|
|
[RELATIONAL]: () => relationalQuery(...args),
|
|
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
|
[KAFKA]: () => kafkaQuery(...args),
|
|
});
|
|
}
|
|
|
|
async function relationalQuery(website_id, { session_id, url, referrer }) {
|
|
return runQuery(
|
|
prisma.pageview.create({
|
|
data: {
|
|
website_id,
|
|
session_id,
|
|
url: url?.substr(0, URL_LENGTH),
|
|
referrer: referrer?.substr(0, URL_LENGTH),
|
|
},
|
|
}),
|
|
);
|
|
}
|
|
|
|
async function clickhouseQuery(website_id, { session_uuid, url, referrer }) {
|
|
const params = [
|
|
website_id,
|
|
session_uuid,
|
|
url?.substr(0, URL_LENGTH),
|
|
referrer?.substr(0, URL_LENGTH),
|
|
];
|
|
|
|
return rawQueryClickhouse(
|
|
`
|
|
insert into umami.pageview (created_at, website_id, session_uuid, url, referrer)
|
|
values (${getDateFormatClickhouse(new Date())}, $1, $2, $3, $4);`,
|
|
params,
|
|
);
|
|
}
|
|
|
|
async function kafkaQuery(website_id, { session_uuid, url, referrer }) {
|
|
const params = {
|
|
website_id: website_id,
|
|
session_uuid: session_uuid,
|
|
created_at: getDateFormatKafka(new Date()),
|
|
url: url?.substr(0, URL_LENGTH),
|
|
referrer: referrer?.substr(0, URL_LENGTH),
|
|
};
|
|
|
|
await kafkaProducer(params, 'pageview');
|
|
}
|