diff --git a/apps/supercode-cli/server/src/index.ts b/apps/supercode-cli/server/src/index.ts index 56d8b17..847c362 100644 --- a/apps/supercode-cli/server/src/index.ts +++ b/apps/supercode-cli/server/src/index.ts @@ -122,6 +122,16 @@ app.use( registerAnalyticsRoutes(app, prisma) +app.get("/api/data/users/count", async (_req, res) => { + try { + const count = await prisma.user.count() + res.json({ count }) + } catch (error) { + console.error("[users/count] Error:", error) + res.status(500).json({ error: "Failed to fetch user count" }) + } +}) + app.get("/device", async (req, res) => { const { user_code } = req.query res.redirect(`${clientUrl}/device?user_code=${user_code}`) diff --git a/apps/web/modules/stats/actions/index.ts b/apps/web/modules/stats/actions/index.ts index 3c3950f..71a67c1 100644 --- a/apps/web/modules/stats/actions/index.ts +++ b/apps/web/modules/stats/actions/index.ts @@ -1,26 +1,31 @@ "use server" -import prisma from "@super/db" +async function safeFetchJson(url: string, fallback: T): Promise { + try { + const res = await fetch(url, { next: { revalidate: 3600 } }) + return await res.json() + } catch { + return fallback + } +} export async function getStats() { - try { - const [userCount, npmResponse, githubResponse] = await Promise.all([ - prisma.user.count(), - fetch("https://api.npmjs.org/downloads/point/last-month/supercode-cli").then( - (r) => r.json().catch(() => ({ downloads: 0 })), - ), - fetch("https://api.github.com/repos/yashdev9274/superCli").then((r) => - r.json().catch(() => ({ stargazers_count: 0 })), - ), - ]) + const serverUrl = process.env.TERMINAL_SERVER_URL || "http://localhost:3004" + const [userStats, npmResponse, githubResponse] = await Promise.all([ + safeFetchJson<{ count?: number }>(`${serverUrl}/api/data/users/count`, { count: 0 }), + safeFetchJson<{ downloads?: number }>( + "https://api.npmjs.org/downloads/point/last-month/supercode-cli", + { downloads: 0 }, + ), + safeFetchJson<{ stargazers_count?: number }>( + "https://api.github.com/repos/yashdev9274/superCli", + { stargazers_count: 0 }, + ), + ]) - return { - users: userCount + 100, - downloads: (npmResponse.downloads ?? 0) + 6000, - stars: githubResponse.stargazers_count ?? 0, - } - } catch (error) { - console.error("Error fetching stats:", error) - return { users: 0, downloads: 0, stars: 0 } + return { + users: (userStats.count ?? 0) + 100, + downloads: (npmResponse.downloads ?? 0) + 6000, + stars: githubResponse.stargazers_count ?? 0, } }