umami/queries/analytics/eventData/getEventDataFields.ts

96 lines
2.7 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-07-26 08:59:08 +02:00
import { loadWebsite } from 'lib/load';
import { maxDate } from 'lib/date';
2023-06-03 08:10:59 +02:00
export async function getEventDataFields(
...args: [websiteId: string, startDate: Date, endDate: Date, field?: string]
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-25 08:06:16 +02:00
const { rawQuery } = prisma;
2023-06-03 08:10:59 +02:00
const website = await loadWebsite(websiteId);
2023-07-11 10:16:17 +02:00
if (field) {
return rawQuery(
2023-07-25 08:06:16 +02:00
`
select
event_key as field,
string_value as value,
count(*) as total
from event_data
where website_id = {{websiteId::uuid}}
and event_key = {{field}}
and created_at between {{startDate}} and {{endDate}}
group by event_key, string_value
order by 3 desc, 2 desc, 1 asc
limit 100
2023-07-11 10:16:17 +02:00
`,
2023-07-26 08:59:08 +02:00
{ websiteId, field, startDate: maxDate(startDate, website.resetAt), endDate },
2023-07-11 10:16:17 +02:00
);
}
2023-06-03 08:10:59 +02:00
return rawQuery(
2023-07-25 08:06:16 +02:00
`
select
event_key as field,
data_type as type,
count(*) as total
from event_data
where website_id = {{websiteId::uuid}}
and created_at between {{startDate}} and {{endDate}}
group by event_key, data_type
order by 3 desc, 2 asc, 1 asc
limit 100
2023-07-11 10:16:17 +02:00
`,
2023-07-26 08:59:08 +02:00
{ websiteId, startDate: maxDate(startDate, website.resetAt), endDate },
2023-06-03 08:10:59 +02:00
);
}
async function clickhouseQuery(websiteId: string, startDate: Date, endDate: Date, field: string) {
2023-07-25 08:06:16 +02:00
const { rawQuery } = clickhouse;
2023-06-03 08:10:59 +02:00
const website = await loadWebsite(websiteId);
2023-07-11 10:16:17 +02:00
if (field) {
return rawQuery(
2023-07-25 08:06:16 +02:00
`
select
2023-07-11 10:16:17 +02:00
event_key as field,
string_value as value,
count(*) as total
2023-07-25 08:06:16 +02:00
from event_data
where website_id = {websiteId:UUID}
and event_key = {field:String}
and created_at between {startDate:DateTime} and {endDate:DateTime}
group by event_key, string_value
order by 3 desc, 2 desc, 1 asc
limit 100
`,
2023-07-26 08:59:08 +02:00
{ websiteId, field, startDate: maxDate(startDate, website.resetAt), endDate },
2023-07-11 10:16:17 +02:00
);
}
2023-06-03 08:10:59 +02:00
return rawQuery(
2023-07-25 08:06:16 +02:00
`
select
event_key as field,
data_type as type,
count(*) as total
from event_data
where website_id = {websiteId:UUID}
and created_at between {startDate:DateTime} and {endDate:DateTime}
group by event_key, data_type
order by 3 desc, 2 asc, 1 asc
limit 100
2023-07-11 10:16:17 +02:00
`,
2023-07-26 08:59:08 +02:00
{ websiteId, startDate: maxDate(startDate, website.resetAt), endDate },
2023-06-03 08:10:59 +02:00
);
}