@@ -5,19 +5,6 @@ import { requireUser } from "@/lib/session";
55
66export const runtime = "nodejs" ;
77
8- /**
9- * GET /api/servers/[id]/metrics/live
10- *
11- * Returns current live server metrics (CPU, Memory, Network, etc.)
12- * This endpoint would typically pull from an agent monitoring system
13- * or container monitoring service.
14- *
15- * In a production system, this would:
16- * - Query Prometheus/Grafana
17- * - Call the server agent API
18- * - Poll container stats (docker stats)
19- * - Get game-specific metrics from the server
20- */
218export const GET = async (
229 _request : Request ,
2310 context : { params : Promise < { id : string } > }
@@ -26,12 +13,29 @@ export const GET = async (
2613 const { id } = await context . params ;
2714
2815 const server = await prisma . server . findFirst ( {
29- select : {
30- id : true ,
31- lastHeartbeatAt : true ,
32- observedState : true ,
16+ include : {
17+ agent : {
18+ select : {
19+ cpuPercent : true ,
20+ diskTotalBytes : true ,
21+ diskUsedBytes : true ,
22+ lastHeartbeatAt : true ,
23+ maxPlayers : true ,
24+ memTotalBytes : true ,
25+ memUsedBytes : true ,
26+ networkInBps : true ,
27+ networkOutBps : true ,
28+ playerCount : true ,
29+ tps : true ,
30+ uptimeSeconds : true ,
31+ } ,
32+ } ,
33+ } ,
34+ where : {
35+ OR : [ { userId : user . id } , { members : { some : { userId : user . id } } } ] ,
36+ deletedAt : null ,
37+ id,
3338 } ,
34- where : { deletedAt : null , id, userId : user . id } ,
3539 } ) ;
3640
3741 if ( ! server ) {
@@ -45,32 +49,35 @@ export const GET = async (
4549 ) ;
4650 }
4751
48- // Integrate with monitoring system (Prometheus, agent API, container stats, etc.)
49- // For now, return mock data to demonstrate the API structure
52+ const { agent } = server ;
53+ if ( ! agent || ! agent . lastHeartbeatAt ) {
54+ return NextResponse . json ( { metrics : null } ) ;
55+ }
5056
51- const uptime = ( server . lastHeartbeatAt as Date | null )
52- ? Math . floor (
53- ( Date . now ( ) - ( server . lastHeartbeatAt as Date ) . getTime ( ) ) / 1000
54- )
55- : 0 ;
57+ // Consider metrics stale if no heartbeat in the last 60 seconds
58+ const staleSec = ( Date . now ( ) - agent . lastHeartbeatAt . getTime ( ) ) / 1000 ;
59+ if ( staleSec > 60 ) {
60+ return NextResponse . json ( { metrics : null } ) ;
61+ }
62+
63+ const memTotal = agent . memTotalBytes ?? 0 ;
64+ const memUsed = agent . memUsedBytes ?? 0 ;
65+ const memPercent = memTotal > 0 ? ( memUsed / memTotal ) * 100 : 0 ;
5666
5767 return NextResponse . json ( {
5868 metrics : {
59- // System metrics (typically from container/host monitoring)
60- // cpu: value from container/host monitoring
61- // memory: value from container/host monitoring
62- // networkIn: bytes per second
63- // networkOut: bytes per second
64- cpu : Math . random ( ) * 50 + 10 ,
65- memory : Math . random ( ) * 40 + 20 ,
66- networkIn : Math . random ( ) * 1_000_000 ,
67- networkOut : Math . random ( ) * 500_000 ,
68- uptime : Math . max ( uptime , 0 ) ,
69-
70- // Game-specific metrics (if available)
71- // playerCount: 0,
72- // maxPlayers: 32,
73- // tps: 20,
69+ cpu : agent . cpuPercent ?? 0 ,
70+ diskPercent :
71+ agent . diskTotalBytes && agent . diskTotalBytes > 0
72+ ? ( ( agent . diskUsedBytes ?? 0 ) / agent . diskTotalBytes ) * 100
73+ : undefined ,
74+ maxPlayers : agent . maxPlayers ?? undefined ,
75+ memory : memPercent ,
76+ networkIn : agent . networkInBps ?? 0 ,
77+ networkOut : agent . networkOutBps ?? 0 ,
78+ playerCount : agent . playerCount ?? undefined ,
79+ tps : agent . tps ?? undefined ,
80+ uptime : agent . uptimeSeconds ?? 0 ,
7481 } ,
7582 } ) ;
7683} ;
0 commit comments