Merge branch 'dev' into hosts-support

This commit is contained in:
Mike Cao 2024-06-18 23:02:14 -07:00 committed by GitHub
commit d1559c3a98
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
281 changed files with 7555 additions and 1973 deletions

View file

@ -1,3 +1,4 @@
import * as yup from 'yup';
import { canViewWebsite } from 'lib/auth';
import { useAuth, useCors, useValidate } from 'lib/middleware';
import { getRequestFilters, getRequestDateRange } from 'lib/request';
@ -5,6 +6,8 @@ import { NextApiRequestQueryBody, WebsitePageviews } from 'lib/types';
import { NextApiResponse } from 'next';
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { getPageviewStats, getSessionStats } from 'queries';
import { TimezoneTest, UnitTypeTest } from 'lib/yup';
import { getCompareDate } from 'lib/date';
export interface WebsitePageviewRequestQuery {
websiteId: string;
@ -22,10 +25,9 @@ export interface WebsitePageviewRequestQuery {
country?: string;
region: string;
city?: string;
compare?: string;
}
import { TimezoneTest, UnitTypeTest } from 'lib/yup';
import * as yup from 'yup';
const schema = {
GET: yup.object().shape({
websiteId: yup.string().uuid().required(),
@ -43,6 +45,7 @@ const schema = {
country: yup.string(),
region: yup.string(),
city: yup.string(),
compare: yup.string(),
}),
};
@ -54,7 +57,7 @@ export default async (
await useAuth(req, res);
await useValidate(schema, req, res);
const { websiteId, timezone } = req.query;
const { websiteId, timezone, compare } = req.query;
if (req.method === 'GET') {
if (!(await canViewWebsite(req.auth, websiteId))) {
@ -76,6 +79,40 @@ export default async (
getSessionStats(websiteId, filters),
]);
if (compare) {
const { startDate: compareStartDate, endDate: compareEndDate } = getCompareDate(
compare,
startDate,
endDate,
);
const [comparePageviews, compareSessions] = await Promise.all([
getPageviewStats(websiteId, {
...filters,
startDate: compareStartDate,
endDate: compareEndDate,
}),
getSessionStats(websiteId, {
...filters,
startDate: compareStartDate,
endDate: compareEndDate,
}),
]);
return ok(res, {
pageviews,
sessions,
startDate,
endDate,
compare: {
pageviews: comparePageviews,
sessions: compareSessions,
startDate: compareStartDate,
endDate: compareEndDate,
},
});
}
return ok(res, { pageviews, sessions });
}

View file

@ -1,4 +1,4 @@
import { subMinutes, differenceInMinutes } from 'date-fns';
import * as yup from 'yup';
import { NextApiResponse } from 'next';
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { canViewWebsite } from 'lib/auth';
@ -6,6 +6,7 @@ import { useAuth, useCors, useValidate } from 'lib/middleware';
import { NextApiRequestQueryBody, WebsiteStats } from 'lib/types';
import { getRequestFilters, getRequestDateRange } from 'lib/request';
import { getWebsiteStats } from 'queries';
import { getCompareDate } from 'lib/date';
export interface WebsiteStatsRequestQuery {
websiteId: string;
@ -23,9 +24,9 @@ export interface WebsiteStatsRequestQuery {
country?: string;
region?: string;
city?: string;
compare?: string;
}
import * as yup from 'yup';
const schema = {
GET: yup.object().shape({
websiteId: yup.string().uuid().required(),
@ -43,6 +44,7 @@ const schema = {
country: yup.string(),
region: yup.string(),
city: yup.string(),
compare: yup.string(),
}),
};
@ -54,7 +56,7 @@ export default async (
await useAuth(req, res);
await useValidate(schema, req, res);
const { websiteId } = req.query;
const { websiteId, compare } = req.query;
if (req.method === 'GET') {
if (!(await canViewWebsite(req.auth, websiteId))) {
@ -62,9 +64,11 @@ export default async (
}
const { startDate, endDate } = await getRequestDateRange(req);
const diff = differenceInMinutes(endDate, startDate);
const prevStartDate = subMinutes(startDate, diff);
const prevEndDate = subMinutes(endDate, diff);
const { startDate: compareStartDate, endDate: compareEndDate } = getCompareDate(
compare,
startDate,
endDate,
);
const filters = getRequestFilters(req);
@ -72,14 +76,14 @@ export default async (
const prevPeriod = await getWebsiteStats(websiteId, {
...filters,
startDate: prevStartDate,
endDate: prevEndDate,
startDate: compareStartDate,
endDate: compareEndDate,
});
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,
prev: Number(prevPeriod[0][key]) || 0,
};
return obj;
}, {});