diff --git a/components/WebsiteStats.js b/components/WebsiteStats.js index 0f3314a8..7896168a 100644 --- a/components/WebsiteStats.js +++ b/components/WebsiteStats.js @@ -33,7 +33,7 @@ export default function WebsiteStats({ websiteId, startDate, endDate, unit }) { return (
- +
); diff --git a/components/WebsiteSummary.js b/components/WebsiteSummary.js index fb8eb244..42e2c384 100644 --- a/components/WebsiteSummary.js +++ b/components/WebsiteSummary.js @@ -1,16 +1,30 @@ -import React from 'react'; +import React, { useState, useEffect } from 'react'; import MetricCard from './MetricCard'; +import { get } from '../lib/web'; import styles from './WebsiteSummary.module.css'; -function getTotal(data) { - return data.reduce((n, v) => n + v.y, 0); -} +export default function WebsiteSummary({ websiteId, startDate, endDate }) { + const [data, setData] = useState({}); + const { pageviews, uniques, bounces } = data; + + async function loadData() { + setData( + await get(`/api/website/${websiteId}/summary`, { + start_at: +startDate, + end_at: +endDate, + }), + ); + } + + useEffect(() => { + loadData(); + }, [startDate, endDate]); -export default function WebsiteSummary({ data }) { return (
- - + + +
); } diff --git a/lib/db.js b/lib/db.js index 198c52e2..b829c101 100644 --- a/lib/db.js +++ b/lib/db.js @@ -179,3 +179,33 @@ export async function getPageviewData( ), ); } + +export async function getSummary(website_id, start_at, end_at) { + return runQuery( + prisma.queryRaw( + ` + select + (select count(*) + from pageview + where website_id=${website_id} + and created_at between '${start_at}' and '${end_at}' + ) as "pageviews", + (select + count(distinct session_id) + from pageview + where website_id=${website_id} + and created_at between '${start_at}' and '${end_at}' + ) as "uniques", + (select sum(t.c) from + (select count(*) c + from pageview + where website_id=${website_id} + and created_at between '${start_at}' and '${end_at}' + group by session_id + having count(*) = 1 + ) t + ) as "bounces" + `, + ), + ); +} diff --git a/pages/api/website/[id]/summary.js b/pages/api/website/[id]/summary.js index b6d35dc3..a440b793 100644 --- a/pages/api/website/[id]/summary.js +++ b/pages/api/website/[id]/summary.js @@ -1,13 +1,17 @@ -import { getPageviews } from 'lib/db'; +import { getSummary } from 'lib/db'; import { useAuth } from 'lib/middleware'; +import { format } from 'date-fns'; export default async (req, res) => { await useAuth(req, res); - console.log(req.query); const { id, start_at, end_at } = req.query; - const pageviews = await getPageviews(+id, new Date(+start_at), new Date(+end_at)); + const summary = await getSummary( + +id, + format(new Date(+start_at), 'yyyy-MM-dd hh:mm:ss'), + format(new Date(+end_at), 'yyyy-MM-dd hh:mm:ss'), + ); - return res.status(200).json({ pageviews }); + return res.status(200).json(summary[0]); }; diff --git a/pages/index.js b/pages/index.js index 5804baa9..f99854d0 100644 --- a/pages/index.js +++ b/pages/index.js @@ -29,7 +29,7 @@ export default function HomePage({ username }) { } export async function getServerSideProps({ req, res }) { - const token = parse(req.headers.cookie)['umami.auth']; + const token = parse(req.headers.cookie || '')['umami.auth']; try { const payload = await verifySecureToken(token);