2020-08-12 07:24:41 +02:00
|
|
|
import { getMetrics } from 'lib/queries';
|
2020-09-18 07:52:20 +02:00
|
|
|
import { methodNotAllowed, ok, unauthorized } from 'lib/response';
|
|
|
|
import { allowQuery } from 'lib/auth';
|
2020-07-28 08:52:14 +02:00
|
|
|
|
|
|
|
export default async (req, res) => {
|
2020-09-11 22:49:43 +02:00
|
|
|
if (req.method === 'GET') {
|
2020-09-18 07:52:20 +02:00
|
|
|
if (!(await allowQuery(req))) {
|
|
|
|
return unauthorized(res);
|
|
|
|
}
|
|
|
|
|
2020-09-26 07:31:18 +02:00
|
|
|
const { id, start_at, end_at, url } = req.query;
|
2020-09-18 07:52:20 +02:00
|
|
|
|
2020-09-11 22:49:43 +02:00
|
|
|
const websiteId = +id;
|
|
|
|
const startDate = new Date(+start_at);
|
|
|
|
const endDate = new Date(+end_at);
|
2020-07-28 08:52:14 +02:00
|
|
|
|
2020-09-26 07:31:18 +02:00
|
|
|
const metrics = await getMetrics(websiteId, startDate, endDate, url);
|
2020-07-30 06:40:26 +02:00
|
|
|
|
2020-09-11 22:49:43 +02:00
|
|
|
const stats = Object.keys(metrics[0]).reduce((obj, key) => {
|
|
|
|
obj[key] = Number(metrics[0][key]) || 0;
|
|
|
|
return obj;
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
return ok(res, stats);
|
|
|
|
}
|
|
|
|
|
|
|
|
return methodNotAllowed(res);
|
2020-07-28 08:52:14 +02:00
|
|
|
};
|