umami/queries/analytics/pageview/savePageView.js

46 lines
1.2 KiB
JavaScript
Raw Normal View History

2022-08-28 06:38:35 +02:00
import { URL_LENGTH } from 'lib/constants';
2022-08-29 19:47:01 +02:00
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
import kafka from 'lib/kafka';
import prisma from 'lib/prisma';
import cache from 'lib/cache';
import { uuid } from 'lib/crypto';
2022-07-16 01:47:38 +02:00
2022-07-22 23:43:19 +02:00
export async function savePageView(...args) {
2022-08-28 06:38:35 +02:00
return runQuery({
[PRISMA]: () => relationalQuery(...args),
2022-07-25 18:47:11 +02:00
[CLICKHOUSE]: () => clickhouseQuery(...args),
2022-07-22 23:43:19 +02:00
});
}
2022-07-12 23:14:36 +02:00
async function relationalQuery(data) {
const { websiteId, sessionId, url, referrer } = data;
2022-08-28 06:38:35 +02:00
return prisma.client.pageview.create({
data: {
id: uuid(),
websiteId,
sessionId,
2022-08-28 06:38:35 +02:00
url: url?.substring(0, URL_LENGTH),
referrer: referrer?.substring(0, URL_LENGTH),
},
});
2022-07-12 23:14:36 +02:00
}
2022-07-22 23:43:19 +02:00
async function clickhouseQuery(data) {
const { websiteId, sessionId, url, referrer } = data;
const website = await cache.fetchWebsite(websiteId);
2022-08-28 06:38:35 +02:00
const { getDateFormat, sendMessage } = kafka;
const msg = {
2022-11-01 07:42:37 +01:00
session_id: sessionId,
website_id: websiteId,
2022-08-28 06:38:35 +02:00
url: url?.substring(0, URL_LENGTH),
referrer: referrer?.substring(0, URL_LENGTH),
rev_id: website?.revId || 0,
created_at: getDateFormat(new Date()),
2022-08-05 02:43:47 +02:00
};
await sendMessage(msg, 'event');
return data;
2022-08-05 02:43:47 +02:00
}