mirror of
https://github.com/umami-software/umami.git
synced 2026-02-08 14:47:14 +01:00
Merge branch 'dev' into hosts-support
This commit is contained in:
commit
d1559c3a98
281 changed files with 7555 additions and 1973 deletions
|
|
@ -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 });
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}, {});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue