add filtering to event chart

This commit is contained in:
Francis Cao 2024-04-23 11:19:05 -07:00
parent fde2be4900
commit cfbc4ebd72
3 changed files with 40 additions and 18 deletions

View File

@ -1,18 +1,14 @@
import useApi from './useApi';
import { UseQueryOptions } from '@tanstack/react-query';
import { useDateRange, useNavigation, useTimezone } from 'components/hooks'; import { useDateRange, useNavigation, useTimezone } from 'components/hooks';
import { zonedTimeToUtc } from 'date-fns-tz'; import { zonedTimeToUtc } from 'date-fns-tz';
import useApi from './useApi';
export function useWebsiteEvents( export function useWebsiteEvents(websiteId: string, options?: { [key: string]: string }) {
websiteId: string,
options?: Omit<UseQueryOptions, 'queryKey' | 'queryFn'>,
) {
const { get, useQuery } = useApi(); const { get, useQuery } = useApi();
const [dateRange] = useDateRange(websiteId); const [dateRange] = useDateRange(websiteId);
const { startDate, endDate, unit, offset } = dateRange; const { startDate, endDate, unit, offset } = dateRange;
const { timezone } = useTimezone(); const { timezone } = useTimezone();
const { const {
query: { url, event }, query: { url, referrer, os, browser, device, country, region, city, title, event },
} = useNavigation(); } = useNavigation();
const params = { const params = {
@ -20,14 +16,22 @@ export function useWebsiteEvents(
endAt: +zonedTimeToUtc(endDate, timezone), endAt: +zonedTimeToUtc(endDate, timezone),
unit, unit,
offset, offset,
timezone,
url, url,
referrer,
os,
browser,
device,
country,
region,
city,
title,
timezone,
event, event,
}; };
return useQuery({ return useQuery({
queryKey: ['events', { ...params }], queryKey: ['events', { websiteId, ...params }],
queryFn: () => get(`/websites/${websiteId}/events`, { ...params }), queryFn: () => get(`/websites/${websiteId}/events`, params),
enabled: !!websiteId, enabled: !!websiteId,
...options, ...options,
}); });

View File

@ -1,6 +1,6 @@
import { canViewWebsite } from 'lib/auth'; import { canViewWebsite } from 'lib/auth';
import { useAuth, useCors, useValidate } from 'lib/middleware'; import { useAuth, useCors, useValidate } from 'lib/middleware';
import { getRequestDateRange } from 'lib/request'; import { getRequestFilters, getRequestDateRange } from 'lib/request';
import { NextApiRequestQueryBody, WebsiteMetric } from 'lib/types'; import { NextApiRequestQueryBody, WebsiteMetric } from 'lib/types';
import { TimezoneTest, UnitTypeTest } from 'lib/yup'; import { TimezoneTest, UnitTypeTest } from 'lib/yup';
import { NextApiResponse } from 'next'; import { NextApiResponse } from 'next';
@ -15,6 +15,14 @@ export interface WebsiteEventsRequestQuery {
unit?: string; unit?: string;
timezone?: string; timezone?: string;
url: string; url: string;
referrer?: string;
title?: string;
os?: string;
browser?: string;
device?: string;
country?: string;
region: string;
city?: string;
} }
const schema = { const schema = {
@ -25,6 +33,14 @@ const schema = {
unit: UnitTypeTest, unit: UnitTypeTest,
timezone: TimezoneTest, timezone: TimezoneTest,
url: yup.string(), url: yup.string(),
referrer: yup.string(),
title: yup.string(),
os: yup.string(),
browser: yup.string(),
device: yup.string(),
country: yup.string(),
region: yup.string(),
city: yup.string(),
}), }),
}; };
@ -36,7 +52,7 @@ export default async (
await useAuth(req, res); await useAuth(req, res);
await useValidate(schema, req, res); await useValidate(schema, req, res);
const { websiteId, timezone, url } = req.query; const { websiteId, timezone } = req.query;
const { startDate, endDate, unit } = await getRequestDateRange(req); const { startDate, endDate, unit } = await getRequestDateRange(req);
if (req.method === 'GET') { if (req.method === 'GET') {
@ -44,13 +60,15 @@ export default async (
return unauthorized(res); return unauthorized(res);
} }
const events = await getEventMetrics(websiteId, { const filters = {
...getRequestFilters(req),
startDate, startDate,
endDate, endDate,
timezone, timezone,
unit, unit,
url, };
});
const events = await getEventMetrics(websiteId, filters);
return ok(res, events); return ok(res, events);
} }

View File

@ -25,12 +25,12 @@ async function relationalQuery(websiteId: string, filters: QueryFilters) {
` `
select select
event_name x, event_name x,
${getDateQuery('created_at', unit, timezone)} t, ${getDateQuery('website_event.created_at', unit, timezone)} t,
count(*) y count(*) y
from website_event from website_event
${joinSession} ${joinSession}
where website_id = {{websiteId::uuid}} where website_event.website_id = {{websiteId::uuid}}
and created_at between {{startDate}} and {{endDate}} and website_event.created_at between {{startDate}} and {{endDate}}
and event_type = {{eventType}} and event_type = {{eventType}}
${filterQuery} ${filterQuery}
group by 1, 2 group by 1, 2