Add /stats

This commit is contained in:
MartinBraquet
2026-03-07 11:28:24 +01:00
parent 3ddf81d935
commit 008bc11ebf
4 changed files with 41 additions and 1 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "@compass/api",
"version": "1.23.0",
"version": "1.24.0",
"private": true,
"description": "Backend API endpoints",
"main": "src/serve.ts",

View File

@@ -89,6 +89,7 @@ import {setCompatibilityAnswer} from './set-compatibility-answer'
import {setLastOnlineTime} from './set-last-online-time'
import {shipProfiles} from './ship-profiles'
import {starProfile} from './star-profile'
import {stats} from './stats'
import {updateEvent} from './update-event'
import {updateMe} from './update-me'
import {updateNotifSettings} from './update-notif-setting'
@@ -662,6 +663,7 @@ const handlers: {[k in APIPath]: APIHandler<k>} = {
'rsvp-event': rsvpEvent,
'update-event': updateEvent,
health: health,
stats: stats,
me: getMe,
'get-user-and-profile': getUserAndProfileHandler,
report: report,

19
backend/api/src/stats.ts Normal file
View File

@@ -0,0 +1,19 @@
import {createSupabaseDirectClient} from 'shared/supabase/init'
import {APIHandler} from './helpers/endpoint'
export const stats: APIHandler<'stats'> = async (_, _auth) => {
const pg = createSupabaseDirectClient()
const [userCount, profileCount, eventsCount] = await Promise.all([
pg.one(`SELECT COUNT(*)::int as count FROM users`),
pg.one(`SELECT COUNT(*)::int as count FROM profiles`),
pg.one(`SELECT COUNT(*)::int as count FROM events WHERE event_start_time > now()`),
])
return {
users: userCount.count,
profiles: profileCount.count,
upcomingEvents: eventsCount.count,
}
}

View File

@@ -138,6 +138,25 @@ export const API = (_apiTypeCheck = {
summary: 'Check whether the API server is running',
tag: 'General',
},
/**
* Get platform statistics
* Returns public statistics about the platform including user count and other metrics
*
* @returns Object containing various platform statistics
*/
stats: {
method: 'GET',
authed: false,
rateLimited: true,
props: z.object({}),
returns: {} as {
users: number
profiles: number
upcomingEvents: number
},
summary: 'Get platform statistics',
tag: 'General',
},
/**
* Get Supabase JWT token
* Returns a JWT token for authenticated clients to access Supabase directly