umami/queries/analytics/session/getSessions.js
Brian Cao 78338205a3
Feat/um 62 prisma property names (#1562)
* checkpoint

* fix pg schema

* fix mysql schema

* change property names
2022-10-10 13:42:18 -07:00

51 lines
1.1 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 prisma.client.session.findMany({
where: {
...(websites && websites.length > 0
? {
website: {
id: {
in: websites,
},
},
}
: {}),
createdAt: {
gte: start_at,
},
},
});
}
async function clickhouseQuery(websites, start_at) {
const { rawQuery, getDateFormat } = clickhouse;
return rawQuery(
`select distinct
session_uuid,
website_id,
created_at,
hostname,
browser,
os,
device,
screen,
language,
country
from event
where ${websites && websites.length > 0 ? `website_id in (${websites.join(',')})` : '0 = 0'}
and created_at >= ${getDateFormat(start_at)}`,
);
}