Revert "add user-monitor-list"

This reverts commit 230a9bfaf9.
This commit is contained in:
LouisLam 2021-09-08 19:54:38 +08:00
parent 0ddbac5109
commit a7b50c3630
2 changed files with 24 additions and 91 deletions

View File

@ -27,7 +27,6 @@ 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");
@ -106,9 +105,10 @@ let totalClient = 0;
let jwtSecret = null; let jwtSecret = null;
/** /**
* Main monitor list, filled by startMonitors() * Main monitor list
* @type {{}}
*/ */
let userMonitorList = new UserMonitorList(); let monitorList = {};
/** /**
* Show Setup Page * Show Setup Page
@ -427,13 +427,11 @@ 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}`)
let monitor = userMonitorList.getMonitor(socket.userID, monitorID); if (monitorID in monitorList) {
monitorList[monitorID].stop();
if (monitor) { delete monitorList[monitorID]
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 = ? ", [
@ -682,10 +680,7 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString();
}); });
console.log("Starting All Monitors"); console.log("Init the server")
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);
@ -698,7 +693,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();
}); });
@ -761,10 +756,11 @@ async function afterLogin(socket, user) {
async function getMonitorJSONList(userID) { async function getMonitorJSONList(userID) {
let result = {}; 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) { for (let monitor of monitorList) {
let monitor = monitorList[monitorID];
result[monitor.id] = await monitor.toJSON(); result[monitor.id] = await monitor.toJSON();
} }
@ -825,13 +821,11 @@ async function startMonitor(userID, monitorID) {
monitorID, monitorID,
]) ])
let oldMonitor = userMonitorList.getMonitor(userID, monitorID); if (monitor.id in monitorList) {
monitorList[monitor.id].stop();
if (oldMonitor) {
oldMonitor.stop();
} }
userMonitorList.add(userID, monitor); monitorList[monitor.id] = monitor;
monitor.start(io) monitor.start(io)
} }
@ -849,10 +843,8 @@ async function pauseMonitor(userID, monitorID) {
userID, userID,
]); ]);
let monitor = userMonitorList.getMonitor(userID, monitorID); if (monitorID in monitorList) {
monitorList[monitorID].stop();
if (monitor) {
monitor.stop();
} }
} }
@ -860,23 +852,16 @@ 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) {
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) { 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(500, 1200)); await sleep(getRandomInt(300, 1000));
} }
} }
@ -885,12 +870,9 @@ 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 allMonitorList = userMonitorList.getAllMonitorList(); let monitor = monitorList[id]
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

@ -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;