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(session_uuid) {
2022-08-29 19:47:01 +02:00
return prisma.client.session
.findUnique({
where: {
session_uuid,
},
})
.then(async res => {
2022-08-29 22:04:58 +02:00
if (redis.client && res) {
await redis.client.set(`session:${res.session_uuid}`, res.session_id);
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(session_uuid) {
2022-08-28 06:38:35 +02:00
const { rawQuery, findFirst } = clickhouse;
2022-07-21 06:31:26 +02:00
const params = [session_uuid];
2022-08-28 06:38:35 +02:00
return rawQuery(
2022-09-12 18:55:34 +02:00
`select distinct
2022-07-21 06:31:26 +02:00
session_uuid,
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-08-28 06:38:35 +02:00
where session_uuid = $1`,
params,
2022-08-29 19:47:01 +02:00
)
.then(result => findFirst(result))
.then(async res => {
2022-08-29 22:04:58 +02:00
if (redis.client && res) {
2022-08-29 19:47:01 +02:00
await redis.client.set(`session:${res.session_uuid}`, 1);
}
return res;
});
2022-07-21 06:31:26 +02:00
}