Skip to content

Commit

Permalink
Update page.tsx
Browse files Browse the repository at this point in the history
  • Loading branch information
et0and authored Oct 14, 2024
1 parent 3386517 commit 520acaa
Showing 1 changed file with 32 additions and 19 deletions.
51 changes: 32 additions & 19 deletions src/app/info/[[...slug]]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,45 @@ async function getPageViews(path: string) {
});

const viewsOverTime = await prisma.pageView.groupBy({
by: ["timestamp"],
by: [
{
year: {
extract: {
datepart: 'year',
from: 'timestamp',
},
},
},
{
month: {
extract: {
datepart: 'month',
from: 'timestamp',
},
},
},
],
where: {
pagePath: path,
filtered: false,
},
_count: {
id: true,
},
orderBy: {
timestamp: "asc",
},
take: 30,
orderBy: [
{ year: 'desc' },
{ month: 'desc' },
],
take: 12, // Show last 12 months
});

return {
totalPageViews,
last30DaysViews,
viewsOverTime: viewsOverTime.map(
(view: {
timestamp: { toISOString: () => any };
_count: { id: any };
}) => ({
date: view.timestamp.toISOString(),
count: view._count.id,
}),
),
viewsOverTime: viewsOverTime.map((view: any) => ({
date: new Date(view.year, view.month - 1, 1),
count: view._count.id,
})),
};
}

Expand Down Expand Up @@ -88,12 +101,12 @@ export default async function InfoPage({
<>
<p>Total Views: {pageViews.totalPageViews}</p>
<p>Views in last 30 days: {pageViews.last30DaysViews}</p>
<h2>Views over time</h2>
<h2 className="text-2xl mt-4 mb-2">Views by Month</h2>
<ul>
{pageViews.viewsOverTime.map(
({ date, count }: { date: string; count: number }) => (
<li key={date}>
{new Date(date).toLocaleDateString()}: {count} views
({ date, count }: { date: Date; count: number }) => (
<li key={date.toISOString()}>
{date.toLocaleString('default', { month: 'long', year: 'numeric' })}: {count} views
</li>
),
)}
Expand All @@ -102,4 +115,4 @@ export default async function InfoPage({
)}
</div>
);
}
}

0 comments on commit 520acaa

Please sign in to comment.