mirror of
https://github.com/umami-software/umami.git
synced 2026-02-15 18:15:35 +01:00
Use getRequestDateRange in all routes.
This commit is contained in:
parent
b0023feee9
commit
5171bdaf47
21 changed files with 72 additions and 173 deletions
|
|
@ -1,25 +0,0 @@
|
||||||
.container {
|
|
||||||
display: grid;
|
|
||||||
grid-template-columns: repeat(auto-fill, minmax(420px, 1fr));
|
|
||||||
gap: 60px;
|
|
||||||
margin-bottom: 40px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.table {
|
|
||||||
align-self: flex-start;
|
|
||||||
}
|
|
||||||
|
|
||||||
.link:hover {
|
|
||||||
cursor: pointer;
|
|
||||||
color: var(--primary-color);
|
|
||||||
}
|
|
||||||
|
|
||||||
.title {
|
|
||||||
text-align: center;
|
|
||||||
font-weight: bold;
|
|
||||||
margin: 20px 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
.chart {
|
|
||||||
min-height: 620px;
|
|
||||||
}
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import { DataColumn, DataTable } from '@umami/react-zen';
|
import { Grid, DataColumn, DataTable } from '@umami/react-zen';
|
||||||
import {
|
import {
|
||||||
useSessionDataPropertiesQuery,
|
useSessionDataPropertiesQuery,
|
||||||
useSessionDataValuesQuery,
|
useSessionDataValuesQuery,
|
||||||
|
|
@ -8,12 +8,11 @@ import { LoadingPanel } from '@/components/common/LoadingPanel';
|
||||||
import { PieChart } from '@/components/charts/PieChart';
|
import { PieChart } from '@/components/charts/PieChart';
|
||||||
import { useState } from 'react';
|
import { useState } from 'react';
|
||||||
import { CHART_COLORS } from '@/lib/constants';
|
import { CHART_COLORS } from '@/lib/constants';
|
||||||
import styles from './SessionProperties.module.css';
|
|
||||||
|
|
||||||
export function SessionProperties({ websiteId }: { websiteId: string }) {
|
export function SessionProperties({ websiteId }: { websiteId: string }) {
|
||||||
const [propertyName, setPropertyName] = useState('');
|
const [propertyName, setPropertyName] = useState('');
|
||||||
const { formatMessage, labels } = useMessages();
|
const { formatMessage, labels } = useMessages();
|
||||||
const { data, isLoading, isFetched, error } = useSessionDataPropertiesQuery(websiteId);
|
const { data, isLoading, isFetching, error } = useSessionDataPropertiesQuery(websiteId);
|
||||||
const { data: values } = useSessionDataValuesQuery(websiteId, propertyName);
|
const { data: values } = useSessionDataValuesQuery(websiteId, propertyName);
|
||||||
const chartData =
|
const chartData =
|
||||||
propertyName && values
|
propertyName && values
|
||||||
|
|
@ -30,25 +29,23 @@ export function SessionProperties({ websiteId }: { websiteId: string }) {
|
||||||
: null;
|
: null;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<LoadingPanel isLoading={isLoading} isFetched={isFetched} error={error}>
|
<LoadingPanel data={data} isLoading={isLoading} isFetching={isFetching} error={error}>
|
||||||
<div className={styles.container}>
|
<Grid>
|
||||||
<DataTable data={data} className={styles.table}>
|
<DataTable data={data}>
|
||||||
<DataColumn id="propertyName" label={formatMessage(labels.property)}>
|
<DataColumn id="propertyName" label={formatMessage(labels.property)}>
|
||||||
{(row: any) => (
|
{(row: any) => (
|
||||||
<div className={styles.link} onClick={() => setPropertyName(row.propertyName)}>
|
<div onClick={() => setPropertyName(row.propertyName)}>{row.propertyName}</div>
|
||||||
{row.propertyName}
|
|
||||||
</div>
|
|
||||||
)}
|
)}
|
||||||
</DataColumn>
|
</DataColumn>
|
||||||
<DataColumn id="total" label={formatMessage(labels.count)} align="end" />
|
<DataColumn id="total" label={formatMessage(labels.count)} align="end" />
|
||||||
</DataTable>
|
</DataTable>
|
||||||
{propertyName && (
|
{propertyName && (
|
||||||
<div className={styles.chart}>
|
<div>
|
||||||
<div className={styles.title}>{propertyName}</div>
|
<div>{propertyName}</div>
|
||||||
<PieChart key={propertyName} type="doughnut" chartData={chartData} />
|
<PieChart key={propertyName} type="doughnut" chartData={chartData} />
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
</div>
|
</Grid>
|
||||||
</LoadingPanel>
|
</LoadingPanel>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +0,0 @@
|
||||||
.link {
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
gap: 20px;
|
|
||||||
}
|
|
||||||
|
|
@ -2,7 +2,6 @@ import Link from 'next/link';
|
||||||
import { DataColumn, DataTable } from '@umami/react-zen';
|
import { DataColumn, DataTable } from '@umami/react-zen';
|
||||||
import { useFormat, useMessages, useTimezone } from '@/components/hooks';
|
import { useFormat, useMessages, useTimezone } from '@/components/hooks';
|
||||||
import { Avatar } from '@/components/common/Avatar';
|
import { Avatar } from '@/components/common/Avatar';
|
||||||
import styles from './SessionsTable.module.css';
|
|
||||||
import { TypeIcon } from '@/components/common/TypeIcon';
|
import { TypeIcon } from '@/components/common/TypeIcon';
|
||||||
|
|
||||||
export function SessionsTable({ data = [] }: { data: any[]; showDomain?: boolean }) {
|
export function SessionsTable({ data = [] }: { data: any[]; showDomain?: boolean }) {
|
||||||
|
|
@ -14,7 +13,7 @@ export function SessionsTable({ data = [] }: { data: any[]; showDomain?: boolean
|
||||||
<DataTable data={data}>
|
<DataTable data={data}>
|
||||||
<DataColumn id="id" label={formatMessage(labels.session)} width="100px">
|
<DataColumn id="id" label={formatMessage(labels.session)} width="100px">
|
||||||
{(row: any) => (
|
{(row: any) => (
|
||||||
<Link href={`sessions/${row.id}`} className={styles.link}>
|
<Link href={`sessions/${row.id}`}>
|
||||||
<Avatar seed={row.id} size={64} />
|
<Avatar seed={row.id} size={64} />
|
||||||
</Link>
|
</Link>
|
||||||
)}
|
)}
|
||||||
|
|
|
||||||
|
|
@ -1,43 +0,0 @@
|
||||||
.week {
|
|
||||||
display: flex;
|
|
||||||
justify-content: space-between;
|
|
||||||
position: relative;
|
|
||||||
}
|
|
||||||
|
|
||||||
.header {
|
|
||||||
text-align: center;
|
|
||||||
font-weight: 700;
|
|
||||||
margin-bottom: 10px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.day {
|
|
||||||
display: flex;
|
|
||||||
flex-direction: column;
|
|
||||||
align-items: flex-start;
|
|
||||||
justify-content: flex-start;
|
|
||||||
gap: 1px;
|
|
||||||
position: relative;
|
|
||||||
}
|
|
||||||
|
|
||||||
.cell {
|
|
||||||
display: flex;
|
|
||||||
background-color: var(--base-color-2);
|
|
||||||
width: 20px;
|
|
||||||
height: 20px;
|
|
||||||
margin: auto;
|
|
||||||
border-radius: 100%;
|
|
||||||
align-items: flex-start;
|
|
||||||
}
|
|
||||||
|
|
||||||
.hour {
|
|
||||||
font-weight: 700;
|
|
||||||
color: var(--font-color);
|
|
||||||
height: 20px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.block {
|
|
||||||
background-color: var(--primary-color);
|
|
||||||
width: 20px;
|
|
||||||
height: 20px;
|
|
||||||
border-radius: 100%;
|
|
||||||
}
|
|
||||||
|
|
@ -14,7 +14,7 @@ export function SessionsWeekly({ websiteId }: { websiteId: string }) {
|
||||||
.fill(weekStartsOn)
|
.fill(weekStartsOn)
|
||||||
.map((d, i) => (d + i) % 7);
|
.map((d, i) => (d + i) % 7);
|
||||||
|
|
||||||
const [, max] = data
|
const [, max = 1] = data
|
||||||
? data.reduce((arr: number[], hours: number[], index: number) => {
|
? data.reduce((arr: number[], hours: number[], index: number) => {
|
||||||
const min = Math.min(...hours);
|
const min = Math.min(...hours);
|
||||||
const max = Math.max(...hours);
|
const max = Math.max(...hours);
|
||||||
|
|
@ -69,17 +69,25 @@ export function SessionsWeekly({ websiteId }: { websiteId: string }) {
|
||||||
key={index}
|
key={index}
|
||||||
gap="1"
|
gap="1"
|
||||||
>
|
>
|
||||||
<Row marginBottom="3">
|
<Row alignItems="center" justifyContent="center" marginBottom="3">
|
||||||
<Text weight="bold" align="center">
|
<Text weight="bold" align="center">
|
||||||
{format(getDayOfWeekAsDate(index), 'EEE', { locale: dateLocale })}
|
{format(getDayOfWeekAsDate(index), 'EEE', { locale: dateLocale })}
|
||||||
</Text>
|
</Text>
|
||||||
</Row>
|
</Row>
|
||||||
{day?.map((count: number, j) => {
|
{day?.map((count: number, j) => {
|
||||||
const pct = count / max;
|
const pct = max ? count / max : 0;
|
||||||
return (
|
return (
|
||||||
<TooltipTrigger key={j} delay={0} isDisabled={count <= 0}>
|
<TooltipTrigger key={j} delay={0} isDisabled={count <= 0}>
|
||||||
<Focusable>
|
<Focusable>
|
||||||
<Row backgroundColor="2" width="20px" height="20px" borderRadius="full">
|
<Row
|
||||||
|
alignItems="center"
|
||||||
|
justifyContent="center"
|
||||||
|
backgroundColor="2"
|
||||||
|
width="20px"
|
||||||
|
height="20px"
|
||||||
|
borderRadius="full"
|
||||||
|
style={{ margin: '0 auto' }}
|
||||||
|
>
|
||||||
<Row
|
<Row
|
||||||
backgroundColor="primary"
|
backgroundColor="primary"
|
||||||
width="20px"
|
width="20px"
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ import { json, unauthorized } from '@/lib/response';
|
||||||
import { getAllUserWebsitesIncludingTeamOwner } from '@/queries/prisma/website';
|
import { getAllUserWebsitesIncludingTeamOwner } from '@/queries/prisma/website';
|
||||||
import { getEventUsage } from '@/queries/sql/events/getEventUsage';
|
import { getEventUsage } from '@/queries/sql/events/getEventUsage';
|
||||||
import { getEventDataUsage } from '@/queries/sql/events/getEventDataUsage';
|
import { getEventDataUsage } from '@/queries/sql/events/getEventDataUsage';
|
||||||
import { parseRequest } from '@/lib/request';
|
import { parseRequest, getRequestDateRange } from '@/lib/request';
|
||||||
|
|
||||||
export async function GET(request: Request, { params }: { params: Promise<{ userId: string }> }) {
|
export async function GET(request: Request, { params }: { params: Promise<{ userId: string }> }) {
|
||||||
const schema = z.object({
|
const schema = z.object({
|
||||||
|
|
@ -22,10 +22,7 @@ export async function GET(request: Request, { params }: { params: Promise<{ user
|
||||||
}
|
}
|
||||||
|
|
||||||
const { userId } = await params;
|
const { userId } = await params;
|
||||||
const { startAt, endAt } = query;
|
const { startDate, endDate } = await getRequestDateRange(query);
|
||||||
|
|
||||||
const startDate = new Date(+startAt);
|
|
||||||
const endDate = new Date(+endAt);
|
|
||||||
|
|
||||||
const websites = await getAllUserWebsitesIncludingTeamOwner(userId);
|
const websites = await getAllUserWebsitesIncludingTeamOwner(userId);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import { parseRequest } from '@/lib/request';
|
import { getRequestDateRange, parseRequest } from '@/lib/request';
|
||||||
import { unauthorized, json } from '@/lib/response';
|
import { unauthorized, json } from '@/lib/response';
|
||||||
import { canViewWebsite } from '@/lib/auth';
|
import { canViewWebsite } from '@/lib/auth';
|
||||||
import { getEventDataEvents } from '@/queries/sql/events/getEventDataEvents';
|
import { getEventDataEvents } from '@/queries/sql/events/getEventDataEvents';
|
||||||
|
|
@ -20,15 +20,13 @@ export async function GET(
|
||||||
}
|
}
|
||||||
|
|
||||||
const { websiteId } = await params;
|
const { websiteId } = await params;
|
||||||
const { startAt, endAt, event } = query;
|
const { event } = query;
|
||||||
|
const { startDate, endDate } = await getRequestDateRange(query);
|
||||||
|
|
||||||
if (!(await canViewWebsite(auth, websiteId))) {
|
if (!(await canViewWebsite(auth, websiteId))) {
|
||||||
return unauthorized();
|
return unauthorized();
|
||||||
}
|
}
|
||||||
|
|
||||||
const startDate = new Date(+startAt);
|
|
||||||
const endDate = new Date(+endAt);
|
|
||||||
|
|
||||||
const data = await getEventDataEvents(websiteId, {
|
const data = await getEventDataEvents(websiteId, {
|
||||||
startDate,
|
startDate,
|
||||||
endDate,
|
endDate,
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import { parseRequest } from '@/lib/request';
|
import { getRequestDateRange, parseRequest } from '@/lib/request';
|
||||||
import { unauthorized, json } from '@/lib/response';
|
import { unauthorized, json } from '@/lib/response';
|
||||||
import { canViewWebsite } from '@/lib/auth';
|
import { canViewWebsite } from '@/lib/auth';
|
||||||
import { getEventDataFields } from '@/queries';
|
import { getEventDataFields } from '@/queries';
|
||||||
|
|
@ -20,15 +20,12 @@ export async function GET(
|
||||||
}
|
}
|
||||||
|
|
||||||
const { websiteId } = await params;
|
const { websiteId } = await params;
|
||||||
const { startAt, endAt } = query;
|
const { startDate, endDate } = await getRequestDateRange(query);
|
||||||
|
|
||||||
if (!(await canViewWebsite(auth, websiteId))) {
|
if (!(await canViewWebsite(auth, websiteId))) {
|
||||||
return unauthorized();
|
return unauthorized();
|
||||||
}
|
}
|
||||||
|
|
||||||
const startDate = new Date(+startAt);
|
|
||||||
const endDate = new Date(+endAt);
|
|
||||||
|
|
||||||
const data = await getEventDataFields(websiteId, {
|
const data = await getEventDataFields(websiteId, {
|
||||||
startDate,
|
startDate,
|
||||||
endDate,
|
endDate,
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import { parseRequest } from '@/lib/request';
|
import { getRequestDateRange, parseRequest } from '@/lib/request';
|
||||||
import { unauthorized, json } from '@/lib/response';
|
import { unauthorized, json } from '@/lib/response';
|
||||||
import { canViewWebsite } from '@/lib/auth';
|
import { canViewWebsite } from '@/lib/auth';
|
||||||
import { getEventDataProperties } from '@/queries';
|
import { getEventDataProperties } from '@/queries';
|
||||||
|
|
@ -21,15 +21,13 @@ export async function GET(
|
||||||
}
|
}
|
||||||
|
|
||||||
const { websiteId } = await params;
|
const { websiteId } = await params;
|
||||||
const { startAt, endAt, propertyName } = query;
|
const { propertyName } = query;
|
||||||
|
const { startDate, endDate } = await getRequestDateRange(query);
|
||||||
|
|
||||||
if (!(await canViewWebsite(auth, websiteId))) {
|
if (!(await canViewWebsite(auth, websiteId))) {
|
||||||
return unauthorized();
|
return unauthorized();
|
||||||
}
|
}
|
||||||
|
|
||||||
const startDate = new Date(+startAt);
|
|
||||||
const endDate = new Date(+endAt);
|
|
||||||
|
|
||||||
const data = await getEventDataProperties(websiteId, { startDate, endDate, propertyName });
|
const data = await getEventDataProperties(websiteId, { startDate, endDate, propertyName });
|
||||||
|
|
||||||
return json(data);
|
return json(data);
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import { parseRequest } from '@/lib/request';
|
import { getRequestDateRange, parseRequest } from '@/lib/request';
|
||||||
import { unauthorized, json } from '@/lib/response';
|
import { unauthorized, json } from '@/lib/response';
|
||||||
import { canViewWebsite } from '@/lib/auth';
|
import { canViewWebsite } from '@/lib/auth';
|
||||||
import { getEventDataStats } from '@/queries';
|
import { getEventDataStats } from '@/queries';
|
||||||
|
|
@ -21,15 +21,12 @@ export async function GET(
|
||||||
}
|
}
|
||||||
|
|
||||||
const { websiteId } = await params;
|
const { websiteId } = await params;
|
||||||
const { startAt, endAt } = query;
|
const { startDate, endDate } = await getRequestDateRange(query);
|
||||||
|
|
||||||
if (!(await canViewWebsite(auth, websiteId))) {
|
if (!(await canViewWebsite(auth, websiteId))) {
|
||||||
return unauthorized();
|
return unauthorized();
|
||||||
}
|
}
|
||||||
|
|
||||||
const startDate = new Date(+startAt);
|
|
||||||
const endDate = new Date(+endAt);
|
|
||||||
|
|
||||||
const data = await getEventDataStats(websiteId, { startDate, endDate });
|
const data = await getEventDataStats(websiteId, { startDate, endDate });
|
||||||
|
|
||||||
return json(data);
|
return json(data);
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import { parseRequest } from '@/lib/request';
|
import { getRequestDateRange, parseRequest } from '@/lib/request';
|
||||||
import { unauthorized, json } from '@/lib/response';
|
import { unauthorized, json } from '@/lib/response';
|
||||||
import { canViewWebsite } from '@/lib/auth';
|
import { canViewWebsite } from '@/lib/auth';
|
||||||
import { getEventDataValues } from '@/queries';
|
import { getEventDataValues } from '@/queries';
|
||||||
|
|
@ -22,15 +22,13 @@ export async function GET(
|
||||||
}
|
}
|
||||||
|
|
||||||
const { websiteId } = await params;
|
const { websiteId } = await params;
|
||||||
const { startAt, endAt, eventName, propertyName } = query;
|
const { eventName, propertyName } = query;
|
||||||
|
const { startDate, endDate } = await getRequestDateRange(query);
|
||||||
|
|
||||||
if (!(await canViewWebsite(auth, websiteId))) {
|
if (!(await canViewWebsite(auth, websiteId))) {
|
||||||
return unauthorized();
|
return unauthorized();
|
||||||
}
|
}
|
||||||
|
|
||||||
const startDate = new Date(+startAt);
|
|
||||||
const endDate = new Date(+endAt);
|
|
||||||
|
|
||||||
const data = await getEventDataValues(websiteId, {
|
const data = await getEventDataValues(websiteId, {
|
||||||
startDate,
|
startDate,
|
||||||
endDate,
|
endDate,
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import { parseRequest } from '@/lib/request';
|
import { getRequestDateRange, parseRequest } from '@/lib/request';
|
||||||
import { unauthorized, json } from '@/lib/response';
|
import { unauthorized, json } from '@/lib/response';
|
||||||
import { canViewWebsite } from '@/lib/auth';
|
import { canViewWebsite } from '@/lib/auth';
|
||||||
import { pagingParams } from '@/lib/schema';
|
import { pagingParams } from '@/lib/schema';
|
||||||
|
|
@ -22,15 +22,12 @@ export async function GET(
|
||||||
}
|
}
|
||||||
|
|
||||||
const { websiteId } = await params;
|
const { websiteId } = await params;
|
||||||
const { startAt, endAt } = query;
|
const { startDate, endDate } = await getRequestDateRange(query);
|
||||||
|
|
||||||
if (!(await canViewWebsite(auth, websiteId))) {
|
if (!(await canViewWebsite(auth, websiteId))) {
|
||||||
return unauthorized();
|
return unauthorized();
|
||||||
}
|
}
|
||||||
|
|
||||||
const startDate = new Date(+startAt);
|
|
||||||
const endDate = new Date(+endAt);
|
|
||||||
|
|
||||||
const data = await getWebsiteEvents(websiteId, { startDate, endDate }, query);
|
const data = await getWebsiteEvents(websiteId, { startDate, endDate }, query);
|
||||||
|
|
||||||
return json(data);
|
return json(data);
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ export async function GET(
|
||||||
const schema = z.object({
|
const schema = z.object({
|
||||||
startAt: z.coerce.number().int(),
|
startAt: z.coerce.number().int(),
|
||||||
endAt: z.coerce.number().int(),
|
endAt: z.coerce.number().int(),
|
||||||
unit: unitParam,
|
unit: unitParam.optional(),
|
||||||
timezone: timezoneParam,
|
timezone: timezoneParam,
|
||||||
...filterParams,
|
...filterParams,
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import { parseRequest } from '@/lib/request';
|
import { getRequestDateRange, parseRequest } from '@/lib/request';
|
||||||
import { unauthorized, json } from '@/lib/response';
|
import { unauthorized, json } from '@/lib/response';
|
||||||
import { canViewWebsite } from '@/lib/auth';
|
import { canViewWebsite } from '@/lib/auth';
|
||||||
import { getSessionDataProperties } from '@/queries';
|
import { getSessionDataProperties } from '@/queries';
|
||||||
|
|
@ -20,16 +20,14 @@ export async function GET(
|
||||||
return error();
|
return error();
|
||||||
}
|
}
|
||||||
|
|
||||||
const { startAt, endAt, propertyName } = query;
|
|
||||||
const { websiteId } = await params;
|
const { websiteId } = await params;
|
||||||
|
const { propertyName } = query;
|
||||||
|
const { startDate, endDate } = await getRequestDateRange(query);
|
||||||
|
|
||||||
if (!(await canViewWebsite(auth, websiteId))) {
|
if (!(await canViewWebsite(auth, websiteId))) {
|
||||||
return unauthorized();
|
return unauthorized();
|
||||||
}
|
}
|
||||||
|
|
||||||
const startDate = new Date(+startAt);
|
|
||||||
const endDate = new Date(+endAt);
|
|
||||||
|
|
||||||
const data = await getSessionDataProperties(websiteId, { startDate, endDate, propertyName });
|
const data = await getSessionDataProperties(websiteId, { startDate, endDate, propertyName });
|
||||||
|
|
||||||
return json(data);
|
return json(data);
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import { canViewWebsite } from '@/lib/auth';
|
import { canViewWebsite } from '@/lib/auth';
|
||||||
import { parseRequest } from '@/lib/request';
|
import { getRequestDateRange, parseRequest } from '@/lib/request';
|
||||||
import { json, unauthorized } from '@/lib/response';
|
import { json, unauthorized } from '@/lib/response';
|
||||||
import { getSessionDataValues } from '@/queries';
|
import { getSessionDataValues } from '@/queries';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
@ -20,16 +20,14 @@ export async function GET(
|
||||||
return error();
|
return error();
|
||||||
}
|
}
|
||||||
|
|
||||||
const { startAt, endAt, propertyName } = query;
|
const { propertyName } = query;
|
||||||
const { websiteId } = await params;
|
const { websiteId } = await params;
|
||||||
|
const { startDate, endDate } = await getRequestDateRange(query);
|
||||||
|
|
||||||
if (!(await canViewWebsite(auth, websiteId))) {
|
if (!(await canViewWebsite(auth, websiteId))) {
|
||||||
return unauthorized();
|
return unauthorized();
|
||||||
}
|
}
|
||||||
|
|
||||||
const startDate = new Date(+startAt);
|
|
||||||
const endDate = new Date(+endAt);
|
|
||||||
|
|
||||||
const data = await getSessionDataValues(websiteId, {
|
const data = await getSessionDataValues(websiteId, {
|
||||||
startDate,
|
startDate,
|
||||||
endDate,
|
endDate,
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import { parseRequest } from '@/lib/request';
|
import { parseRequest, getRequestDateRange } from '@/lib/request';
|
||||||
import { unauthorized, json } from '@/lib/response';
|
import { unauthorized, json } from '@/lib/response';
|
||||||
import { canViewWebsite } from '@/lib/auth';
|
import { canViewWebsite } from '@/lib/auth';
|
||||||
import { getSessionActivity } from '@/queries';
|
import { getSessionActivity } from '@/queries';
|
||||||
|
|
@ -20,15 +20,12 @@ export async function GET(
|
||||||
}
|
}
|
||||||
|
|
||||||
const { websiteId, sessionId } = await params;
|
const { websiteId, sessionId } = await params;
|
||||||
const { startAt, endAt } = query;
|
const { startDate, endDate } = await getRequestDateRange(query);
|
||||||
|
|
||||||
if (!(await canViewWebsite(auth, websiteId))) {
|
if (!(await canViewWebsite(auth, websiteId))) {
|
||||||
return unauthorized();
|
return unauthorized();
|
||||||
}
|
}
|
||||||
|
|
||||||
const startDate = new Date(+startAt);
|
|
||||||
const endDate = new Date(+endAt);
|
|
||||||
|
|
||||||
const data = await getSessionActivity(websiteId, sessionId, startDate, endDate);
|
const data = await getSessionActivity(websiteId, sessionId, startDate, endDate);
|
||||||
|
|
||||||
return json(data);
|
return json(data);
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import { parseRequest } from '@/lib/request';
|
import { getRequestDateRange, parseRequest } from '@/lib/request';
|
||||||
import { unauthorized, json } from '@/lib/response';
|
import { unauthorized, json } from '@/lib/response';
|
||||||
import { canViewWebsite } from '@/lib/auth';
|
import { canViewWebsite } from '@/lib/auth';
|
||||||
import { pagingParams } from '@/lib/schema';
|
import { pagingParams } from '@/lib/schema';
|
||||||
|
|
@ -21,16 +21,13 @@ export async function GET(
|
||||||
return error();
|
return error();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const { startDate, endDate } = await getRequestDateRange(query);
|
||||||
const { websiteId } = await params;
|
const { websiteId } = await params;
|
||||||
const { startAt, endAt } = query;
|
|
||||||
|
|
||||||
if (!(await canViewWebsite(auth, websiteId))) {
|
if (!(await canViewWebsite(auth, websiteId))) {
|
||||||
return unauthorized();
|
return unauthorized();
|
||||||
}
|
}
|
||||||
|
|
||||||
const startDate = new Date(+startAt);
|
|
||||||
const endDate = new Date(+endAt);
|
|
||||||
|
|
||||||
const data = await getWebsiteSessions(websiteId, { startDate, endDate }, query);
|
const data = await getWebsiteSessions(websiteId, { startDate, endDate }, query);
|
||||||
|
|
||||||
return json(data);
|
return json(data);
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import { parseRequest } from '@/lib/request';
|
import { getRequestDateRange, parseRequest } from '@/lib/request';
|
||||||
import { unauthorized, json } from '@/lib/response';
|
import { unauthorized, json } from '@/lib/response';
|
||||||
import { canViewWebsite } from '@/lib/auth';
|
import { canViewWebsite } from '@/lib/auth';
|
||||||
import { pagingParams, timezoneParam } from '@/lib/schema';
|
import { pagingParams, timezoneParam } from '@/lib/schema';
|
||||||
|
|
@ -23,15 +23,13 @@ export async function GET(
|
||||||
}
|
}
|
||||||
|
|
||||||
const { websiteId } = await params;
|
const { websiteId } = await params;
|
||||||
const { startAt, endAt, timezone } = query;
|
const { timezone } = query;
|
||||||
|
const { startDate, endDate } = await getRequestDateRange(query);
|
||||||
|
|
||||||
if (!(await canViewWebsite(auth, websiteId))) {
|
if (!(await canViewWebsite(auth, websiteId))) {
|
||||||
return unauthorized();
|
return unauthorized();
|
||||||
}
|
}
|
||||||
|
|
||||||
const startDate = new Date(+startAt);
|
|
||||||
const endDate = new Date(+endAt);
|
|
||||||
|
|
||||||
const data = await getWebsiteSessionsWeekly(websiteId, { startDate, endDate, timezone });
|
const data = await getWebsiteSessionsWeekly(websiteId, { startDate, endDate, timezone });
|
||||||
|
|
||||||
return json(data);
|
return json(data);
|
||||||
|
|
|
||||||
|
|
@ -310,7 +310,11 @@ export function getDayOfWeekAsDate(dayOfWeek: number) {
|
||||||
return currentDate;
|
return currentDate;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function formatDate(date: string | number | Date, dateFormat: string, locale = 'en-US') {
|
export function formatDate(
|
||||||
|
date: string | number | Date,
|
||||||
|
dateFormat: string = 'PPpp',
|
||||||
|
locale = 'en-US',
|
||||||
|
) {
|
||||||
return format(typeof date === 'string' ? new Date(date) : date, dateFormat, {
|
return format(typeof date === 'string' ? new Date(date) : date, dateFormat, {
|
||||||
locale: getDateLocale(locale),
|
locale: getDateLocale(locale),
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,8 @@
|
||||||
import { z } from 'zod/v4';
|
import { z } from 'zod/v4';
|
||||||
import { FILTER_COLUMNS } from '@/lib/constants';
|
import { FILTER_COLUMNS } from '@/lib/constants';
|
||||||
import { badRequest, unauthorized } from '@/lib/response';
|
import { badRequest, unauthorized } from '@/lib/response';
|
||||||
import { getAllowedUnits, getMinimumUnit } from '@/lib/date';
|
import { getAllowedUnits, getCompareDate, getMinimumUnit } from '@/lib/date';
|
||||||
import { checkAuth } from '@/lib/auth';
|
import { checkAuth } from '@/lib/auth';
|
||||||
import { getWebsiteDateRange } from '@/queries';
|
|
||||||
|
|
||||||
export async function parseRequest(
|
export async function parseRequest(
|
||||||
request: Request,
|
request: Request,
|
||||||
|
|
@ -48,31 +47,26 @@ export async function getJsonBody(request: Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getRequestDateRange(query: Record<string, any>) {
|
export async function getRequestDateRange(query: Record<string, string>) {
|
||||||
const { websiteId, startAt, endAt, unit } = query;
|
const { startAt, endAt, unit, compare } = query;
|
||||||
|
|
||||||
// All-time
|
|
||||||
if (+startAt === 0 && +endAt === 1) {
|
|
||||||
const result = await getWebsiteDateRange(websiteId as string);
|
|
||||||
const { min, max } = result[0];
|
|
||||||
const startDate = new Date(min);
|
|
||||||
const endDate = new Date(max);
|
|
||||||
|
|
||||||
return {
|
|
||||||
startDate,
|
|
||||||
endDate,
|
|
||||||
unit: getMinimumUnit(startDate, endDate),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const startDate = new Date(+startAt);
|
const startDate = new Date(+startAt);
|
||||||
const endDate = new Date(+endAt);
|
const endDate = new Date(+endAt);
|
||||||
const minUnit = getMinimumUnit(startDate, endDate);
|
|
||||||
|
const { startDate: compareStartDate, endDate: compareEndDate } = getCompareDate(
|
||||||
|
compare,
|
||||||
|
startDate,
|
||||||
|
endDate,
|
||||||
|
);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
startDate,
|
startDate,
|
||||||
endDate,
|
endDate,
|
||||||
unit: (getAllowedUnits(startDate, endDate).includes(unit as string) ? unit : minUnit) as string,
|
compareStartDate,
|
||||||
|
compareEndDate,
|
||||||
|
unit: getAllowedUnits(startDate, endDate).includes(unit)
|
||||||
|
? unit
|
||||||
|
: getMinimumUnit(startDate, endDate),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue