umami/queries/analytics/session/getSessionByUuid.js

58 lines
1.2 KiB
JavaScript
Raw Normal View History

2022-08-26 07:04:32 +02:00
import clickhouse from 'lib/clickhouse';
2022-08-29 19:47:01 +02:00
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
import prisma from 'lib/prisma';
import redis from 'lib/redis';
2022-07-12 23:14:36 +02:00
2022-07-21 06:31:26 +02:00
export async function getSessionByUuid(...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-21 06:31:26 +02:00
});
}
async function relationalQuery(sessionUuid) {
2022-08-29 19:47:01 +02:00
return prisma.client.session
.findUnique({
where: {
sessionUuid,
2022-08-29 19:47:01 +02:00
},
})
.then(async res => {
2022-10-26 05:17:13 +02:00
if (redis.enabled && res) {
await redis.set(`session:${res.sessionUuid}`, 1);
2022-08-29 19:47:01 +02:00
}
return res;
});
2022-07-12 23:14:36 +02:00
}
2022-07-21 06:31:26 +02:00
async function clickhouseQuery(sessionUuid) {
2022-08-28 06:38:35 +02:00
const { rawQuery, findFirst } = clickhouse;
const params = [sessionUuid];
2022-07-21 06:31:26 +02:00
2022-08-28 06:38:35 +02:00
return rawQuery(
2022-09-12 18:55:34 +02:00
`select distinct
2022-10-12 08:09:06 +02:00
session_id,
2022-07-21 06:31:26 +02:00
website_id,
created_at,
hostname,
browser,
os,
device,
screen,
2022-08-28 06:38:35 +02:00
language,
2022-07-21 06:31:26 +02:00
country
2022-09-12 18:55:34 +02:00
from event
2022-10-12 08:09:06 +02:00
where session_id = $1`,
2022-08-28 06:38:35 +02:00
params,
2022-08-29 19:47:01 +02:00
)
.then(result => findFirst(result))
.then(async res => {
2022-10-26 05:17:13 +02:00
if (redis.enabled && res) {
await redis.set(`session:${res.session_uuid}`, 1);
2022-08-29 19:47:01 +02:00
}
return res;
});
2022-07-21 06:31:26 +02:00
}