Skip to content

Commit

Permalink
feat: API for event data now can return associated event name
Browse files Browse the repository at this point in the history
  • Loading branch information
tairosonloa committed Jul 14, 2023
1 parent 7bfbe26 commit 92fc4e8
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ node_modules
*.iml
*.log
.vscode
.tool-versions

# debug
npm-debug.log*
Expand Down
10 changes: 8 additions & 2 deletions pages/api/event-data/fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
66 changes: 59 additions & 7 deletions queries/analytics/eventData/getEventDataFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,27 @@ 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<WebsiteEventDataFields[]> {
return runQuery({
[PRISMA]: () => relationalQuery(...args),
[CLICKHOUSE]: () => clickhouseQuery(...args),
});
}

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);
Expand All @@ -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,
Expand All @@ -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);
Expand All @@ -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,
Expand All @@ -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 },
);
Expand Down

0 comments on commit 92fc4e8

Please sign in to comment.