umami/src/pages/api/websites/[websiteId]/stats.ts

100 lines
2.4 KiB
TypeScript
Raw Normal View History

2024-05-24 02:35:29 +00:00
import * as yup from 'yup';
2023-07-26 16:55:54 +00:00
import { NextApiResponse } from 'next';
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { canViewWebsite } from 'lib/auth';
2023-08-20 05:23:15 +00:00
import { useAuth, useCors, useValidate } from 'lib/middleware';
2023-03-02 00:42:47 +00:00
import { NextApiRequestQueryBody, WebsiteStats } from 'lib/types';
2024-04-03 00:06:06 +00:00
import { getRequestFilters, getRequestDateRange } from 'lib/request';
2022-11-19 02:49:58 +00:00
import { getWebsiteStats } from 'queries';
2024-05-24 02:35:29 +00:00
import { getCompareDate } from 'lib/date';
2022-11-18 06:46:05 +00:00
export interface WebsiteStatsRequestQuery {
2024-02-01 06:08:48 +00:00
websiteId: string;
2022-12-27 01:36:48 +00:00
startAt: number;
endAt: number;
2023-09-25 20:19:56 +00:00
url?: string;
referrer?: string;
title?: string;
query?: string;
event?: string;
2024-06-19 16:50:39 +00:00
host?: string;
2023-09-25 20:19:56 +00:00
os?: string;
browser?: string;
device?: string;
country?: string;
region?: string;
city?: string;
compare?: string;
2022-11-15 21:21:14 +00:00
}
2023-08-20 05:23:15 +00:00
const schema = {
GET: yup.object().shape({
2024-02-01 06:08:48 +00:00
websiteId: yup.string().uuid().required(),
2023-09-25 20:19:56 +00:00
startAt: yup.number().required(),
endAt: yup.number().required(),
url: yup.string(),
referrer: yup.string(),
title: yup.string(),
query: yup.string(),
event: yup.string(),
2024-06-19 16:50:39 +00:00
host: yup.string(),
2023-09-25 20:19:56 +00:00
os: yup.string(),
browser: yup.string(),
device: yup.string(),
country: yup.string(),
region: yup.string(),
city: yup.string(),
compare: yup.string(),
2023-08-20 05:23:15 +00:00
}),
};
2022-11-15 21:21:14 +00:00
export default async (
2022-11-18 06:46:05 +00:00
req: NextApiRequestQueryBody<WebsiteStatsRequestQuery>,
2022-11-15 21:21:14 +00:00
res: NextApiResponse<WebsiteStats>,
) => {
2022-10-12 20:11:44 +00:00
await useCors(req, res);
await useAuth(req, res);
2023-09-30 03:24:48 +00:00
await useValidate(schema, req, res);
2023-08-20 05:23:15 +00:00
const { websiteId, compare } = req.query;
2022-10-12 20:11:44 +00:00
if (req.method === 'GET') {
if (!(await canViewWebsite(req.auth, websiteId))) {
return unauthorized(res);
}
2024-04-03 00:06:06 +00:00
const { startDate, endDate } = await getRequestDateRange(req);
2024-05-24 02:35:29 +00:00
const { startDate: compareStartDate, endDate: compareEndDate } = getCompareDate(
compare,
startDate,
endDate,
);
2024-04-03 00:06:06 +00:00
const filters = getRequestFilters(req);
2023-08-04 20:18:30 +00:00
const metrics = await getWebsiteStats(websiteId, {
...filters,
startDate,
endDate,
});
const prevPeriod = await getWebsiteStats(websiteId, {
2023-08-04 20:18:30 +00:00
...filters,
2024-05-24 02:35:29 +00:00
startDate: compareStartDate,
endDate: compareEndDate,
});
const stats = Object.keys(metrics[0]).reduce((obj, key) => {
obj[key] = {
value: Number(metrics[0][key]) || 0,
prev: Number(prevPeriod[0][key]) || 0,
};
return obj;
}, {});
return ok(res, stats);
}
return methodNotAllowed(res);
};