2020-07-29 04:04:45 +02:00
|
|
|
import moment from 'moment-timezone';
|
2020-07-28 08:52:14 +02:00
|
|
|
import { getPageviewData } from 'lib/db';
|
|
|
|
import { useAuth } from 'lib/middleware';
|
2020-07-26 09:12:42 +02:00
|
|
|
|
|
|
|
export default async (req, res) => {
|
2020-07-28 08:52:14 +02:00
|
|
|
await useAuth(req, res);
|
2020-07-26 09:12:42 +02:00
|
|
|
|
2020-07-28 10:17:45 +02:00
|
|
|
const { id, start_at, end_at, unit, tz } = req.query;
|
2020-07-26 09:12:42 +02:00
|
|
|
|
2020-07-29 04:04:45 +02:00
|
|
|
if (!moment.tz.zone(tz) || !['hour', 'day'].includes(unit)) {
|
|
|
|
return res.status(400).end();
|
|
|
|
}
|
|
|
|
|
2020-07-28 08:52:14 +02:00
|
|
|
const [pageviews, uniques] = await Promise.all([
|
2020-07-28 10:17:45 +02:00
|
|
|
getPageviewData(+id, new Date(+start_at), new Date(+end_at), tz, unit, '*'),
|
|
|
|
getPageviewData(+id, new Date(+start_at), new Date(+end_at), tz, unit, 'distinct session_id'),
|
2020-07-28 08:52:14 +02:00
|
|
|
]);
|
|
|
|
|
2020-07-29 04:04:45 +02:00
|
|
|
return res.status(200).json({ pageviews, uniques });
|
2020-07-26 09:12:42 +02:00
|
|
|
};
|