2022-07-22 23:43:19 +02:00
|
|
|
import { CLICKHOUSE, RELATIONAL } from 'lib/constants';
|
|
|
|
import {
|
|
|
|
getDateFormatClickhouse,
|
|
|
|
prisma,
|
|
|
|
rawQueryClickhouse,
|
|
|
|
runAnalyticsQuery,
|
|
|
|
runQuery,
|
2022-08-01 09:28:38 +02:00
|
|
|
kafkaProducer,
|
2022-07-22 23:43:19 +02:00
|
|
|
} from 'lib/db';
|
|
|
|
import { getSessionByUuid } from 'queries';
|
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-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-01 09:28:38 +02:00
|
|
|
const params = {
|
|
|
|
session_uuid: session_uuid,
|
|
|
|
website_id: website_id,
|
|
|
|
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-01 09:28:38 +02:00
|
|
|
if (process.env.KAFKA_URL) {
|
|
|
|
await kafkaProducer(params, 'session');
|
|
|
|
} else {
|
|
|
|
const paramsValue = Object.keys(params);
|
|
|
|
|
|
|
|
await rawQueryClickhouse(
|
|
|
|
`insert into umami_dev.session (created_at, session_uuid, website_id, hostname, browser, os, device, screen, language, country)
|
|
|
|
values (${getDateFormatClickhouse(new Date())}, $1, $2, $3, $4, $5, $6, $7, $8, $9);`,
|
|
|
|
paramsValue,
|
|
|
|
);
|
|
|
|
}
|
2022-07-22 23:43:19 +02:00
|
|
|
|
|
|
|
return getSessionByUuid(session_uuid);
|
|
|
|
}
|