The SQL query to determine if the monitor is under maintenance is now in its own method.

This commit is contained in:
Karel Krýda 2022-01-25 19:07:27 +01:00
parent e7b2832967
commit b49e5d5c39
2 changed files with 9 additions and 10 deletions

View file

@ -29,12 +29,10 @@ class Monitor extends BeanModel {
* Only show necessary data to public * Only show necessary data to public
*/ */
async toPublicJSON() { async toPublicJSON() {
const maintenance = await R.getAll("SELECT mm.*, maintenance.start_date, maintenance.end_date FROM monitor_maintenance mm JOIN maintenance ON mm.maintenance_id = maintenance.id WHERE mm.monitor_id = ? AND datetime(maintenance.start_date) <= datetime('now') AND datetime(maintenance.end_date) >= datetime('now')", [this.id]);
return { return {
id: this.id, id: this.id,
name: this.name, name: this.name,
maintenance: (maintenance.length !== 0), maintenance: await Monitor.isUnderMaintenance(this.id),
}; };
} }
@ -54,7 +52,6 @@ class Monitor extends BeanModel {
} }
const tags = await R.getAll("SELECT mt.*, tag.name, tag.color FROM monitor_tag mt JOIN tag ON mt.tag_id = tag.id WHERE mt.monitor_id = ?", [this.id]); const tags = await R.getAll("SELECT mt.*, tag.name, tag.color FROM monitor_tag mt JOIN tag ON mt.tag_id = tag.id WHERE mt.monitor_id = ?", [this.id]);
const maintenance = await R.getAll("SELECT mm.*, maintenance.start_date, maintenance.end_date FROM monitor_maintenance mm JOIN maintenance ON mm.maintenance_id = maintenance.id WHERE mm.monitor_id = ? AND datetime(maintenance.start_date) <= datetime('now') AND datetime(maintenance.end_date) >= datetime('now')", [this.id]);
return { return {
id: this.id, id: this.id,
@ -84,7 +81,7 @@ class Monitor extends BeanModel {
pushToken: this.pushToken, pushToken: this.pushToken,
notificationIDList, notificationIDList,
tags: tags, tags: tags,
maintenance: (maintenance.length !== 0), maintenance: await Monitor.isUnderMaintenance(this.id),
}; };
} }
@ -142,8 +139,6 @@ class Monitor extends BeanModel {
bean.time = R.isoDateTime(dayjs.utc()); bean.time = R.isoDateTime(dayjs.utc());
bean.status = DOWN; bean.status = DOWN;
const maintenance = await R.getAll("SELECT mm.*, maintenance.start_date, maintenance.end_date FROM monitor_maintenance mm JOIN maintenance ON mm.maintenance_id = maintenance.id WHERE mm.monitor_id = ? AND datetime(maintenance.start_date) <= datetime('now') AND datetime(maintenance.end_date) >= datetime('now')", [this.id]);
if (this.isUpsideDown()) { if (this.isUpsideDown()) {
bean.status = flipStatus(bean.status); bean.status = flipStatus(bean.status);
} }
@ -156,7 +151,7 @@ class Monitor extends BeanModel {
} }
try { try {
if (maintenance.length !== 0) { if (await Monitor.isUnderMaintenance(this.id)) {
bean.msg = "Monitor under maintenance"; bean.msg = "Monitor under maintenance";
bean.status = MAINTENANCE; bean.status = MAINTENANCE;
} }
@ -813,6 +808,11 @@ class Monitor extends BeanModel {
monitorID monitorID
]); ]);
} }
static async isUnderMaintenance(monitorID) {
const maintenance = await R.getRow("SELECT COUNT(*) AS count FROM monitor_maintenance mm JOIN maintenance ON mm.maintenance_id = maintenance.id WHERE mm.monitor_id = ? AND datetime(maintenance.start_date) <= datetime('now') AND datetime(maintenance.end_date) >= datetime('now') LIMIT 1", [monitorID]);
return maintenance.count !== 0;
}
} }
module.exports = Monitor; module.exports = Monitor;

View file

@ -51,8 +51,7 @@ router.get("/api/push/:pushToken", async (request, response) => {
duration = dayjs(bean.time).diff(dayjs(previousHeartbeat.time), "second"); duration = dayjs(bean.time).diff(dayjs(previousHeartbeat.time), "second");
} }
const maintenance = await R.getAll("SELECT mm.*, maintenance.start_date, maintenance.end_date FROM monitor_maintenance mm JOIN maintenance ON mm.maintenance_id = maintenance.id WHERE mm.monitor_id = ? AND datetime(maintenance.start_date) <= datetime('now') AND datetime(maintenance.end_date) >= datetime('now')", [monitor.id]); if (await Monitor.isUnderMaintenance(monitor.id)) {
if (maintenance.length !== 0) {
msg = "Monitor under maintenance"; msg = "Monitor under maintenance";
status = MAINTENANCE; status = MAINTENANCE;
} }