From bf29f287264223a88dda7d4f97f4cf73c9546ed4 Mon Sep 17 00:00:00 2001 From: LouisLam Date: Mon, 30 Aug 2021 14:55:33 +0800 Subject: [PATCH] send stats only if there is at least one client in the room --- server/model/monitor.js | 16 +++++++++++----- server/util-server.js | 23 +++++++++++++++++++++++ 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/server/model/monitor.js b/server/model/monitor.js index 6d0d812b..19f21d92 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -7,7 +7,7 @@ dayjs.extend(timezone) const axios = require("axios"); const { Prometheus } = require("../prometheus"); const { debug, UP, DOWN, PENDING, flipStatus, TimeLogger } = require("../../src/util"); -const { tcping, ping, dnsResolve, checkCertificate, checkStatusCode } = require("../util-server"); +const { tcping, ping, dnsResolve, checkCertificate, checkStatusCode, getTotalClientInRoom } = require("../util-server"); const { R } = require("redbean-node"); const { BeanModel } = require("redbean-node/dist/bean-model"); const { Notification } = require("../notification") @@ -353,10 +353,16 @@ class Monitor extends BeanModel { } static async sendStats(io, monitorID, userID) { - await Monitor.sendAvgPing(24, io, monitorID, userID); - await Monitor.sendUptime(24, io, monitorID, userID); - await Monitor.sendUptime(24 * 30, io, monitorID, userID); - await Monitor.sendCertInfo(io, monitorID, userID); + const hasClients = getTotalClientInRoom(io, userID) > 0; + + if (hasClients) { + await Monitor.sendAvgPing(24, io, monitorID, userID); + await Monitor.sendUptime(24, io, monitorID, userID); + await Monitor.sendUptime(24 * 30, io, monitorID, userID); + await Monitor.sendCertInfo(io, monitorID, userID); + } else { + debug("No clients in the room, no need to send stats"); + } } /** diff --git a/server/util-server.js b/server/util-server.js index a30bcfec..a2fef065 100644 --- a/server/util-server.js +++ b/server/util-server.js @@ -248,3 +248,26 @@ exports.checkStatusCode = function (status, accepted_codes) { return false; } + +exports.getTotalClientInRoom = (io, roomName) => { + + const sockets = io.sockets; + + if (! sockets) { + return 0; + } + + const adapter = sockets.adapter; + + if (! adapter) { + return 0; + } + + const room = adapter.rooms.get(roomName); + + if (room) { + return room.size; + } else { + return 0; + } +}