2020-08-28 03:44:20 +02:00
|
|
|
import { getRankings } from 'lib/queries';
|
2020-08-08 02:19:42 +02:00
|
|
|
import { ok, badRequest } from 'lib/response';
|
2020-08-01 04:05:14 +02:00
|
|
|
|
2020-08-07 04:37:19 +02:00
|
|
|
const sessionColumns = ['browser', 'os', 'device', 'country'];
|
2020-08-01 04:05:14 +02:00
|
|
|
const pageviewColumns = ['url', 'referrer'];
|
|
|
|
|
2020-08-28 03:44:20 +02:00
|
|
|
function getTable(type) {
|
|
|
|
if (type === 'event') {
|
|
|
|
return 'event';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sessionColumns.includes(type)) {
|
|
|
|
return 'session';
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'pageview';
|
|
|
|
}
|
|
|
|
|
|
|
|
function getColumn(type) {
|
|
|
|
if (type === 'event') {
|
|
|
|
return `concat(event_type, ':', event_value)`;
|
|
|
|
}
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2020-08-01 04:05:14 +02:00
|
|
|
export default async (req, res) => {
|
|
|
|
const { id, type, start_at, end_at } = req.query;
|
2020-08-25 08:49:14 +02:00
|
|
|
const websiteId = +id;
|
|
|
|
const startDate = new Date(+start_at);
|
|
|
|
const endDate = new Date(+end_at);
|
2020-08-01 04:05:14 +02:00
|
|
|
|
2020-08-25 08:49:14 +02:00
|
|
|
if (type !== 'event' && !sessionColumns.includes(type) && !pageviewColumns.includes(type)) {
|
2020-08-08 02:19:42 +02:00
|
|
|
return badRequest(res);
|
2020-08-01 04:05:14 +02:00
|
|
|
}
|
|
|
|
|
2020-08-28 03:44:20 +02:00
|
|
|
const rankings = await getRankings(
|
|
|
|
websiteId,
|
|
|
|
startDate,
|
|
|
|
endDate,
|
|
|
|
getColumn(type),
|
|
|
|
getTable(type),
|
|
|
|
);
|
2020-08-01 04:05:14 +02:00
|
|
|
|
2020-08-08 02:19:42 +02:00
|
|
|
return ok(res, rankings);
|
2020-08-01 04:05:14 +02:00
|
|
|
};
|