2022-12-13 04:45:38 +01:00
|
|
|
import prisma from 'lib/prisma';
|
|
|
|
import clickhouse from 'lib/clickhouse';
|
|
|
|
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
|
|
|
|
import { WebsiteEventMetric } from 'lib/types';
|
2023-07-23 22:18:01 +02:00
|
|
|
import { DEFAULT_RESET_DATE, EVENT_TYPE } from 'lib/constants';
|
2023-04-02 02:38:35 +02:00
|
|
|
import { loadWebsite } from 'lib/query';
|
2022-12-13 04:45:38 +01:00
|
|
|
|
|
|
|
export async function getEventMetrics(
|
|
|
|
...args: [
|
|
|
|
websiteId: string,
|
|
|
|
data: {
|
|
|
|
startDate: Date;
|
|
|
|
endDate: Date;
|
|
|
|
timezone: string;
|
|
|
|
unit: string;
|
|
|
|
filters: {
|
|
|
|
url: string;
|
|
|
|
eventName: string;
|
|
|
|
};
|
|
|
|
},
|
|
|
|
]
|
|
|
|
): Promise<WebsiteEventMetric[]> {
|
|
|
|
return runQuery({
|
|
|
|
[PRISMA]: () => relationalQuery(...args),
|
|
|
|
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function relationalQuery(
|
|
|
|
websiteId: string,
|
|
|
|
{
|
|
|
|
startDate,
|
|
|
|
endDate,
|
|
|
|
timezone = 'utc',
|
|
|
|
unit = 'day',
|
|
|
|
filters,
|
|
|
|
}: {
|
|
|
|
startDate: Date;
|
|
|
|
endDate: Date;
|
|
|
|
timezone: string;
|
|
|
|
unit: string;
|
|
|
|
filters: {
|
|
|
|
url: string;
|
|
|
|
eventName: string;
|
|
|
|
};
|
|
|
|
},
|
|
|
|
) {
|
2023-07-25 08:06:16 +02:00
|
|
|
const { rawQuery, getDateQuery, getFilterQuery } = prisma;
|
2023-04-02 02:38:35 +02:00
|
|
|
const website = await loadWebsite(websiteId);
|
2023-07-23 22:18:01 +02:00
|
|
|
const resetDate = new Date(website?.resetAt || DEFAULT_RESET_DATE);
|
2023-07-25 08:06:16 +02:00
|
|
|
const filterQuery = getFilterQuery(filters);
|
2022-12-13 04:45:38 +01:00
|
|
|
|
|
|
|
return rawQuery(
|
2023-07-25 08:06:16 +02:00
|
|
|
`
|
|
|
|
select
|
2022-12-13 04:45:38 +01:00
|
|
|
event_name x,
|
|
|
|
${getDateQuery('created_at', unit, timezone)} t,
|
|
|
|
count(*) y
|
|
|
|
from website_event
|
2023-07-25 08:06:16 +02:00
|
|
|
where website_id = {{websiteId::uuid}}
|
|
|
|
and created_at >= {{resetDate}}
|
|
|
|
and created_at between {{startDate}} and {{endDate}}
|
2022-12-13 04:45:38 +01:00
|
|
|
and event_type = ${EVENT_TYPE.customEvent}
|
2023-04-02 00:44:30 +02:00
|
|
|
${filterQuery}
|
2022-12-13 04:45:38 +01:00
|
|
|
group by 1, 2
|
2023-07-25 08:06:16 +02:00
|
|
|
order by 2
|
|
|
|
`,
|
|
|
|
{ ...filters, websiteId, resetDate, startDate, endDate },
|
2022-12-13 04:45:38 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function clickhouseQuery(
|
|
|
|
websiteId: string,
|
|
|
|
{
|
|
|
|
startDate,
|
|
|
|
endDate,
|
|
|
|
timezone = 'utc',
|
|
|
|
unit = 'day',
|
|
|
|
filters,
|
|
|
|
}: {
|
|
|
|
startDate: Date;
|
|
|
|
endDate: Date;
|
|
|
|
timezone: string;
|
|
|
|
unit: string;
|
|
|
|
filters: {
|
|
|
|
url: string;
|
|
|
|
eventName: string;
|
|
|
|
};
|
|
|
|
},
|
|
|
|
) {
|
2023-07-25 08:06:16 +02:00
|
|
|
const { rawQuery, getDateQuery, getFilterQuery } = clickhouse;
|
2023-04-02 02:38:35 +02:00
|
|
|
const website = await loadWebsite(websiteId);
|
2023-07-23 22:18:01 +02:00
|
|
|
const resetDate = new Date(website?.resetAt || DEFAULT_RESET_DATE);
|
2023-07-25 08:06:16 +02:00
|
|
|
const filterQuery = getFilterQuery(filters);
|
2022-12-13 04:45:38 +01:00
|
|
|
|
|
|
|
return rawQuery(
|
2023-07-25 08:06:16 +02:00
|
|
|
`
|
|
|
|
select
|
2022-12-13 04:45:38 +01:00
|
|
|
event_name x,
|
|
|
|
${getDateQuery('created_at', unit, timezone)} t,
|
|
|
|
count(*) y
|
2023-03-29 20:06:12 +02:00
|
|
|
from website_event
|
2023-01-12 09:02:12 +01:00
|
|
|
where website_id = {websiteId:UUID}
|
2022-12-13 04:45:38 +01:00
|
|
|
and event_type = ${EVENT_TYPE.customEvent}
|
2023-07-25 08:06:16 +02:00
|
|
|
and created_at >= {resetDate:DateTIme}
|
|
|
|
and created_at between {startDate:DateTime} and {endDate:DateTime}
|
|
|
|
${filterQuery}
|
2022-12-13 04:45:38 +01:00
|
|
|
group by x, t
|
2023-07-25 08:06:16 +02:00
|
|
|
order by t
|
|
|
|
`,
|
|
|
|
{ ...filters, websiteId, resetDate, startDate, endDate },
|
2022-12-13 04:45:38 +01:00
|
|
|
);
|
|
|
|
}
|