convert analytics queries

This commit is contained in:
Brian Cao 2022-07-20 21:31:26 -07:00
parent 4f12933f42
commit 6ea2282f82
12 changed files with 470 additions and 52 deletions

View file

@ -1,6 +1,20 @@
import { prisma, runQuery } from 'lib/db';
import { CLICKHOUSE, RELATIONAL } from 'lib/constants';
import {
getDateFormatClickhouse,
prisma,
rawQueryClickhouse,
runAnalyticsQuery,
runQuery,
} from 'lib/db';
export async function getSessions(websites, start_at) {
export async function getSessions(...args) {
return runAnalyticsQuery({
[`${RELATIONAL}`]: () => relationalQuery(...args),
[`${CLICKHOUSE}`]: () => clickhouseQuery(...args),
});
}
async function relationalQuery(websites, start_at) {
return runQuery(
prisma.session.findMany({
where: {
@ -16,3 +30,25 @@ export async function getSessions(websites, start_at) {
}),
);
}
async function clickhouseQuery(websites, start_at) {
return rawQueryClickhouse(
`
select
session_id,
session_uuid,
website_id,
created_at,
hostname,
browser,
os,
device,
screen,
"language",
country
from session
where website_id in (${websites.join[',']}
and created_at >= ${getDateFormatClickhouse(start_at)})
`,
);
}