umami/queries/analytics/eventData/getEventData.ts

106 lines
2.7 KiB
TypeScript
Raw Normal View History

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-05 07:51:23 +02:00
export interface EventDataCriteria {
fields: [{ name: string; type: string; value: string }];
filters: [
{
name: string;
type: string;
value: [string, string];
},
];
groups: [
{
name: string;
type: string;
},
2023-07-05 07:51:23 +02:00
];
}
export async function getEventData(
...args: [websiteId: string, startDate: Date, endDate: Date, criteria: EventDataCriteria]
): Promise<WebsiteEventDataMetric[]> {
return runQuery({
[PRISMA]: () => relationalQuery(...args),
[CLICKHOUSE]: () => clickhouseQuery(...args),
});
}
2023-07-05 07:51:23 +02:00
async function relationalQuery() {
return null;
}
async function clickhouseQuery(
websiteId: string,
2023-07-05 07:51:23 +02:00
startDate: Date,
endDate: Date,
criteria: EventDataCriteria,
) {
2023-07-08 05:38:43 +02:00
const { fields, filters } = criteria;
2023-07-05 07:51:23 +02:00
const { rawQuery, getDateFormat, getBetweenDates } = clickhouse;
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-07-08 05:38:43 +02:00
const uniqueFields = fields.reduce((obj, { name, type, value }) => {
const prefix = type === 'array' ? 'string' : type;
2023-07-05 07:51:23 +02:00
if (!obj[name]) {
obj[name] = {
2023-07-08 05:38:43 +02:00
columns: [
'event_key as field',
`count(*) as total`,
value === 'unique' ? `${prefix}_value as value` : null,
].filter(n => n),
groups: ['event_key', value === 'unique' ? `${prefix}_value` : null].filter(n => n),
2023-07-05 07:51:23 +02:00
};
}
return obj;
}, {});
const queries = Object.keys(uniqueFields).reduce((arr, key) => {
const field = uniqueFields[key];
const params = { websiteId, name: key };
return arr.concat(
rawQuery(
`select
${field.columns.join(',')}
from event_data
where website_id = {websiteId:UUID}
and event_key = {name:String}
and created_at >= ${getDateFormat(resetDate)}
and ${getBetweenDates('created_at', startDate, endDate)}
group by ${field.groups.join(',')}
2023-07-08 05:38:43 +02:00
limit 100
2023-07-05 07:51:23 +02:00
`,
params,
),
);
}, []);
const results = (await Promise.all(queries)).flatMap(n => n);
const columns = results.reduce((arr, row) => {
const keys = Object.keys(row);
for (const key of keys) {
if (!arr.includes(key)) {
arr.push(key);
}
}
return arr;
}, []);
return results.reduce((arr, row) => {
return arr.concat(
columns.reduce((obj, key) => {
obj[key] = row[key];
return obj;
}, {}),
);
}, []);
}