mirror of
https://github.com/umami-software/umami.git
synced 2026-02-13 09:05:36 +01:00
Moved code into src folder. Added build for component library.
This commit is contained in:
parent
7a7233ead4
commit
ede658771e
490 changed files with 749 additions and 442 deletions
103
src/pages/api/websites/[id]/stats.ts
Normal file
103
src/pages/api/websites/[id]/stats.ts
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
import { subMinutes, differenceInMinutes } from 'date-fns';
|
||||
import { NextApiResponse } from 'next';
|
||||
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
|
||||
import { canViewWebsite } from 'lib/auth';
|
||||
import { useAuth, useCors, useValidate } from 'lib/middleware';
|
||||
import { NextApiRequestQueryBody, WebsiteStats } from 'lib/types';
|
||||
import { parseDateRangeQuery } from 'lib/query';
|
||||
import { getWebsiteStats } from 'queries';
|
||||
|
||||
export interface WebsiteStatsRequestQuery {
|
||||
id: string;
|
||||
startAt: number;
|
||||
endAt: number;
|
||||
url: string;
|
||||
referrer: string;
|
||||
title: string;
|
||||
query: string;
|
||||
event: string;
|
||||
os: string;
|
||||
browser: string;
|
||||
device: string;
|
||||
country: string;
|
||||
region: string;
|
||||
city: string;
|
||||
}
|
||||
|
||||
import * as yup from 'yup';
|
||||
const schema = {
|
||||
GET: yup.object().shape({
|
||||
id: yup.string().uuid().required(),
|
||||
}),
|
||||
};
|
||||
|
||||
export default async (
|
||||
req: NextApiRequestQueryBody<WebsiteStatsRequestQuery>,
|
||||
res: NextApiResponse<WebsiteStats>,
|
||||
) => {
|
||||
await useCors(req, res);
|
||||
await useAuth(req, res);
|
||||
|
||||
req.yup = schema;
|
||||
await useValidate(req, res);
|
||||
|
||||
const {
|
||||
id: websiteId,
|
||||
url,
|
||||
referrer,
|
||||
title,
|
||||
query,
|
||||
event,
|
||||
os,
|
||||
browser,
|
||||
device,
|
||||
country,
|
||||
region,
|
||||
city,
|
||||
} = req.query;
|
||||
|
||||
if (req.method === 'GET') {
|
||||
if (!(await canViewWebsite(req.auth, websiteId))) {
|
||||
return unauthorized(res);
|
||||
}
|
||||
|
||||
const { startDate, endDate } = await parseDateRangeQuery(req);
|
||||
const diff = differenceInMinutes(endDate, startDate);
|
||||
const prevStartDate = subMinutes(startDate, diff);
|
||||
const prevEndDate = subMinutes(endDate, diff);
|
||||
|
||||
const filters = {
|
||||
url,
|
||||
referrer,
|
||||
title,
|
||||
query,
|
||||
event,
|
||||
os,
|
||||
browser,
|
||||
device,
|
||||
country,
|
||||
region,
|
||||
city,
|
||||
};
|
||||
|
||||
const metrics = await getWebsiteStats(websiteId, { ...filters, startDate, endDate });
|
||||
|
||||
const prevPeriod = await getWebsiteStats(websiteId, {
|
||||
...filters,
|
||||
startDate: prevStartDate,
|
||||
endDate: prevEndDate,
|
||||
});
|
||||
|
||||
const stats = Object.keys(metrics[0]).reduce((obj, key) => {
|
||||
obj[key] = {
|
||||
value: Number(metrics[0][key]) || 0,
|
||||
change: Number(metrics[0][key]) - Number(prevPeriod[0][key]) || 0,
|
||||
};
|
||||
return obj;
|
||||
}, {});
|
||||
|
||||
return ok(res, stats);
|
||||
}
|
||||
|
||||
return methodNotAllowed(res);
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue