umami/queries/analytics/pageview/savePageView.js

55 lines
1.5 KiB
JavaScript
Raw Normal View History

2022-08-26 07:04:32 +02:00
import { CLICKHOUSE, KAFKA, RELATIONAL, URL_LENGTH } from 'lib/constants';
import clickhouse from 'lib/clickhouse';
2022-08-26 07:23:19 +02:00
import { runAnalyticsQuery } from 'lib/db';
import kafka from 'lib/kafka';
2022-08-26 07:20:30 +02:00
import { prisma, runQuery } from 'lib/relational';
2022-07-16 01:47:38 +02:00
2022-07-22 23:43:19 +02:00
export async function savePageView(...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-22 23:43:19 +02:00
async function relationalQuery(website_id, { session_id, url, referrer }) {
2022-07-12 23:14:36 +02:00
return runQuery(
prisma.pageview.create({
data: {
website_id,
session_id,
url: url?.substr(0, URL_LENGTH),
referrer: referrer?.substr(0, URL_LENGTH),
},
}),
);
}
2022-07-22 23:43:19 +02:00
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),
];
2022-08-26 07:04:32 +02:00
return clickhouse.rawQuery(
2022-07-22 23:43:19 +02:00
`
2022-08-09 07:09:18 +02:00
insert into umami.pageview (created_at, website_id, session_uuid, url, referrer)
2022-08-26 07:04:32 +02:00
values (${clickhouse.getDateFormat(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, { session_uuid, url, referrer }) {
const params = {
website_id: website_id,
session_uuid: session_uuid,
2022-08-26 07:04:32 +02:00
created_at: kafka.getDateFormat(new Date()),
2022-08-05 02:43:47 +02:00
url: url?.substr(0, URL_LENGTH),
referrer: referrer?.substr(0, URL_LENGTH),
};
await kafka.sendMessage(params, 'pageview');
2022-08-05 02:43:47 +02:00
}