mirror of
https://github.com/umami-software/umami.git
synced 2026-02-10 23:57:12 +01:00
Event data updates.
This commit is contained in:
parent
d1e93b8e62
commit
55701c9e8c
15 changed files with 642 additions and 693 deletions
45
pages/api/event-data/stats.ts
Normal file
45
pages/api/event-data/stats.ts
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
import { canViewWebsite } from 'lib/auth';
|
||||
import { useCors, useAuth } from 'lib/middleware';
|
||||
import { NextApiRequestQueryBody } from 'lib/types';
|
||||
import { NextApiResponse } from 'next';
|
||||
import { ok, methodNotAllowed, unauthorized } from 'next-basics';
|
||||
import { getEventDataFields } from 'queries';
|
||||
|
||||
export interface EventDataRequestBody {
|
||||
websiteId: string;
|
||||
dateRange: {
|
||||
startDate: string;
|
||||
endDate: string;
|
||||
};
|
||||
field?: string;
|
||||
}
|
||||
|
||||
export default async (
|
||||
req: NextApiRequestQueryBody<any, EventDataRequestBody>,
|
||||
res: NextApiResponse<any>,
|
||||
) => {
|
||||
await useCors(req, res);
|
||||
await useAuth(req, res);
|
||||
|
||||
if (req.method === 'GET') {
|
||||
const { websiteId, startAt, endAt } = req.query;
|
||||
|
||||
if (!(await canViewWebsite(req.auth, websiteId))) {
|
||||
return unauthorized(res);
|
||||
}
|
||||
|
||||
const results = await getEventDataFields(websiteId, new Date(+startAt), new Date(+endAt));
|
||||
|
||||
const data = results.reduce(
|
||||
(obj, row) => {
|
||||
obj.records += row.total;
|
||||
return obj;
|
||||
},
|
||||
{ fields: results.length, records: 0 },
|
||||
);
|
||||
|
||||
return ok(res, data);
|
||||
}
|
||||
|
||||
return methodNotAllowed(res);
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue