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");
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();

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;