mirror of
https://github.com/louislam/uptime-kuma.git
synced 2025-08-08 17:13:08 +08:00
[WIP] Checking maintenance time using maintenance_timeslot table
This commit is contained in:
46
server/model/maintenance_timeslot.js
Normal file
46
server/model/maintenance_timeslot.js
Normal file
@@ -0,0 +1,46 @@
|
||||
const { BeanModel } = require("redbean-node/dist/bean-model");
|
||||
const { R } = require("redbean-node");
|
||||
const dayjs = require("dayjs");
|
||||
|
||||
class MaintenanceTimeslot extends BeanModel {
|
||||
|
||||
async toPublicJSON() {
|
||||
|
||||
}
|
||||
|
||||
async toJSON() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Maintenance} maintenance
|
||||
* @param {dayjs} startFrom (For recurring type only) Generate Timeslot from this date, if it is smaller than the current date, it will use the current date instead. As generating a passed timeslot is meaningless.
|
||||
* @param {boolean} removeExist Remove existing timeslot before create
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
static async generateTimeslot(maintenance, startFrom = null, removeExist = false) {
|
||||
if (!startFrom) {
|
||||
startFrom = dayjs();
|
||||
}
|
||||
|
||||
if (removeExist) {
|
||||
await R.exec("DELETE FROM maintenance_timeslot WHERE maintenance_id = ? ", [
|
||||
maintenance.id
|
||||
]);
|
||||
}
|
||||
|
||||
if (maintenance.strategy === "single") {
|
||||
let bean = R.dispense("maintenance_timeslot");
|
||||
bean.maintenance_id = maintenance.id;
|
||||
bean.start_date = maintenance.start_datetime;
|
||||
bean.end_date = maintenance.end_datetime;
|
||||
bean.generated_next = true;
|
||||
await R.store(bean);
|
||||
} else {
|
||||
throw new Error("Unknown maintenance strategy");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = MaintenanceTimeslot;
|
@@ -1105,7 +1105,17 @@ class Monitor extends BeanModel {
|
||||
* @returns {Promise<boolean>}
|
||||
*/
|
||||
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 ]);
|
||||
const maintenance = await R.getRow(`
|
||||
SELECT COUNT(*) AS count
|
||||
FROM monitor_maintenance mm
|
||||
JOIN maintenance
|
||||
ON mm.maintenance_id = maintenance.id
|
||||
JOIN maintenance_timeslot
|
||||
ON maintenance_timeslot.maintenance_id = maintenance.id
|
||||
WHERE mm.monitor_id = ?
|
||||
AND maintenance_timeslot.start_date <= DATETIME('now')
|
||||
AND maintenance_timeslot.end_date >= DATETIME('now')
|
||||
LIMIT 1`, [ monitorID ]);
|
||||
return maintenance.count !== 0;
|
||||
}
|
||||
}
|
||||
|
@@ -272,15 +272,15 @@ class StatusPage extends BeanModel {
|
||||
const publicMaintenanceList = [];
|
||||
|
||||
let maintenanceBeanList = R.convertToBeans("maintenance", await R.getAll(`
|
||||
SELECT m.*
|
||||
FROM maintenance m
|
||||
JOIN maintenance_status_page msp
|
||||
ON msp.maintenance_id = m.id
|
||||
WHERE datetime(m.start_date) <= datetime('now')
|
||||
AND datetime(m.end_date) >= datetime('now')
|
||||
AND msp.status_page_id = ?
|
||||
ORDER BY m.end_date
|
||||
`, [ statusPageId ]));
|
||||
SELECT m.*
|
||||
FROM maintenance m, maintenance_status_page msp, maintenance_timeslot
|
||||
WHERE msp.maintenance_id = m.id
|
||||
AND maintenance_timeslot.maintenance.id = m.id
|
||||
AND maintenance_timeslot.start_date <= DATETIME('now')
|
||||
AND maintenance_timeslot.end_date >= DATETIME('now')
|
||||
AND msp.status_page_id = ?
|
||||
ORDER BY m.end_date
|
||||
`, [ statusPageId ]));
|
||||
|
||||
for (const bean of maintenanceBeanList) {
|
||||
publicMaintenanceList.push(await bean.toPublicJSON());
|
||||
|
Reference in New Issue
Block a user