mirror of
https://github.com/kremalicious/umami.git
synced 2025-02-07 09:55:45 +01:00
49 lines
1.3 KiB
TypeScript
49 lines
1.3 KiB
TypeScript
import prisma from 'lib/prisma';
|
|
import clickhouse from 'lib/clickhouse';
|
|
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
|
import { loadWebsite } from 'lib/query';
|
|
import { DEFAULT_RESET_DATE } from 'lib/constants';
|
|
import { max } from 'date-fns';
|
|
|
|
export async function getWebsiteDateRange(...args: [websiteId: string]) {
|
|
return runQuery({
|
|
[PRISMA]: () => relationalQuery(...args),
|
|
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
|
});
|
|
}
|
|
|
|
async function relationalQuery(websiteId: string) {
|
|
const { rawQuery } = prisma;
|
|
|
|
return rawQuery(
|
|
`
|
|
select
|
|
min(created_at) as min,
|
|
max(created_at) as max
|
|
from website_event
|
|
join website
|
|
on website_event.website_id = website.website_id
|
|
where website.website_id = {{websiteId::uuid}}
|
|
and website_event.created_at >= coalesce(website.reset_at, website.created_at)
|
|
`,
|
|
{ websiteId },
|
|
);
|
|
}
|
|
|
|
async function clickhouseQuery(websiteId: string) {
|
|
const { rawQuery } = clickhouse;
|
|
const website = await loadWebsite(websiteId);
|
|
|
|
return rawQuery(
|
|
`
|
|
select
|
|
min(created_at) as min,
|
|
max(created_at) as max
|
|
from website_event
|
|
where website_id = {websiteId:UUID}
|
|
and created_at >= {startDate:DateTime}
|
|
`,
|
|
{ websiteId, startDate: max([new Date('2000-01-01'), website.resetAt]) },
|
|
);
|
|
}
|