umami/queries/analytics/session/getSessions.js
2022-08-27 21:38:35 -07:00

53 lines
1.2 KiB
JavaScript

import prisma from 'lib/prisma';
import clickhouse from 'lib/clickhouse';
import { runQuery, PRISMA, CLICKHOUSE } from 'lib/db';
export async function getSessions(...args) {
return runQuery({
[PRISMA]: () => relationalQuery(...args),
[CLICKHOUSE]: () => clickhouseQuery(...args),
});
}
async function relationalQuery(websites, start_at) {
return runQuery(
prisma.client.session.findMany({
where: {
...(websites && websites.length > 0
? {
website: {
website_id: {
in: websites,
},
},
}
: {}),
created_at: {
gte: start_at,
},
},
}),
);
}
async function clickhouseQuery(websites, start_at) {
const { rawQuery, getDateFormat } = clickhouse;
return rawQuery(
`select
session_uuid,
website_id,
created_at,
hostname,
browser,
os,
device,
screen,
language,
country
from session
where ${websites && websites.length > 0 ? `(website_id in (${websites.join[',']})` : '0 = 0'}
and created_at >= ${getDateFormat(start_at)}`,
);
}