mirror of
https://github.com/louislam/uptime-kuma.git
synced 2025-08-09 02:22:57 +08:00
many update
This commit is contained in:
31
server/model/heartbeat.js
Normal file
31
server/model/heartbeat.js
Normal 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;
|
@@ -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();
|
||||
|
Reference in New Issue
Block a user