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';
|
2022-12-02 04:53:37 +00:00
|
|
|
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';
|
2020-10-08 22:02:48 +00:00
|
|
|
|
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;
|
2024-05-23 07:17:20 +00:00
|
|
|
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(),
|
2024-05-23 07:17:20 +00:00
|
|
|
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
|
|
|
|
2024-05-24 02:35:29 +00:00
|
|
|
const { websiteId, compare } = req.query;
|
2022-12-02 04:53:37 +00:00
|
|
|
|
2022-10-12 20:11:44 +00:00
|
|
|
if (req.method === 'GET') {
|
2022-12-27 23:18:58 +00:00
|
|
|
if (!(await canViewWebsite(req.auth, websiteId))) {
|
2020-10-08 22:02:48 +00:00
|
|
|
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,
|
|
|
|
|
);
|
2021-08-12 23:01:51 +00:00
|
|
|
|
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 });
|
2023-07-26 20:42:55 +00:00
|
|
|
|
2022-10-11 00:01:48 +00:00
|
|
|
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,
|
2022-03-20 21:10:41 +00:00
|
|
|
});
|
2020-10-08 22:02:48 +00:00
|
|
|
|
|
|
|
|
const stats = Object.keys(metrics[0]).reduce((obj, key) => {
|
2021-08-12 23:01:51 +00:00
|
|
|
obj[key] = {
|
|
|
|
|
value: Number(metrics[0][key]) || 0,
|
2024-05-23 07:17:20 +00:00
|
|
|
prev: Number(prevPeriod[0][key]) || 0,
|
2021-08-12 23:01:51 +00:00
|
|
|
};
|
2020-10-08 22:02:48 +00:00
|
|
|
return obj;
|
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
|
|
return ok(res, stats);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return methodNotAllowed(res);
|
|
|
|
|
};
|