umami/queries/analytics/session/createSession.js

71 lines
1.7 KiB
JavaScript
Raw Normal View History

2022-08-26 07:04:32 +02:00
import { CLICKHOUSE, KAFKA, RELATIONAL } from 'lib/constants';
2022-08-26 07:20:30 +02:00
import { prisma, runQuery } from 'lib/relational';
2022-08-26 07:04:32 +02:00
import clickhouse from 'lib/clickhouse';
2022-08-26 07:23:19 +02:00
import kafka from 'lib/kafka';
import { runAnalyticsQuery } from 'lib/db';
2022-07-12 23:14:36 +02:00
2022-07-22 23:43:19 +02:00
export async function createSession(...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
});
}
async function relationalQuery(website_id, data) {
2022-07-12 23:14:36 +02:00
return runQuery(
prisma.session.create({
data: {
website_id,
...data,
},
select: {
session_id: true,
},
}),
);
}
2022-07-22 23:43:19 +02:00
async function clickhouseQuery(
website_id,
{ session_uuid, hostname, browser, os, screen, language, country, device },
2022-08-05 02:43:47 +02:00
) {
const params = [
session_uuid,
website_id,
hostname,
browser,
os,
device,
screen,
language,
country ? country : null,
];
2022-08-26 07:04:32 +02:00
await clickhouse.rawQuery(
2022-08-09 07:09:18 +02:00
`insert into umami.session (created_at, session_uuid, website_id, hostname, browser, os, device, screen, language, country)
2022-08-26 07:04:32 +02:00
values (${clickhouse.getDateFormat(new Date())}, $1, $2, $3, $4, $5, $6, $7, $8, $9);`,
2022-08-05 02:43:47 +02:00
params,
);
}
async function kafkaQuery(
website_id,
{ session_uuid, hostname, browser, os, screen, language, country, device },
2022-07-22 23:43:19 +02:00
) {
2022-08-01 09:28:38 +02:00
const params = {
session_uuid: session_uuid,
website_id: website_id,
2022-08-26 07:04:32 +02:00
created_at: kafka.getDateFormat(new Date()),
2022-08-01 09:28:38 +02:00
hostname: hostname,
browser: browser,
os: os,
device: device,
screen: screen,
language: language,
country: country ? country : null,
};
2022-07-22 23:43:19 +02:00
2022-08-26 07:04:32 +02:00
await kafka.sendKafkaMessage(params, 'session');
2022-07-22 23:43:19 +02:00
}