From 44101e72e056633295e60a339044ccaddeebf7ec Mon Sep 17 00:00:00 2001 From: AntonSelezev Date: Tue, 7 Apr 2026 17:58:43 +0300 Subject: [PATCH 1/2] it can check multiple nodes --- index.js | 4 +- sama-stats/index.js | 64 ++++++++++++++++++++---- sama-stats/views/sama-server-stats.ejs | 69 +++++++++++++++++++++----- 3 files changed, 111 insertions(+), 26 deletions(-) diff --git a/index.js b/index.js index 440a1f4..5ad5cb7 100644 --- a/index.js +++ b/index.js @@ -31,8 +31,8 @@ const run = async () => { serverAdapter.setBasePath('/ui'); app.register(serverAdapter.registerPlugin(), { prefix: '/ui' }); - if (process.env.SAMA_URL) { - require('./sama-stats')(app); + if (process.env.SAMA_ADMIN_API_KEY) { + require('./sama-stats')(app, redisOptions); } await app.listen({ port: process.env.PORT, host: process.env.HOST }); diff --git a/sama-stats/index.js b/sama-stats/index.js index fb5cbd2..7bf5ebb 100644 --- a/sama-stats/index.js +++ b/sama-stats/index.js @@ -1,15 +1,19 @@ const path = require('node:path') +const Redis = require('ioredis') const pointOfView = require('@fastify/view') +let redisConnection = void 0 +let lastFetchDate = void 0 let lastServerStats = {} -const fetchSamaServerStats = async () => { +const fetchSamaServerStats = async (endpoint) => { + endpoint = endpoint.replace(/\/$/, '') + const serverStats = await fetch( - `${process.env.SAMA_URL}/admin/server-stats?format=1`, + `${endpoint}/admin/server-stats?format=1`, { method: 'GET', headers: {'Admin-Api-Key': process.env.SAMA_ADMIN_API_KEY} } ) .then(response => response.json()) - .then(serverStats => ({ serverStats: Object.assign(serverStats, { fetchDate: new Date().toString() }) })) .catch(error => { console.log('[Error][fetching]', error) const message = `SAMA Server: ${error.message}` @@ -19,13 +23,49 @@ const fetchSamaServerStats = async () => { return serverStats } -const startFetchingServerDate = () => { - setInterval(async () => { - lastServerStats = await fetchSamaServerStats() - }, process.env.SERVER_UPDATE_INTERVAL ?? 30_000) +const fetchClusterStats = async () => { + let clusterEndpoints = [] + + if (process.env.SAMA_URL) { + clusterEndpoints = process.env.SAMA_URL.split(',') + } else { + const listNodes = await redisConnection.keys('sama-node-data:*') + + clusterEndpoints = listNodes + .map(key => key.replace('sama-node-data:', '')) + .map(wsEndpoint => { + const url = new URL(wsEndpoint) + url.protocol = 'http' + url.port = 9001 + return url.toString() + }) + } + + console.log('[Endpoints]', clusterEndpoints) + + const clusterStats = {} + + for (const clusterEndpoint of clusterEndpoints) { + const stats = await fetchSamaServerStats(clusterEndpoint) + + console.log('[Stats]', clusterEndpoint, stats) + + clusterStats[clusterEndpoint] = stats + } + + return clusterStats +} + +const updateClusterStats = async () => { + lastServerStats = await fetchClusterStats() + lastFetchDate = new Date().toString() } -module.exports = (fastifyApp) => { +const startFetchingServerStats = () => setInterval(updateClusterStats, process.env.SERVER_UPDATE_INTERVAL ?? 30_000) + +module.exports = (fastifyApp, redisOptions) => { + redisConnection = new Redis(redisOptions) + fastifyApp.register(pointOfView, { engine: { ejs: require('ejs'), @@ -37,7 +77,9 @@ module.exports = (fastifyApp) => { method: 'GET', url: '/stats/sama-server', handler: (req, reply) => { - reply.view('sama-server-stats.ejs', { updateTime: process.env.CLIENT_UPDATE_INTERVAL ?? 10_000 }); + reply.view('sama-server-stats.ejs', { + updateTime: process.env.CLIENT_UPDATE_INTERVAL ?? 10_000, + }); }, }); @@ -45,9 +87,9 @@ module.exports = (fastifyApp) => { method: 'GET', url: '/stats/data/sama-server', handler: async (req, reply) => { - reply.send(lastServerStats) + reply.send({ fetchDate: lastFetchDate, stats: lastServerStats }) }, }); - startFetchingServerDate() + startFetchingServerStats() } \ No newline at end of file diff --git a/sama-stats/views/sama-server-stats.ejs b/sama-stats/views/sama-server-stats.ejs index 18c5ead..250778e 100644 --- a/sama-stats/views/sama-server-stats.ejs +++ b/sama-stats/views/sama-server-stats.ejs @@ -13,54 +13,97 @@
-
+
Server Fetch date:
Client Update date:
+
Nodes List:
From 1d82a18bebceb29323d5d0940578618b08fd1911 Mon Sep 17 00:00:00 2001 From: AntonSelezev Date: Fri, 19 Jun 2026 12:32:32 +0300 Subject: [PATCH 2/2] update --- .env.example | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.env.example b/.env.example index 51ef9af..2f8349f 100644 --- a/.env.example +++ b/.env.example @@ -9,7 +9,7 @@ REDIS_TLS=false BULL_QUEUE_NAME=notification -SAMA_URL=http://localhost:9001 +SAMA_URL=http://localhost:9001 # If multiple nodes (discovering from redis) commed this env line SAMA_ADMIN_API_KEY=********** SERVER_UPDATE_INTERVAL=30000