2022-12-02 05:53:37 +01:00
|
|
|
import { canViewWebsite } from 'lib/auth';
|
2022-11-19 03:49:58 +01:00
|
|
|
import { useAuth, useCors } from 'lib/middleware';
|
2023-03-02 01:42:47 +01:00
|
|
|
import { NextApiRequestQueryBody, WebsiteStats } from 'lib/types';
|
2022-11-15 22:21:14 +01:00
|
|
|
import { NextApiResponse } from 'next';
|
2022-11-19 03:49:58 +01:00
|
|
|
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
|
|
|
|
import { getWebsiteStats } from 'queries';
|
2020-10-09 00:02:48 +02:00
|
|
|
|
2022-11-18 07:46:05 +01:00
|
|
|
export interface WebsiteStatsRequestQuery {
|
2022-11-15 22:21:14 +01:00
|
|
|
id: string;
|
|
|
|
type: string;
|
2022-12-27 02:36:48 +01:00
|
|
|
startAt: number;
|
|
|
|
endAt: number;
|
2022-11-15 22:21:14 +01:00
|
|
|
url: string;
|
|
|
|
referrer: string;
|
2023-03-01 19:53:57 +01:00
|
|
|
pageTitle: string;
|
2022-11-15 22:21:14 +01:00
|
|
|
os: string;
|
|
|
|
browser: string;
|
|
|
|
device: string;
|
|
|
|
country: string;
|
2023-02-20 18:04:20 +01:00
|
|
|
subdivision1: string;
|
|
|
|
subdivision2: string;
|
|
|
|
city: string;
|
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);
|
2022-04-04 09:33:20 +02:00
|
|
|
|
2023-02-20 18:04:20 +01:00
|
|
|
const {
|
|
|
|
id: websiteId,
|
|
|
|
startAt,
|
|
|
|
endAt,
|
|
|
|
url,
|
|
|
|
referrer,
|
2023-03-01 19:53:57 +01:00
|
|
|
pageTitle,
|
2023-02-20 18:04:20 +01:00
|
|
|
os,
|
|
|
|
browser,
|
|
|
|
device,
|
|
|
|
country,
|
|
|
|
subdivision1,
|
|
|
|
subdivision2,
|
|
|
|
city,
|
|
|
|
} = 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);
|
|
|
|
}
|
|
|
|
|
2022-12-27 02:36:48 +01:00
|
|
|
const startDate = new Date(+startAt);
|
|
|
|
const endDate = new Date(+endAt);
|
2020-10-09 00:02:48 +02:00
|
|
|
|
2022-12-27 02:36:48 +01:00
|
|
|
const distance = endAt - startAt;
|
|
|
|
const prevStartDate = new Date(+startAt - distance);
|
|
|
|
const prevEndDate = new Date(+endAt - distance);
|
2021-08-13 01:01:51 +02:00
|
|
|
|
2022-10-11 02:01:48 +02:00
|
|
|
const metrics = await getWebsiteStats(websiteId, {
|
2022-11-18 07:27:33 +01:00
|
|
|
startDate,
|
|
|
|
endDate,
|
2022-09-24 07:43:51 +02:00
|
|
|
filters: {
|
|
|
|
url,
|
|
|
|
referrer,
|
2023-03-01 19:53:57 +01:00
|
|
|
pageTitle,
|
2022-09-24 07:43:51 +02:00
|
|
|
os,
|
|
|
|
browser,
|
|
|
|
device,
|
|
|
|
country,
|
2023-02-20 18:04:20 +01:00
|
|
|
subdivision1,
|
|
|
|
subdivision2,
|
|
|
|
city,
|
2022-09-24 07:43:51 +02:00
|
|
|
},
|
2022-04-10 12:51:43 +02:00
|
|
|
});
|
2022-10-11 02:01:48 +02:00
|
|
|
const prevPeriod = await getWebsiteStats(websiteId, {
|
2022-11-18 07:27:33 +01:00
|
|
|
startDate: prevStartDate,
|
|
|
|
endDate: prevEndDate,
|
2022-09-24 07:43:51 +02:00
|
|
|
filters: {
|
|
|
|
url,
|
|
|
|
referrer,
|
2023-03-01 19:53:57 +01:00
|
|
|
pageTitle,
|
2022-09-24 07:43:51 +02:00
|
|
|
os,
|
|
|
|
browser,
|
|
|
|
device,
|
|
|
|
country,
|
2023-02-20 18:04:20 +01:00
|
|
|
subdivision1,
|
|
|
|
subdivision2,
|
|
|
|
city,
|
2022-09-24 07:43:51 +02:00
|
|
|
},
|
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);
|
|
|
|
};
|