From d6f5bb78332fd00f40510062d9422bb7af44385d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carl-Gerhard=20Lindesva=CC=88rd?= Date: Thu, 27 Feb 2025 00:07:25 +0100 Subject: [PATCH] fix(public): remove db dependencies and use traditional api call to get stats --- apps/api/src/controllers/misc.controller.ts | 22 +++++++++++++-- apps/api/src/routes/misc.router.ts | 6 ++++ apps/public/app/page.tsx | 8 ++---- apps/public/components/github-button.tsx | 8 ++++-- apps/public/components/sections/stats.tsx | 31 ++++++++------------- apps/public/lib/github.ts | 12 +++++--- apps/public/next.config.mjs | 13 +-------- apps/public/package.json | 2 -- pnpm-lock.yaml | 6 ---- 9 files changed, 55 insertions(+), 53 deletions(-) diff --git a/apps/api/src/controllers/misc.controller.ts b/apps/api/src/controllers/misc.controller.ts index a79c8d7a..e623f34f 100644 --- a/apps/api/src/controllers/misc.controller.ts +++ b/apps/api/src/controllers/misc.controller.ts @@ -5,8 +5,8 @@ import icoToPng from 'ico-to-png'; import sharp from 'sharp'; import { createHash } from '@openpanel/common/server'; -import { TABLE_NAMES, ch, formatClickhouseDate } from '@openpanel/db'; -import { getRedisCache } from '@openpanel/redis'; +import { TABLE_NAMES, ch, chQuery, formatClickhouseDate } from '@openpanel/db'; +import { cacheable, getCache, getRedisCache } from '@openpanel/redis'; interface GetFaviconParams { url: string; @@ -152,3 +152,21 @@ export async function ping( }); } } + +export async function stats(request: FastifyRequest, reply: FastifyReply) { + const res = await getCache('api:stats', 60 * 60, async () => { + const projects = await chQuery<{ project_id: string; count: number }>( + `SELECT project_id, count(*) as count from ${TABLE_NAMES.events} GROUP by project_id order by count()`, + ); + const last24h = await chQuery<{ count: number }>( + `SELECT count(*) as count from ${TABLE_NAMES.events} WHERE created_at > now() - interval '24 hours'`, + ); + return { projects, last24hCount: last24h[0]?.count || 0 }; + }); + + reply.status(200).send({ + projectsCount: res.projects.length, + eventsCount: res.projects.reduce((acc, { count }) => acc + count, 0), + eventsLast24hCount: res.last24hCount, + }); +} diff --git a/apps/api/src/routes/misc.router.ts b/apps/api/src/routes/misc.router.ts index e03c9cb1..882f0d5b 100644 --- a/apps/api/src/routes/misc.router.ts +++ b/apps/api/src/routes/misc.router.ts @@ -8,6 +8,12 @@ const miscRouter: FastifyPluginCallback = (fastify, opts, done) => { handler: controller.ping, }); + fastify.route({ + method: 'GET', + url: '/stats', + handler: controller.stats, + }); + fastify.route({ method: 'GET', url: '/favicon', diff --git a/apps/public/app/page.tsx b/apps/public/app/page.tsx index 34728845..5fad38aa 100644 --- a/apps/public/app/page.tsx +++ b/apps/public/app/page.tsx @@ -14,7 +14,7 @@ export const metadata: Metadata = { title: 'OpenPanel | An open-source alternative to Mixpanel', }; -export const experimental_ppr = true; +// export const experimental_ppr = true; export default function HomePage() { return ( @@ -26,11 +26,7 @@ export default function HomePage() { + } > diff --git a/apps/public/components/github-button.tsx b/apps/public/components/github-button.tsx index 82fff4c8..923fefad 100644 --- a/apps/public/components/github-button.tsx +++ b/apps/public/components/github-button.tsx @@ -14,9 +14,13 @@ function formatStars(stars: number) { } export function GithubButton() { - const [stars, setStars] = useState(3_263); + const [stars, setStars] = useState(3_700); useEffect(() => { - getGithubRepoInfo().then((res) => setStars(res.stargazers_count)); + getGithubRepoInfo().then((res) => { + if (res?.stargazers_count) { + setStars(res.stargazers_count); + } + }); }, []); return (