2022-10-11 02:01:48 +02:00
|
|
|
import { getWebsiteStats } from 'queries';
|
2022-08-29 05:20:54 +02:00
|
|
|
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
|
2020-10-09 00:02:48 +02:00
|
|
|
import { allowQuery } from 'lib/auth';
|
2022-10-12 22:11:44 +02:00
|
|
|
import { useAuth, useCors } from 'lib/middleware';
|
2022-10-25 19:45:56 +02:00
|
|
|
import { TYPE_WEBSITE } from 'lib/constants';
|
2022-11-15 22:21:14 +01:00
|
|
|
import { WebsiteStats } from 'interface/api/models';
|
|
|
|
import { NextApiRequestQueryBody } from 'interface/api/nextApi';
|
|
|
|
import { NextApiResponse } from 'next';
|
2020-10-09 00:02:48 +02:00
|
|
|
|
2022-11-15 22:21:14 +01:00
|
|
|
export interface WebsiteStatsReqeustQuery {
|
|
|
|
id: string;
|
|
|
|
type: string;
|
|
|
|
start_at: number;
|
|
|
|
end_at: number;
|
|
|
|
url: string;
|
|
|
|
referrer: string;
|
|
|
|
os: string;
|
|
|
|
browser: string;
|
|
|
|
device: string;
|
|
|
|
country: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default async (
|
|
|
|
req: NextApiRequestQueryBody<WebsiteStatsReqeustQuery>,
|
|
|
|
res: NextApiResponse<WebsiteStats>,
|
|
|
|
) => {
|
2022-10-12 22:11:44 +02:00
|
|
|
await useCors(req, res);
|
|
|
|
await useAuth(req, res);
|
2022-04-04 09:33:20 +02:00
|
|
|
|
2022-10-12 22:11:44 +02:00
|
|
|
if (req.method === 'GET') {
|
2022-10-25 19:45:56 +02:00
|
|
|
if (!(await allowQuery(req, TYPE_WEBSITE))) {
|
2020-10-09 00:02:48 +02:00
|
|
|
return unauthorized(res);
|
|
|
|
}
|
|
|
|
|
2022-10-12 04:37:38 +02:00
|
|
|
const {
|
|
|
|
id: websiteId,
|
|
|
|
start_at,
|
|
|
|
end_at,
|
|
|
|
url,
|
|
|
|
referrer,
|
|
|
|
os,
|
|
|
|
browser,
|
|
|
|
device,
|
|
|
|
country,
|
|
|
|
} = req.query;
|
2020-10-09 00:02:48 +02:00
|
|
|
|
|
|
|
const startDate = new Date(+start_at);
|
|
|
|
const endDate = new Date(+end_at);
|
|
|
|
|
2021-08-13 01:01:51 +02:00
|
|
|
const distance = end_at - start_at;
|
|
|
|
const prevStartDate = new Date(+start_at - distance);
|
|
|
|
const prevEndDate = new Date(+end_at - distance);
|
|
|
|
|
2022-10-11 02:01:48 +02:00
|
|
|
const metrics = await getWebsiteStats(websiteId, {
|
2022-09-24 07:43:51 +02:00
|
|
|
start_at: startDate,
|
|
|
|
end_at: endDate,
|
|
|
|
filters: {
|
|
|
|
url,
|
|
|
|
referrer,
|
|
|
|
os,
|
|
|
|
browser,
|
|
|
|
device,
|
|
|
|
country,
|
|
|
|
},
|
2022-04-10 12:51:43 +02:00
|
|
|
});
|
2022-10-11 02:01:48 +02:00
|
|
|
const prevPeriod = await getWebsiteStats(websiteId, {
|
2022-09-24 07:43:51 +02:00
|
|
|
start_at: prevStartDate,
|
|
|
|
end_at: prevEndDate,
|
|
|
|
filters: {
|
|
|
|
url,
|
|
|
|
referrer,
|
|
|
|
os,
|
|
|
|
browser,
|
|
|
|
device,
|
|
|
|
country,
|
|
|
|
},
|
2022-03-20 22:10:41 +01:00
|
|
|
});
|
2020-10-09 00:02:48 +02:00
|
|
|
|
|
|
|
const stats = Object.keys(metrics[0]).reduce((obj, key) => {
|
2021-08-13 01:01:51 +02:00
|
|
|
obj[key] = {
|
|
|
|
value: Number(metrics[0][key]) || 0,
|
2022-07-21 10:35:14 +02:00
|
|
|
change: Number(metrics[0][key]) - Number(prevPeriod[0][key]) || 0,
|
2021-08-13 01:01:51 +02:00
|
|
|
};
|
2020-10-09 00:02:48 +02:00
|
|
|
return obj;
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
return ok(res, stats);
|
|
|
|
}
|
|
|
|
|
|
|
|
return methodNotAllowed(res);
|
|
|
|
};
|