( serverId: string, timeout = 2000, )
| 1248 | * @param timeout - Timeout in milliseconds (default: 2000) |
| 1249 | */ |
| 1250 | export async function getServerStats( |
| 1251 | serverId: string, |
| 1252 | timeout = 2000, |
| 1253 | ): Promise<LspServerStatsFormatted | null> { |
| 1254 | const entry = managedServers.get(serverId); |
| 1255 | if (!entry?.port) { |
| 1256 | return null; |
| 1257 | } |
| 1258 | |
| 1259 | try { |
| 1260 | const controller = new AbortController(); |
| 1261 | const timeoutId = setTimeout(() => controller.abort(), timeout); |
| 1262 | |
| 1263 | const response = await fetch(`http://127.0.0.1:${entry.port}/status`, { |
| 1264 | signal: controller.signal, |
| 1265 | }); |
| 1266 | |
| 1267 | clearTimeout(timeoutId); |
| 1268 | |
| 1269 | if (!response.ok) { |
| 1270 | return null; |
| 1271 | } |
| 1272 | |
| 1273 | const data = (await response.json()) as LspServerStats; |
| 1274 | |
| 1275 | // Aggregate stats from all processes |
| 1276 | let totalMemory = 0; |
| 1277 | let maxUptime = 0; |
| 1278 | let firstPid: number | null = null; |
| 1279 | |
| 1280 | for (const proc of data.processes || []) { |
| 1281 | totalMemory += proc.memory_bytes || 0; |
| 1282 | if (proc.uptime_secs > maxUptime) { |
| 1283 | maxUptime = proc.uptime_secs; |
| 1284 | } |
| 1285 | if (firstPid === null && proc.pid) { |
| 1286 | firstPid = proc.pid; |
| 1287 | } |
| 1288 | } |
| 1289 | |
| 1290 | return { |
| 1291 | memoryBytes: totalMemory, |
| 1292 | memoryFormatted: formatMemory(totalMemory), |
| 1293 | uptimeSeconds: maxUptime, |
| 1294 | uptimeFormatted: formatUptime(maxUptime), |
| 1295 | pid: firstPid, |
| 1296 | processCount: data.processes?.length ?? 0, |
| 1297 | }; |
| 1298 | } catch { |
| 1299 | return null; |
| 1300 | } |
| 1301 | } |
no test coverage detected