2023-03-23 22:01:15 +01:00
|
|
|
import clickhouse from 'lib/clickhouse';
|
|
|
|
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
|
|
|
import { WebsiteEventDataMetric } from 'lib/types';
|
2023-04-02 02:38:35 +02:00
|
|
|
import { loadWebsite } from 'lib/query';
|
2023-06-15 07:32:06 +02:00
|
|
|
import { DEFAULT_CREATED_AT } from 'lib/constants';
|
2023-07-10 13:35:19 +02:00
|
|
|
import prisma from '../../../lib/prisma';
|
2023-07-05 07:51:23 +02:00
|
|
|
|
|
|
|
export async function getEventData(
|
2023-07-10 13:35:19 +02:00
|
|
|
...args: [websiteId: string, startDate: Date, endDate: Date, field?: string]
|
2023-03-23 22:01:15 +01:00
|
|
|
): Promise<WebsiteEventDataMetric[]> {
|
|
|
|
return runQuery({
|
|
|
|
[PRISMA]: () => relationalQuery(...args),
|
|
|
|
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-10 13:35:19 +02:00
|
|
|
async function relationalQuery(websiteId: string, startDate: Date, endDate: Date, field: string) {
|
|
|
|
const { toUuid, rawQuery } = prisma;
|
2023-04-02 02:38:35 +02:00
|
|
|
const website = await loadWebsite(websiteId);
|
2023-06-15 07:32:06 +02:00
|
|
|
const resetDate = new Date(website?.resetAt || DEFAULT_CREATED_AT);
|
2023-03-23 22:01:15 +01:00
|
|
|
|
2023-07-10 13:35:19 +02:00
|
|
|
if (field) {
|
|
|
|
return rawQuery(
|
|
|
|
`select event_key as field,
|
|
|
|
count(*) as total
|
|
|
|
from event_data
|
|
|
|
where website_id = $1${toUuid()}
|
|
|
|
and event_key = $2
|
|
|
|
and created_at >= $3
|
|
|
|
and created_at between $4 and $5
|
|
|
|
group by event_key
|
|
|
|
order by 2 desc, 1 asc
|
|
|
|
limit 1000
|
|
|
|
`,
|
|
|
|
[websiteId, field, resetDate, startDate, endDate] as any,
|
|
|
|
);
|
|
|
|
}
|
2023-07-08 05:38:43 +02:00
|
|
|
|
2023-07-10 13:35:19 +02:00
|
|
|
return rawQuery(
|
|
|
|
`select
|
|
|
|
event_key as field,
|
|
|
|
count(*) as total
|
|
|
|
from event_data
|
|
|
|
where website_id = $1${toUuid()}
|
|
|
|
and created_at >= $2
|
|
|
|
and created_at between $3 and $4
|
|
|
|
group by event_key
|
|
|
|
order by 2 desc, 1 asc
|
|
|
|
limit 1000
|
|
|
|
`,
|
|
|
|
[websiteId, resetDate, startDate, endDate] as any,
|
|
|
|
);
|
|
|
|
}
|
2023-07-05 07:51:23 +02:00
|
|
|
|
2023-07-10 13:35:19 +02:00
|
|
|
async function clickhouseQuery(websiteId: string, startDate: Date, endDate: Date, field: string) {
|
|
|
|
const { rawQuery, getDateFormat, getBetweenDates } = clickhouse;
|
|
|
|
const website = await loadWebsite(websiteId);
|
|
|
|
const resetDate = new Date(website?.resetAt || DEFAULT_CREATED_AT);
|
2023-07-05 07:51:23 +02:00
|
|
|
|
2023-07-10 13:35:19 +02:00
|
|
|
if (field) {
|
|
|
|
return rawQuery(
|
|
|
|
`select
|
|
|
|
event_key as field,
|
|
|
|
count(*) as total
|
2023-07-05 07:51:23 +02:00
|
|
|
from event_data
|
|
|
|
where website_id = {websiteId:UUID}
|
2023-07-10 13:35:19 +02:00
|
|
|
and event_key = {field:String}
|
2023-07-05 07:51:23 +02:00
|
|
|
and created_at >= ${getDateFormat(resetDate)}
|
|
|
|
and ${getBetweenDates('created_at', startDate, endDate)}
|
2023-07-10 13:35:19 +02:00
|
|
|
group by event_key
|
|
|
|
order by 2 desc, 1 asc
|
|
|
|
limit 1000
|
2023-07-05 07:51:23 +02:00
|
|
|
`,
|
2023-07-10 13:35:19 +02:00
|
|
|
{ websiteId, field },
|
2023-07-05 07:51:23 +02:00
|
|
|
);
|
2023-07-10 13:35:19 +02:00
|
|
|
}
|
2023-07-05 07:51:23 +02:00
|
|
|
|
2023-07-10 13:35:19 +02:00
|
|
|
return rawQuery(
|
|
|
|
`select
|
|
|
|
event_key as field,
|
|
|
|
count(*) as total
|
|
|
|
from event_data
|
|
|
|
where website_id = {websiteId:UUID}
|
|
|
|
and created_at >= ${getDateFormat(resetDate)}
|
|
|
|
and ${getBetweenDates('created_at', startDate, endDate)}
|
|
|
|
group by event_key
|
|
|
|
order by 2 desc, 1 asc
|
|
|
|
limit 1000
|
|
|
|
`,
|
|
|
|
{ websiteId },
|
|
|
|
);
|
2023-03-23 22:01:15 +01:00
|
|
|
}
|