add user-monitor-list

This commit is contained in:
LouisLam 2021-09-08 14:16:30 +08:00
parent d761d54d0e
commit 230a9bfaf9
2 changed files with 91 additions and 24 deletions

View File

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

View File

@ -0,0 +1,49 @@
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;