Skip to content

Commit

Permalink
add getEventDataFields
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscao633 committed Sep 13, 2024
1 parent 29044de commit e440779
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 2 deletions.
51 changes: 51 additions & 0 deletions src/pages/api/websites/[websiteId]/event-data/fields.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { canViewWebsite } from 'lib/auth';
import { useAuth, useCors, useValidate } from 'lib/middleware';
import { NextApiRequestQueryBody } from 'lib/types';
import { NextApiResponse } from 'next';
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { getEventDataFields } from 'queries';

import * as yup from 'yup';

export interface EventDataFieldsRequestQuery {
websiteId: string;
startAt: string;
endAt: string;
}

const schema = {
GET: yup.object().shape({
websiteId: yup.string().uuid().required(),
startAt: yup.number().integer().required(),
endAt: yup.number().integer().min(yup.ref('startAt')).required(),
}),
};

export default async (
req: NextApiRequestQueryBody<EventDataFieldsRequestQuery>,
res: NextApiResponse<any>,
) => {
await useCors(req, res);
await useAuth(req, res);
await useValidate(schema, req, res);

if (req.method === 'GET') {
const { websiteId, startAt, endAt } = req.query;

if (!(await canViewWebsite(req.auth, websiteId))) {
return unauthorized(res);
}

const startDate = new Date(+startAt);
const endDate = new Date(+endAt);

const data = await getEventDataFields(websiteId, {
startDate,
endDate,
});

return ok(res, data);
}

return methodNotAllowed(res);
};
69 changes: 69 additions & 0 deletions src/queries/analytics/events/getEventDataFields.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import prisma from 'lib/prisma';
import clickhouse from 'lib/clickhouse';
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
import { QueryFilters, WebsiteEventData } from 'lib/types';

export async function getEventDataFields(
...args: [websiteId: string, filters: QueryFilters]
): Promise<WebsiteEventData[]> {
return runQuery({
[PRISMA]: () => relationalQuery(...args),
[CLICKHOUSE]: () => clickhouseQuery(...args),
});
}

async function relationalQuery(websiteId: string, filters: QueryFilters) {
const { rawQuery, parseFilters, getDateSQL } = prisma;
const { filterQuery, params } = await parseFilters(websiteId, filters);

return rawQuery(
`
select
data_key as "propertyName",
data_type as "dataType",
case
when data_type = 2 then replace(string_value, '.0000', '')
when data_type = 4 then ${getDateSQL('date_value', 'hour')}
else string_value
end as "value",
count(*) as "total"
from event_data
join website_event on website_event.event_id = event_data.website_event_id
where event_data.website_id = {{websiteId::uuid}}
and event_data.created_at between {{startDate}} and {{endDate}}
${filterQuery}
group by data_key, data_type, value
order by 2 desc
limit 100
`,
params,
);
}

async function clickhouseQuery(
websiteId: string,
filters: QueryFilters,
): Promise<{ propertyName: string; dataType: number; propertyValue: string; total: number }[]> {
const { rawQuery, parseFilters } = clickhouse;
const { filterQuery, params } = await parseFilters(websiteId, filters);

return rawQuery(
`
select
data_key as propertyName,
data_type as dataType,
multiIf(data_type = 2, replaceAll(string_value, '.0000', ''),
data_type = 4, toString(date_trunc('hour', date_value)),
string_value) as "value",
count(*) as "total"
from event_data
where website_id = {websiteId:UUID}
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
${filterQuery}
group by data_key, data_type, value
order by 2 desc
limit 100
`,
params,
);
}
4 changes: 2 additions & 2 deletions src/queries/analytics/events/getEventDataValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ async function relationalQuery(
async function clickhouseQuery(
websiteId: string,
filters: QueryFilters & { eventName?: string; propertyName?: string },
): Promise<{ propertyName: string; dataType: number; propertyValue: string; total: number }[]> {
): Promise<{ value: string; total: number }[]> {
const { rawQuery, parseFilters } = clickhouse;
const { filterQuery, params } = await parseFilters(websiteId, filters);

Expand All @@ -66,7 +66,7 @@ async function clickhouseQuery(
and data_key = {propertyName:String}
and event_name = {eventName:String}
${filterQuery}
group by event_name, value
group by value
order by 2 desc
limit 100
`,
Expand Down
1 change: 1 addition & 0 deletions src/queries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from 'queries/prisma/teamUser';
export * from 'queries/prisma/user';
export * from 'queries/prisma/website';
export * from './analytics/events/getEventDataEvents';
export * from './analytics/events/getEventDataFields';
export * from './analytics/events/getEventDataProperties';
export * from './analytics/events/getEventDataValues';
export * from './analytics/events/getEventDataStats';
Expand Down

0 comments on commit e440779

Please sign in to comment.