2022-09-27 05:44:44 -07:00
|
|
|
const { BeanModel } = require("redbean-node/dist/bean-model");
|
|
|
|
const { R } = require("redbean-node");
|
|
|
|
const dayjs = require("dayjs");
|
2022-10-11 03:23:17 -07:00
|
|
|
const { log, utcToLocal, SQL_DATETIME_FORMAT_WITHOUT_SECOND } = require("../../src/util");
|
|
|
|
const { UptimeKumaServer } = require("../uptime-kuma-server");
|
2022-09-27 05:44:44 -07:00
|
|
|
|
|
|
|
class MaintenanceTimeslot extends BeanModel {
|
|
|
|
|
|
|
|
async toPublicJSON() {
|
2022-10-11 06:48:43 -07:00
|
|
|
const serverTimezoneOffset = UptimeKumaServer.getInstance().getTimezoneOffset();
|
2022-09-27 05:44:44 -07:00
|
|
|
|
2022-10-11 03:23:17 -07:00
|
|
|
const obj = {
|
|
|
|
id: this.id,
|
|
|
|
startDate: this.start_date,
|
|
|
|
endDate: this.end_date,
|
|
|
|
startDateServerTimezone: utcToLocal(this.start_date, SQL_DATETIME_FORMAT_WITHOUT_SECOND),
|
|
|
|
endDateServerTimezone: utcToLocal(this.end_date, SQL_DATETIME_FORMAT_WITHOUT_SECOND),
|
|
|
|
serverTimezoneOffset,
|
|
|
|
};
|
|
|
|
|
|
|
|
return obj;
|
2022-09-27 05:44:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async toJSON() {
|
2022-10-11 03:23:17 -07:00
|
|
|
return await this.toPublicJSON();
|
2022-09-27 05:44:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2022-09-27 09:20:17 -07:00
|
|
|
if (maintenance.strategy === "manual") {
|
|
|
|
log.debug("maintenance", "No need to generate timeslot for manual type");
|
|
|
|
} else if (maintenance.strategy === "single") {
|
2022-09-27 05:44:44 -07:00
|
|
|
let bean = R.dispense("maintenance_timeslot");
|
|
|
|
bean.maintenance_id = maintenance.id;
|
2022-09-27 09:48:15 -07:00
|
|
|
bean.start_date = maintenance.start_date;
|
|
|
|
bean.end_date = maintenance.end_date;
|
2022-09-27 05:44:44 -07:00
|
|
|
bean.generated_next = true;
|
|
|
|
await R.store(bean);
|
2022-10-10 05:48:11 -07:00
|
|
|
} else if (maintenance.strategy === "recurring-interval") {
|
|
|
|
// TODO
|
|
|
|
} else if (maintenance.strategy === "recurring-weekday") {
|
|
|
|
// TODO
|
|
|
|
} else if (maintenance.strategy === "recurring-day-of-month") {
|
|
|
|
// TODO
|
2022-09-27 05:44:44 -07:00
|
|
|
} else {
|
|
|
|
throw new Error("Unknown maintenance strategy");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = MaintenanceTimeslot;
|