umami/pages/api/website/[id]/metrics.js

19 lines
496 B
JavaScript
Raw Normal View History

2020-08-12 07:24:41 +02:00
import { getMetrics } from 'lib/queries';
import { ok } from 'lib/response';
2020-07-28 08:52:14 +02:00
export default async (req, res) => {
const { id, start_at, end_at } = req.query;
2020-09-01 06:11:53 +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-01 06:11:53 +02:00
const metrics = await getMetrics(websiteId, startDate, endDate);
2020-07-28 08:52:14 +02:00
2020-07-31 05:11:43 +02:00
const stats = Object.keys(metrics[0]).reduce((obj, key) => {
2020-09-01 06:11:53 +02:00
obj[key] = Number(metrics[0][key]) || 0;
return obj;
}, {});
return ok(res, stats);
2020-07-28 08:52:14 +02:00
};