2023-03-23 22:01:15 +01:00
|
|
|
import clickhouse from 'lib/clickhouse';
|
|
|
|
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
|
|
|
|
import prisma from 'lib/prisma';
|
|
|
|
import { WebsiteEventDataMetric } from 'lib/types';
|
2023-04-02 02:38:35 +02:00
|
|
|
import { loadWebsite } from 'lib/query';
|
2023-03-23 22:01:15 +01:00
|
|
|
|
|
|
|
export async function getEventData(
|
|
|
|
...args: [
|
|
|
|
websiteId: string,
|
|
|
|
data: {
|
|
|
|
startDate: Date;
|
|
|
|
endDate: Date;
|
|
|
|
eventName: string;
|
|
|
|
urlPath?: string;
|
|
|
|
filters: [
|
|
|
|
{
|
|
|
|
eventKey?: string;
|
|
|
|
eventValue?: string | number | boolean | Date;
|
|
|
|
},
|
|
|
|
];
|
|
|
|
},
|
|
|
|
]
|
|
|
|
): Promise<WebsiteEventDataMetric[]> {
|
|
|
|
return runQuery({
|
|
|
|
[PRISMA]: () => relationalQuery(...args),
|
|
|
|
[CLICKHOUSE]: () => clickhouseQuery(...args),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function relationalQuery(
|
|
|
|
websiteId: string,
|
|
|
|
data: {
|
|
|
|
startDate: Date;
|
|
|
|
endDate: Date;
|
|
|
|
timeSeries?: {
|
|
|
|
unit: string;
|
|
|
|
timezone: string;
|
|
|
|
};
|
|
|
|
eventName: string;
|
|
|
|
urlPath?: string;
|
|
|
|
filters: [
|
|
|
|
{
|
|
|
|
eventKey?: string;
|
|
|
|
eventValue?: string | number | boolean | Date;
|
|
|
|
},
|
|
|
|
];
|
|
|
|
},
|
|
|
|
) {
|
|
|
|
const { startDate, endDate, timeSeries, eventName, urlPath, filters } = data;
|
|
|
|
const { toUuid, rawQuery, getEventDataFilterQuery, getDateQuery } = prisma;
|
2023-04-02 02:38:35 +02:00
|
|
|
const website = await loadWebsite(websiteId);
|
2023-03-27 20:25:16 +02:00
|
|
|
const resetDate = website?.resetAt || website?.createdAt;
|
|
|
|
const params: any = [websiteId, resetDate, startDate, endDate, eventName || ''];
|
2023-03-23 22:01:15 +01:00
|
|
|
|
|
|
|
return rawQuery(
|
|
|
|
`select
|
|
|
|
count(*) x
|
|
|
|
${eventName ? `,event_name eventName` : ''}
|
|
|
|
${urlPath ? `,url_path urlPath` : ''}
|
|
|
|
${
|
|
|
|
timeSeries ? `,${getDateQuery('created_at', timeSeries.unit, timeSeries.timezone)} t` : ''
|
|
|
|
}
|
|
|
|
from event_data
|
|
|
|
${
|
|
|
|
eventName || urlPath
|
|
|
|
? 'join website_event on event_data.id = website_event.website_event_id'
|
|
|
|
: ''
|
|
|
|
}
|
|
|
|
where website_id = $1${toUuid()}
|
2023-03-27 20:25:16 +02:00
|
|
|
and created_at >= $2
|
|
|
|
and created_at between $3 and $4
|
|
|
|
${eventName ? `and eventName = $5` : ''}
|
2023-03-23 22:01:15 +01:00
|
|
|
${getEventDataFilterQuery(filters, params)}
|
|
|
|
${timeSeries ? 'group by t' : ''}`,
|
|
|
|
params,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function clickhouseQuery(
|
|
|
|
websiteId: string,
|
|
|
|
data: {
|
|
|
|
startDate: Date;
|
|
|
|
endDate: Date;
|
|
|
|
timeSeries?: {
|
|
|
|
unit: string;
|
|
|
|
timezone: string;
|
|
|
|
};
|
|
|
|
eventName?: string;
|
|
|
|
urlPath?: string;
|
|
|
|
filters: [
|
|
|
|
{
|
|
|
|
eventKey?: string;
|
|
|
|
eventValue?: string | number | boolean | Date;
|
|
|
|
},
|
|
|
|
];
|
|
|
|
},
|
|
|
|
) {
|
|
|
|
const { startDate, endDate, timeSeries, eventName, urlPath, filters } = data;
|
2023-03-27 20:25:16 +02:00
|
|
|
const { rawQuery, getDateFormat, getBetweenDates, getDateQuery, getEventDataFilterQuery } =
|
|
|
|
clickhouse;
|
2023-04-02 02:38:35 +02:00
|
|
|
const website = await loadWebsite(websiteId);
|
2023-03-27 20:25:16 +02:00
|
|
|
const resetDate = website?.resetAt || website?.createdAt;
|
|
|
|
const params = { websiteId };
|
2023-03-23 22:01:15 +01:00
|
|
|
|
|
|
|
return rawQuery(
|
|
|
|
`select
|
|
|
|
count(*) x
|
|
|
|
${eventName ? `,event_name eventName` : ''}
|
|
|
|
${urlPath ? `,url_path urlPath` : ''}
|
|
|
|
${
|
|
|
|
timeSeries ? `,${getDateQuery('created_at', timeSeries.unit, timeSeries.timezone)} t` : ''
|
|
|
|
}
|
|
|
|
from event_data
|
|
|
|
where website_id = {websiteId:UUID}
|
|
|
|
${eventName ? `and eventName = ${eventName}` : ''}
|
2023-03-27 20:25:16 +02:00
|
|
|
and created_at >= ${getDateFormat(resetDate)}
|
2023-03-23 22:01:15 +01:00
|
|
|
and ${getBetweenDates('created_at', startDate, endDate)}
|
|
|
|
${getEventDataFilterQuery(filters, params)}
|
|
|
|
${timeSeries ? 'group by t' : ''}`,
|
|
|
|
params,
|
|
|
|
);
|
|
|
|
}
|