many update

This commit is contained in:
Louis
2021-06-29 16:06:20 +08:00
parent 932ebdb8d6
commit 9fa84a0a2b
10 changed files with 235 additions and 24 deletions

31
server/model/heartbeat.js Normal file
View File

@@ -0,0 +1,31 @@
const dayjs = require("dayjs");
const utc = require('dayjs/plugin/utc')
var timezone = require('dayjs/plugin/timezone')
dayjs.extend(utc)
dayjs.extend(timezone)
const axios = require("axios");
const {R} = require("redbean-node");
const {BeanModel} = require("redbean-node/dist/bean-model");
/**
* status:
* 0 = DOWN
* 1 = UP
*/
class Heartbeat extends BeanModel {
toJSON() {
return {
monitorID: this.monitor_id,
status: this.status,
time: this.time,
msg: this.msg,
ping: this.ping,
important: this.important,
};
}
}
module.exports = Heartbeat;

View File

@@ -28,9 +28,17 @@ class Monitor extends BeanModel {
}
start(io) {
let previousBeat = null;
const beat = async () => {
console.log(`Monitor ${this.id}: Heartbeat`)
if (! previousBeat) {
previousBeat = await R.findOne("heartbeat", " monitor_id = ? ORDER BY time DESC", [
this.id
])
}
let bean = R.dispense("heartbeat")
bean.monitor_id = this.id;
bean.time = R.isoDateTime(dayjs.utc());
@@ -49,15 +57,18 @@ class Monitor extends BeanModel {
bean.msg = error.message;
}
io.to(this.user_id).emit("heartbeat", {
monitorID: this.id,
status: bean.status,
time: bean.time,
msg: bean.msg,
ping: bean.ping,
});
// Mark as important if status changed
if (! previousBeat || previousBeat.status !== bean.status) {
bean.important = true;
} else {
bean.important = false;
}
io.to(this.user_id).emit("heartbeat", bean.toJSON());
await R.store(bean)
previousBeat = bean;
}
beat();

View File

@@ -9,7 +9,6 @@ const {R} = require("redbean-node");
const passwordHash = require('password-hash');
const jwt = require('jsonwebtoken');
const Monitor = require("./model/monitor");
const {sleep} = require("./util");
let stop = false;
let interval = 6000;
@@ -40,6 +39,10 @@ let monitorList = {};
// Public API
/*
firstConnect - true = send monitor list + heartbeat list history
false = do not send
*/
socket.on("loginByToken", async (token, callback) => {
try {
@@ -320,13 +323,20 @@ async function checkOwner(userID, monitorID) {
}
async function sendMonitorList(socket) {
io.to(socket.userID).emit("monitorList", await getMonitorJSONList(socket.userID))
let list = await getMonitorJSONList(socket.userID);
io.to(socket.userID).emit("monitorList", list)
return list;
}
async function afterLogin(socket, user) {
socket.userID = user.id;
socket.join(user.id)
socket.emit("monitorList", await getMonitorJSONList(user.id))
let monitorList = await sendMonitorList(socket)
for (let monitorID in monitorList) {
await sendHeartbeatList(socket, monitorID);
}
}
async function getMonitorJSONList(userID) {
@@ -338,8 +348,11 @@ async function getMonitorJSONList(userID) {
for (let monitor of monitorList) {
result[monitor.id] = monitor.toJSON();
}
return result;
}
@@ -421,3 +434,24 @@ async function startMonitors() {
}
}
/**
* Send Heartbeat History list to socket
*/
async function sendHeartbeatList(socket, monitorID) {
let list = await R.find("heartbeat", `
monitor_id = ?
ORDER BY time DESC
LIMIT 100
`, [
monitorID
])
let result = [];
for (let bean of list) {
result.unshift(bean.toJSON())
}
socket.emit("heartbeatList", monitorID, result)
}

View File

@@ -1,3 +1,12 @@
exports.sleep = (ms) => {
/*
* Common functions - can be used in frontend or backend
*/
export function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
export function ucfirst(str) {
const firstLetter = str.substr(0, 1);
return firstLetter.toUpperCase() + str.substr(1);
}