umami/queries/analytics/eventData/getEventDataFields.ts

189 lines
5.5 KiB
TypeScript
Raw Normal View History

2023-07-11 10:16:17 +02:00
import prisma from 'lib/prisma';
2023-06-03 08:10:59 +02:00
import clickhouse from 'lib/clickhouse';
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
2023-07-11 10:16:17 +02:00
import { WebsiteEventDataFields } from 'lib/types';
2023-06-03 08:10:59 +02:00
import { loadWebsite } from 'lib/query';
2023-06-15 07:32:06 +02:00
import { DEFAULT_CREATED_AT } from 'lib/constants';
2023-06-03 08:10:59 +02:00
export async function getEventDataFields(
...args: [
websiteId: string,
startDate: Date,
endDate: Date,
field?: string,
2023-07-14 17:37:14 +02:00
event?: string,
withEventNames?: boolean,
]
2023-07-11 10:16:17 +02:00
): Promise<WebsiteEventDataFields[]> {
2023-06-03 08:10:59 +02:00
return runQuery({
[PRISMA]: () => relationalQuery(...args),
[CLICKHOUSE]: () => clickhouseQuery(...args),
});
}
async function relationalQuery(
websiteId: string,
startDate: Date,
endDate: Date,
field: string,
2023-07-14 17:37:14 +02:00
event: string,
withEventNames: boolean,
) {
2023-06-03 08:10:59 +02:00
const { toUuid, rawQuery } = prisma;
const website = await loadWebsite(websiteId);
2023-06-15 07:32:06 +02:00
const resetDate = new Date(website?.resetAt || DEFAULT_CREATED_AT);
2023-07-11 10:16:17 +02:00
if (field) {
2023-07-14 17:37:14 +02:00
if (event) {
return rawQuery(
`select ed.event_key as field,
ed.string_value as value,
count(ed.*) as total
from event_data as ed
join website_event as e on e.event_id = ed.website_event_id
where ed.website_id = $1${toUuid()}
and ed.event_key = $2
and ed.created_at >= $3
and ed.created_at between $4 and $5
and e.event_name = $6
group by ed.event_key, ed.string_value
order by 3 desc, 2 desc, 1 asc
`,
[websiteId, field, resetDate, startDate, endDate, event] as any,
);
}
2023-07-11 10:16:17 +02:00
return rawQuery(
`select event_key as field,
string_value as value,
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, string_value
order by 3 desc, 2 desc, 1 asc
`,
[websiteId, field, resetDate, startDate, endDate] as any,
);
}
2023-06-03 08:10:59 +02:00
if (withEventNames) {
return rawQuery(
`select
ed.event_key as field,
ed.data_type as type,
count(ed.*) as total,
e.event_name as event
from event_data as ed
join website_event as e on e.event_id = ed.website_event_id
where ed.website_id = $1${toUuid()}
and ed.created_at >= $2
and ed.created_at between $3 and $4
group by e.event_name, ed.event_key, ed.data_type
order by 3 desc, 2 asc, 1 asc
`,
[websiteId, resetDate, startDate, endDate] as any,
);
}
2023-06-03 08:10:59 +02:00
return rawQuery(
`select
2023-07-11 10:16:17 +02:00
event_key as field,
data_type as type,
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, data_type
order by 3 desc, 2 asc, 1 asc
`,
[websiteId, resetDate, startDate, endDate] as any,
2023-06-03 08:10:59 +02:00
);
}
async function clickhouseQuery(
websiteId: string,
startDate: Date,
endDate: Date,
field: string,
2023-07-14 17:37:14 +02:00
event: string,
withEventNames: boolean,
) {
2023-06-03 08:10:59 +02:00
const { rawQuery, getDateFormat, getBetweenDates } = clickhouse;
const website = await loadWebsite(websiteId);
2023-06-15 07:32:06 +02:00
const resetDate = new Date(website?.resetAt || DEFAULT_CREATED_AT);
2023-07-11 10:16:17 +02:00
if (field) {
2023-07-14 17:37:14 +02:00
if (event) {
return rawQuery(
`select
ed.event_key as field,
ed.string_value as value,
count(ed.*) as total
from event_data as ed
join website_event as e on e.event_id = ed.website_event_id
where ed.website_id = {websiteId:UUID}
and ed.event_key = {field:String}
and ed.created_at >= ${getDateFormat(resetDate)}
and ${getBetweenDates('ed.created_at', startDate, endDate)}
and e.event_name = {event:String}
group by event_key, string_value
order by 3 desc, 2 desc, 1 asc
`,
{ websiteId, field, event },
);
}
2023-07-11 10:16:17 +02:00
return rawQuery(
`select
event_key as field,
string_value as value,
count(*) as total
from event_data
where website_id = {websiteId:UUID}
and event_key = {field:String}
and created_at >= ${getDateFormat(resetDate)}
and ${getBetweenDates('created_at', startDate, endDate)}
group by event_key, string_value
order by 3 desc, 2 desc, 1 asc
`,
{ websiteId, field },
);
}
2023-06-03 08:10:59 +02:00
if (withEventNames) {
return rawQuery(
`select
ed.event_key as field,
ed.data_type as type,
count(ed.*) as total,
e.event_name as event
from event_data as ed
join website_event as e on e.event_id = ed.website_event_id
2023-07-14 17:37:14 +02:00
where ed.website_id = {websiteId:UUID}
and ed.created_at >= ${getDateFormat(resetDate)}
and ${getBetweenDates('ed.created_at', startDate, endDate)}
group by e.event_name, ed.event_key, ed.data_type
order by 3 desc, 2 asc, 1 asc
`,
[websiteId, resetDate, startDate, endDate] as any,
);
}
2023-06-03 08:10:59 +02:00
return rawQuery(
`select
2023-07-11 10:16:17 +02:00
event_key as field,
data_type as type,
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, data_type
order by 3 desc, 2 asc, 1 asc
`,
{ websiteId },
2023-06-03 08:10:59 +02:00
);
}