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

23 lines
505 B
JavaScript
Raw Normal View History

2020-07-31 05:11:43 +02:00
import { getMetrics } from 'lib/db';
2020-07-28 08:52:14 +02:00
import { useAuth } from 'lib/middleware';
import { ok } from 'lib/response';
2020-07-28 08:52:14 +02:00
export default async (req, res) => {
await useAuth(req, res);
const { id, start_at, end_at } = req.query;
2020-07-31 05:11:43 +02:00
const metrics = await getMetrics(
2020-07-29 09:16:02 +02:00
+id,
2020-07-30 09:06:29 +02:00
new Date(+start_at).toISOString(),
new Date(+end_at).toISOString(),
2020-07-29 09:16:02 +02:00
);
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) => {
obj[key] = +metrics[0][key];
return obj;
}, {});
return ok(res, stats);
2020-07-28 08:52:14 +02:00
};