2020-10-09 00:02:48 +02:00
|
|
|
import { getWebsiteStats } from 'lib/queries';
|
|
|
|
import { methodNotAllowed, ok, unauthorized } from 'lib/response';
|
|
|
|
import { allowQuery } from 'lib/auth';
|
|
|
|
|
|
|
|
export default async (req, res) => {
|
|
|
|
if (req.method === 'GET') {
|
|
|
|
if (!(await allowQuery(req))) {
|
|
|
|
return unauthorized(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
const { id, start_at, end_at, url } = req.query;
|
|
|
|
|
|
|
|
const websiteId = +id;
|
|
|
|
const startDate = new Date(+start_at);
|
|
|
|
const endDate = new Date(+end_at);
|
|
|
|
|
2021-08-13 01:01:51 +02:00
|
|
|
const distance = end_at - start_at;
|
|
|
|
const prevStartDate = new Date(+start_at - distance);
|
|
|
|
const prevEndDate = new Date(+end_at - distance);
|
|
|
|
|
2020-10-09 00:02:48 +02:00
|
|
|
const metrics = await getWebsiteStats(websiteId, startDate, endDate, { url });
|
2021-08-13 01:01:51 +02:00
|
|
|
const prevPeriod = await getWebsiteStats(websiteId, prevStartDate, prevEndDate, { url });
|
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,
|
|
|
|
change: Number(metrics[0][key] - prevPeriod[0][key]) || 0,
|
|
|
|
};
|
2020-10-09 00:02:48 +02:00
|
|
|
return obj;
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
return ok(res, stats);
|
|
|
|
}
|
|
|
|
|
|
|
|
return methodNotAllowed(res);
|
|
|
|
};
|