From 08681e45db375eaaff2125b82113d471469bccac Mon Sep 17 00:00:00 2001 From: Jon <> Date: Sun, 25 Aug 2024 20:08:23 +0100 Subject: [PATCH] Added server stats endpoint --- GuildWarsPartySearch.NodeJSServer/server.mjs | 27 ++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/GuildWarsPartySearch.NodeJSServer/server.mjs b/GuildWarsPartySearch.NodeJSServer/server.mjs index c0f5471..109dd4c 100644 --- a/GuildWarsPartySearch.NodeJSServer/server.mjs +++ b/GuildWarsPartySearch.NodeJSServer/server.mjs @@ -47,6 +47,8 @@ console.error = (...args) => { } +const started_at = new Date(); + const __dirname = dirname(fileURLToPath(import.meta.url)); let parties_by_client = {}; @@ -533,6 +535,28 @@ function send_all_parties(request_or_websocket = null) { send_to_websockets(send_to, json); } +/** + * Send publicly available server stats + * @param request_or_websocket + */ +function send_stats(request_or_websocket = null) { + const memoryData = process.memoryUsage(); + const formatMemoryUsage = (data) => `${Math.round(data / 1024 / 1024 * 100) / 100} MB`; + + const memoryUsage = { + rss: `${formatMemoryUsage(memoryData.rss)} -> Resident Set Size - total memory allocated for the process execution`, + heapTotal: `${formatMemoryUsage(memoryData.heapTotal)} -> total size of the allocated heap`, + heapUsed: `${formatMemoryUsage(memoryData.heapUsed)} -> actual memory used during the execution`, + external: `${formatMemoryUsage(memoryData.external)} -> V8 external memory`, + }; + const data = { + started_at:started_at, + memoryUsage:memoryUsage, + connected_clients:Object.values(wss.clients).length + }; + send_json(request_or_websocket,data); +} + /** * Handler for incoming message from http request or websocket * @param request {Request|WebSocket} @@ -547,6 +571,9 @@ async function on_request_message(request, data) { return; } switch (data.type || '') { + case "stats": + send_stats(request); + break; case "map_id": case "map_parties": assert(is_numeric(data.map_id), "Invalid or missing map_id");