Skip to content

Commit

Permalink
Fixed reports.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecao committed Feb 2, 2025
1 parent 3e9cb66 commit 530d6fb
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 33 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
"npm-run-all": "^4.1.5",
"prisma": "6.1.0",
"react": "^19.0.0",
"react-basics": "^0.125.0",
"react-basics": "^0.126.0",
"react-beautiful-dnd": "^13.1.0",
"react-dom": "^19.0.0",
"react-error-boundary": "^4.0.4",
Expand Down
2 changes: 1 addition & 1 deletion src/app/(main)/reports/[reportId]/FieldFilterEditForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ const ResultsMenu = ({ values, type, isLoading, onSelect }) => {

return (
<Menu className={styles.menu} variant="popup" onSelect={onSelect}>
{values?.map((value: any) => {
{values?.map(({ value }) => {
return <Item key={value}>{formatValue(value, type)}</Item>;
})}
</Menu>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export function SessionsWeekly({ websiteId }: { websiteId: string }) {
{day?.map((hour: number, n) => {
const pct = hour / max;
return (
<div key={`${index}_${n}_${hour}`} className={classNames(styles.cell)}>
<div key={n} className={classNames(styles.cell)}>
{hour > 0 && (
<TooltipPopup
label={`${formatMessage(labels.visitors)}: ${hour}`}
Expand Down
15 changes: 3 additions & 12 deletions src/app/api/reports/revenue/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@ import { z } from 'zod';
import { canViewWebsite } from 'lib/auth';
import { unauthorized, json } from 'lib/response';
import { parseRequest } from 'lib/request';
import { reportParms, timezoneParam, unitParam } from 'lib/schema';
import { reportParms, timezoneParam } from 'lib/schema';
import { getRevenue } from 'queries/analytics/reports/getRevenue';
import { getRevenueValues } from 'queries/analytics/reports/getRevenueValues';

export async function GET(request: Request) {
const schema = z.object({
...reportParms,
});

const { auth, query, error } = await parseRequest(request, schema);
const { auth, query, error } = await parseRequest(request);

if (error) {
return error();
Expand All @@ -33,12 +29,7 @@ export async function GET(request: Request) {

export async function POST(request: Request) {
const schema = z.object({
websiteId: z.string().uuid(),
dateRange: z.object({
startDate: z.date(),
endDate: z.date(),
unit: unitParam,
}),
...reportParms,
timezone: timezoneParam,
});

Expand Down
41 changes: 38 additions & 3 deletions src/app/api/reports/route.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { z } from 'zod';
import { pagingParams } from 'lib/schema';
import { uuid } from 'lib/crypto';
import { pagingParams, reportTypeParam } from 'lib/schema';
import { parseRequest } from 'lib/request';
import { canViewTeam, canViewWebsite } from 'lib/auth';
import { canViewTeam, canViewWebsite, canUpdateWebsite } from 'lib/auth';
import { unauthorized, json } from 'lib/response';
import { getReports } from 'queries/prisma/report';
import { getReports, createReport } from 'queries';

export async function GET(request: Request) {
const schema = z.object({
Expand Down Expand Up @@ -71,3 +72,37 @@ export async function GET(request: Request) {

return json(data);
}

export async function POST(request: Request) {
const schema = z.object({
websiteId: z.string().uuid(),
name: z.string().max(200),
type: reportTypeParam,
description: z.string().max(500),
parameters: z.object({}).passthrough(),
});

const { auth, body, error } = await parseRequest(request, schema);

if (error) {
return error();
}

const { websiteId, type, name, description, parameters } = body;

if (!(await canUpdateWebsite(auth, websiteId))) {
return unauthorized();
}

const result = await createReport({
id: uuid(),
userId: auth.user.id,
websiteId,
type,
name,
description,
parameters: JSON.stringify(parameters),
} as any);

return json(result);
}
6 changes: 1 addition & 5 deletions src/app/api/teams/[teamId]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ import { parseRequest } from 'lib/request';
import { deleteTeam, getTeam, updateTeam } from 'queries';

export async function GET(request: Request, { params }: { params: Promise<{ teamId: string }> }) {
const schema = z.object({
teamId: z.string().uuid(),
});

const { auth, error } = await parseRequest(request, schema);
const { auth, error } = await parseRequest(request);

if (error) {
return error();
Expand Down
2 changes: 1 addition & 1 deletion src/app/api/websites/[websiteId]/events/series/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export async function GET(

const { websiteId } = await params;
const { timezone } = query;
const { startDate, endDate, unit } = await getRequestDateRange(request);
const { startDate, endDate, unit } = await getRequestDateRange(query);

if (!(await canViewWebsite(auth, websiteId))) {
return unauthorized();
Expand Down
4 changes: 2 additions & 2 deletions src/app/api/websites/[websiteId]/sessions/stats/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export async function GET(
...filterParams,
});

const { auth, error } = await parseRequest(request, schema);
const { auth, query, error } = await parseRequest(request, schema);

if (error) {
return error();
Expand All @@ -27,7 +27,7 @@ export async function GET(
return unauthorized();
}

const { startDate, endDate } = await getRequestDateRange(request);
const { startDate, endDate } = await getRequestDateRange(query);

const filters = getRequestFilters(request);

Expand Down
2 changes: 1 addition & 1 deletion src/app/api/websites/[websiteId]/values/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export async function GET(

const { websiteId } = await params;
const { type, search } = query;
const { startDate, endDate } = await getRequestDateRange(request);
const { startDate, endDate } = await getRequestDateRange(query);

if (!(await canViewWebsite(auth, websiteId))) {
return unauthorized();
Expand Down
4 changes: 4 additions & 0 deletions src/lib/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,9 @@ export const reportParms = {
dateRange: z.object({
startDate: z.coerce.date(),
endDate: z.coerce.date(),
num: z.coerce.number().optional(),
offset: z.coerce.number().optional(),
unit: z.string().optional(),
value: z.string().optional(),
}),
};
4 changes: 2 additions & 2 deletions src/queries/analytics/getValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async function relationalQuery(

return rawQuery(
`
select ${column} as "value", count(*)
select ${column} as "value", count(*) as "count"
from website_event
inner join session
on session.session_id = website_event.session_id
Expand Down Expand Up @@ -98,7 +98,7 @@ async function clickhouseQuery(

return rawQuery(
`
select ${column} as value, count(*)
select ${column} as "value", count(*) as "count"
from website_event
where website_id = {websiteId:UUID}
and created_at between {startDate:DateTime64} and {endDate:DateTime64}
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9206,10 +9206,10 @@ randombytes@^2.1.0:
dependencies:
safe-buffer "^5.1.0"

react-basics@^0.125.0:
version "0.125.0"
resolved "https://registry.yarnpkg.com/react-basics/-/react-basics-0.125.0.tgz#6baf3fea503fb4475f51877efa05d1a734b232c6"
integrity sha512-8swjTaKfenwb+NunwzQo16V+dCA/38Kd+PSYWpBFyNmlFzs3Ax2ZgnysxDhW9IgfFr4wR6/0gzD3S31WzXq6Kw==
react-basics@^0.126.0:
version "0.126.0"
resolved "https://registry.yarnpkg.com/react-basics/-/react-basics-0.126.0.tgz#44e7f5e5ab9d411e91e697dd39c6cb53b6222ae0"
integrity sha512-TQtNZMeH5FtJjYxSN72rBmZWlIcs9jK3oVSCUUxfZq9LnFdoFSagTLCrihs3YCnX8vZEJXaJHQsp7lKEfyH5sw==
dependencies:
"@react-spring/web" "^9.7.3"
classnames "^2.3.1"
Expand Down

0 comments on commit 530d6fb

Please sign in to comment.