umami/pages/api/website/[id]/rankings.js
2020-08-27 19:11:36 -07:00

29 lines
846 B
JavaScript

import { getRankings, getEvents } from 'lib/queries';
import { ok, badRequest } from 'lib/response';
const sessionColumns = ['browser', 'os', 'device', 'country'];
const pageviewColumns = ['url', 'referrer'];
export default async (req, res) => {
const { id, type, start_at, end_at } = req.query;
const websiteId = +id;
const startDate = new Date(+start_at);
const endDate = new Date(+end_at);
if (type !== 'event' && !sessionColumns.includes(type) && !pageviewColumns.includes(type)) {
return badRequest(res);
}
if (type === 'event') {
const events = await getEvents(websiteId, startDate, endDate);
return ok(res, events);
}
const table = sessionColumns.includes(type) ? 'session' : 'pageview';
const rankings = await getRankings(websiteId, startDate, endDate, type, table);
return ok(res, rankings);
};