From 14837275b99dcc7ad471cb034c2e589330817148 Mon Sep 17 00:00:00 2001 From: ChrisChrome Date: Mon, 17 Mar 2025 23:11:07 -0600 Subject: [PATCH] Add json api --- index.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/index.js b/index.js index 6c5d25f..f272c46 100644 --- a/index.js +++ b/index.js @@ -145,6 +145,18 @@ function renderStatusPage() { fs.writeFileSync(process.env.DEV ? "./dev/index.html" : "/var/www/html/status/index.html", str); } }); + + const jsonOutput = { + endpoints: parseEndpoints(getEndpoints()), + userInfo: Object.fromEntries(Object.entries(getUsers()).map(([key, value]) => [key, value[1]])) + }; + + jsonOutput.endpoints.forEach(endpoint => { + const avgPing = endpoint.contacts.reduce((sum, contact) => sum + contact.ping, 0) / endpoint.contacts.length; + endpoint.contacts = [{ ping: isNaN(avgPing) ? 'No Data' : avgPing.toFixed(2) }]; + }); + + fs.writeFileSync(process.env.DEV ? "./dev/status.json" : "/var/www/html/status/status.json", JSON.stringify(jsonOutput, null, 2)); } client.on("ready", () => {