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