diff --git a/server/server.js b/server/server.js index b8eb79ee5..29e0857a0 100644 --- a/server/server.js +++ b/server/server.js @@ -27,7 +27,6 @@ debug("Importing Monitor"); const Monitor = require("./model/monitor"); debug("Importing Settings"); const { getSettings, setSettings, setting, initJWTSecret } = require("./util-server"); -const UserMonitorList = require("./user-monitor-list"); debug("Importing Notification"); const { Notification } = require("./notification"); @@ -106,9 +105,10 @@ let totalClient = 0; let jwtSecret = null; /** - * Main monitor list, filled by startMonitors() + * Main monitor list + * @type {{}} */ -let userMonitorList = new UserMonitorList(); +let monitorList = {}; /** * Show Setup Page @@ -427,13 +427,11 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); try { checkLogin(socket) - console.log(`Delete Monitor: ${monitorID} User ID: ${socket.userID}`); + console.log(`Delete Monitor: ${monitorID} User ID: ${socket.userID}`) - let monitor = userMonitorList.getMonitor(socket.userID, monitorID); - - if (monitor) { - monitor.stop(); - userMonitorList.delete(socket.userID, monitorID); + if (monitorID in monitorList) { + monitorList[monitorID].stop(); + delete monitorList[monitorID] } await R.exec("DELETE FROM monitor WHERE id = ? AND user_id = ? ", [ @@ -682,10 +680,7 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); }); - console.log("Starting All Monitors"); - await startMonitors(); - - console.log("Init the server"); + console.log("Init the server") server.once("error", async (err) => { console.error("Cannot listen: " + err.message); @@ -698,7 +693,7 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); } else { console.log(`Listening on ${port}`); } - + startMonitors(); checkVersion.startInterval(); }); @@ -761,10 +756,11 @@ async function afterLogin(socket, user) { async function getMonitorJSONList(userID) { let result = {}; - let monitorList = userMonitorList.getMonitorList(userID); + let monitorList = await R.find("monitor", " user_id = ? ORDER BY weight DESC, name", [ + userID, + ]) - for (let monitorID in monitorList) { - let monitor = monitorList[monitorID]; + for (let monitor of monitorList) { result[monitor.id] = await monitor.toJSON(); } @@ -825,13 +821,11 @@ async function startMonitor(userID, monitorID) { monitorID, ]) - let oldMonitor = userMonitorList.getMonitor(userID, monitorID); - - if (oldMonitor) { - oldMonitor.stop(); + if (monitor.id in monitorList) { + monitorList[monitor.id].stop(); } - userMonitorList.add(userID, monitor); + monitorList[monitor.id] = monitor; monitor.start(io) } @@ -849,10 +843,8 @@ async function pauseMonitor(userID, monitorID) { userID, ]); - let monitor = userMonitorList.getMonitor(userID, monitorID); - - if (monitor) { - monitor.stop(); + if (monitorID in monitorList) { + monitorList[monitorID].stop(); } } @@ -860,23 +852,16 @@ async function pauseMonitor(userID, monitorID) { * Resume active monitors */ async function startMonitors() { - let list = await R.find("monitor", " active = 1 "); + let list = await R.find("monitor", " active = 1 ") for (let monitor of list) { - userMonitorList.add(monitor.user_id, monitor); + monitorList[monitor.id] = monitor; } - delayStartMonitors(list); -} - -/** - * Only used by startMonitors() - */ -async function delayStartMonitors(list) { for (let monitor of list) { monitor.start(io); // Give some delays, so all monitors won't make request at the same moment when just start the server. - await sleep(getRandomInt(500, 1200)); + await sleep(getRandomInt(300, 1000)); } } @@ -885,12 +870,9 @@ async function shutdownFunction(signal) { console.log("Called signal: " + signal); console.log("Stopping all monitors") - - let allMonitorList = userMonitorList.getAllMonitorList(); - - for (let id in allMonitorList) { - let monitor = allMonitorList[id]; - monitor.stop(); + for (let id in monitorList) { + let monitor = monitorList[id] + monitor.stop() } await sleep(2000); await Database.close(); diff --git a/server/user-monitor-list.js b/server/user-monitor-list.js deleted file mode 100644 index 49c5bbe4b..000000000 --- a/server/user-monitor-list.js +++ /dev/null @@ -1,49 +0,0 @@ -class UserMonitorList { - - list = {}; - - add(userID, monitor) { - if (! this.list[userID]) { - this.list[userID] = {}; - } - this.list[userID][monitor.id] = monitor; - } - - delete(userID, monitorID) { - let monitorList = this.getMonitorList(userID); - delete monitorList[monitorID]; - } - - getMonitor(userID, monitorID) { - let monitorList = this.getMonitorList(userID); - - if (monitorList[monitorID]) { - return monitorList[monitorID]; - } else { - return {}; - } - } - - getMonitorList(userID) { - if (this.list[userID]) { - return this.list[userID]; - } else { - return {}; - } - } - - getAllMonitorList() { - let list = {}; - for (let userID in this.list) { - let monitorList = this.list[userID]; - - for (let monitorID in monitorList) { - list[monitorID] = monitorList[monitorID]; - } - } - return list; - } - -} - -module.exports = UserMonitorList;