Merge remote-tracking branch 'origin/dev' into dev

# Conflicts:
#	src/app/(main)/App.tsx
This commit is contained in:
Mike Cao 2025-08-07 05:16:07 -07:00
commit 2e67e27b2f
27 changed files with 854 additions and 58 deletions

View file

@ -0,0 +1,71 @@
import { canViewWebsite } from '@/lib/auth';
import { EVENT_COLUMNS, SESSION_COLUMNS } from '@/lib/constants';
import { getQueryFilters, parseRequest } from '@/lib/request';
import { badRequest, json, unauthorized } from '@/lib/response';
import { dateRangeParams, filterParams, searchParams } from '@/lib/schema';
import {
getChannelExpandedMetrics,
getEventExpandedMetrics,
getPageviewExpandedMetrics,
getSessionExpandedMetrics,
} from '@/queries';
import { z } from 'zod';
export async function GET(
request: Request,
{ params }: { params: Promise<{ websiteId: string }> },
) {
const schema = z.object({
type: z.string(),
limit: z.coerce.number().optional(),
offset: z.coerce.number().optional(),
...dateRangeParams,
...searchParams,
...filterParams,
});
const { auth, query, error } = await parseRequest(request, schema);
if (error) {
return error();
}
const { websiteId } = await params;
if (!(await canViewWebsite(auth, websiteId))) {
return unauthorized();
}
const { type, limit, offset, search } = query;
const filters = await getQueryFilters(query, websiteId);
if (search) {
filters[type] = `c.${search}`;
}
if (SESSION_COLUMNS.includes(type)) {
const data = await getSessionExpandedMetrics(websiteId, { type, limit, offset }, filters);
return json(data);
}
if (EVENT_COLUMNS.includes(type)) {
let data;
if (type === 'event') {
data = await getEventExpandedMetrics(websiteId, { type, limit, offset }, filters);
} else {
data = await getPageviewExpandedMetrics(websiteId, { type, limit, offset }, filters);
}
return json(data);
}
if (type === 'channel') {
const data = await getChannelExpandedMetrics(websiteId, { limit, offset }, filters);
return json(data);
}
return badRequest();
}

View file

@ -46,22 +46,6 @@ export async function GET(
if (SESSION_COLUMNS.includes(type)) {
const data = await getSessionMetrics(websiteId, { type, limit, offset }, filters);
if (type === 'language') {
const combined = {};
for (const { x, y } of data) {
const key = String(x).toLowerCase().split('-')[0];
if (combined[key] === undefined) {
combined[key] = { x: key, y };
} else {
combined[key].y += y;
}
}
return json(Object.values(combined));
}
return json(data);
}