umami/queries/analytics/session/getSession.js

40 lines
825 B
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';
2022-07-12 23:14:36 +02:00
2022-11-01 07:42:37 +01:00
export async function getSession(...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
});
}
2022-11-01 07:42:37 +01:00
async function relationalQuery(where) {
return prisma.client.session.findUnique({
where,
});
2022-07-12 23:14:36 +02:00
}
2022-07-21 06:31:26 +02:00
async function clickhouseQuery(sessionId) {
2022-08-28 06:38:35 +02:00
const { rawQuery, findFirst } = clickhouse;
const params = [sessionId];
2022-07-21 06:31:26 +02:00
2022-08-28 06:38:35 +02:00
return rawQuery(
`select
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
where session_id = $1
limit 1`,
2022-08-28 06:38:35 +02:00
params,
).then(result => findFirst(result));
2022-07-21 06:31:26 +02:00
}