diff --git a/.gitignore b/.gitignore index 7066fb28..99087ab5 100644 --- a/.gitignore +++ b/.gitignore @@ -25,6 +25,7 @@ node_modules *.iml *.log .vscode +.tool-versions # debug npm-debug.log* diff --git a/pages/api/event-data/fields.ts b/pages/api/event-data/fields.ts index 18b74bc3..ca0f23d4 100644 --- a/pages/api/event-data/fields.ts +++ b/pages/api/event-data/fields.ts @@ -21,13 +21,19 @@ export default async ( await useAuth(req, res); if (req.method === 'GET') { - const { websiteId, startAt, endAt, field } = req.query; + const { websiteId, startAt, endAt, field, withEventNames } = req.query; if (!(await canViewWebsite(req.auth, websiteId))) { return unauthorized(res); } - const data = await getEventDataFields(websiteId, new Date(+startAt), new Date(+endAt), field); + const data = await getEventDataFields( + websiteId, + new Date(+startAt), + new Date(+endAt), + field, + withEventNames, + ); return ok(res, data); } diff --git a/queries/analytics/eventData/getEventDataFields.ts b/queries/analytics/eventData/getEventDataFields.ts index 6306d239..1df6db12 100644 --- a/queries/analytics/eventData/getEventDataFields.ts +++ b/queries/analytics/eventData/getEventDataFields.ts @@ -6,7 +6,13 @@ import { loadWebsite } from 'lib/query'; import { DEFAULT_CREATED_AT } from 'lib/constants'; export async function getEventDataFields( - ...args: [websiteId: string, startDate: Date, endDate: Date, field?: string] + ...args: [ + websiteId: string, + startDate: Date, + endDate: Date, + field?: string, + withEventNames?: boolean, + ] ): Promise { return runQuery({ [PRISMA]: () => relationalQuery(...args), @@ -14,7 +20,13 @@ export async function getEventDataFields( }); } -async function relationalQuery(websiteId: string, startDate: Date, endDate: Date, field: string) { +async function relationalQuery( + websiteId: string, + startDate: Date, + endDate: Date, + field: string, + withEventNames: boolean, +) { const { toUuid, rawQuery } = prisma; const website = await loadWebsite(websiteId); const resetDate = new Date(website?.resetAt || DEFAULT_CREATED_AT); @@ -31,12 +43,30 @@ async function relationalQuery(websiteId: string, startDate: Date, endDate: Date and created_at between $4 and $5 group by event_key, string_value order by 3 desc, 2 desc, 1 asc - limit 100 `, [websiteId, field, resetDate, startDate, endDate] as any, ); } + 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, + ); + } + return rawQuery( `select event_key as field, @@ -48,13 +78,18 @@ async function relationalQuery(websiteId: string, startDate: Date, endDate: Date and created_at between $3 and $4 group by event_key, data_type order by 3 desc, 2 asc, 1 asc - limit 100 `, [websiteId, resetDate, startDate, endDate] as any, ); } -async function clickhouseQuery(websiteId: string, startDate: Date, endDate: Date, field: string) { +async function clickhouseQuery( + websiteId: string, + startDate: Date, + endDate: Date, + field: string, + withEventNames: boolean, +) { const { rawQuery, getDateFormat, getBetweenDates } = clickhouse; const website = await loadWebsite(websiteId); const resetDate = new Date(website?.resetAt || DEFAULT_CREATED_AT); @@ -72,12 +107,30 @@ async function clickhouseQuery(websiteId: string, startDate: Date, endDate: Date and ${getBetweenDates('created_at', startDate, endDate)} group by event_key, string_value order by 3 desc, 2 desc, 1 asc - limit 100 `, { websiteId, field }, ); } + 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 website_id = {websiteId:UUID} + and created_at >= ${getDateFormat(resetDate)} + and ${getBetweenDates('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, + ); + } + return rawQuery( `select event_key as field, @@ -89,7 +142,6 @@ async function clickhouseQuery(websiteId: string, startDate: Date, endDate: Date and ${getBetweenDates('created_at', startDate, endDate)} group by event_key, data_type order by 3 desc, 2 asc, 1 asc - limit 100 `, { websiteId }, );