2023-07-26 00:17:50 +02:00
|
|
|
import prisma from 'lib/prisma';
|
|
|
|
import clickhouse from 'lib/clickhouse';
|
|
|
|
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
2023-07-26 08:59:08 +02:00
|
|
|
import { loadWebsite } from 'lib/load';
|
2023-07-26 00:17:50 +02:00
|
|
|
import { DEFAULT_RESET_DATE } from 'lib/constants';
|
2023-07-26 08:59:08 +02:00
|
|
|
import { maxDate } from 'lib/date';
|
2023-07-26 00:17:50 +02:00
|
|
|
|
|
|
|
export async function getWebsiteDateRange(...args: [websiteId: string]) {
|
|
|
|
return runQuery({
|
|
|
|
[PRISMA]: () => relationalQuery(...args),
|
|
|
|
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function relationalQuery(websiteId: string) {
|
|
|
|
const { rawQuery } = prisma;
|
2023-07-26 18:55:54 +02:00
|
|
|
const website = await loadWebsite(websiteId);
|
2023-07-26 00:17:50 +02:00
|
|
|
|
2023-07-26 22:42:55 +02:00
|
|
|
const result = await rawQuery(
|
2023-07-26 00:17:50 +02:00
|
|
|
`
|
|
|
|
select
|
2023-07-26 22:42:55 +02:00
|
|
|
min(created_at) as mindate,
|
|
|
|
max(created_at) as maxdate
|
2023-07-26 00:17:50 +02:00
|
|
|
from website_event
|
2023-07-26 18:55:54 +02:00
|
|
|
where website_id = {{websiteId::uuid}}
|
|
|
|
and created_at >= {{startDate}}
|
2023-07-26 00:17:50 +02:00
|
|
|
`,
|
2023-07-26 18:55:54 +02:00
|
|
|
{ websiteId, startDate: maxDate(new Date(DEFAULT_RESET_DATE), new Date(website.resetAt)) },
|
2023-07-26 00:17:50 +02:00
|
|
|
);
|
2023-07-26 22:42:55 +02:00
|
|
|
|
|
|
|
return result[0] ?? null;
|
2023-07-26 00:17:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async function clickhouseQuery(websiteId: string) {
|
|
|
|
const { rawQuery } = clickhouse;
|
|
|
|
const website = await loadWebsite(websiteId);
|
|
|
|
|
2023-07-26 22:42:55 +02:00
|
|
|
const result = await rawQuery(
|
2023-07-26 00:17:50 +02:00
|
|
|
`
|
|
|
|
select
|
2023-07-26 22:42:55 +02:00
|
|
|
min(created_at) as mindate,
|
|
|
|
max(created_at) as maxdate
|
2023-07-26 00:17:50 +02:00
|
|
|
from website_event
|
|
|
|
where website_id = {websiteId:UUID}
|
2023-07-26 07:50:55 +02:00
|
|
|
and created_at >= {startDate:DateTime}
|
2023-07-26 00:17:50 +02:00
|
|
|
`,
|
2023-07-26 18:55:54 +02:00
|
|
|
{ websiteId, startDate: maxDate(new Date(DEFAULT_RESET_DATE), new Date(website.resetAt)) },
|
2023-07-26 00:17:50 +02:00
|
|
|
);
|
2023-07-26 22:42:55 +02:00
|
|
|
|
|
|
|
return result[0] ?? null;
|
2023-07-26 00:17:50 +02:00
|
|
|
}
|