uptime-kuma/server/model/maintenance.js

176 lines
5.2 KiB
JavaScript
Raw Normal View History

const { BeanModel } = require("redbean-node/dist/bean-model");
2022-10-10 13:48:11 +01:00
const { parseTimeObject, parseTimeFromTimeObject, utcToLocal, localToUTC } = require("../../src/util");
const { isArray } = require("chart.js/helpers");
const { timeObjectToUTC, timeObjectToLocal } = require("../util-server");
2022-10-10 18:45:30 +01:00
const { R } = require("redbean-node");
const dayjs = require("dayjs");
class Maintenance extends BeanModel {
/**
2022-04-30 14:50:05 +01:00
* Return an object that ready to parse to JSON for public
* Only show necessary data to public
2022-04-30 14:50:05 +01:00
* @returns {Object}
*/
2022-10-10 13:48:11 +01:00
async toPublicJSON() {
let dateRange = [];
if (this.start_date) {
dateRange.push(utcToLocal(this.start_date));
if (this.end_date) {
dateRange.push(utcToLocal(this.end_date));
}
}
let timeRange = [];
2022-10-10 13:48:11 +01:00
let startTime = timeObjectToLocal(parseTimeObject(this.start_time));
timeRange.push(startTime);
2022-10-10 13:48:11 +01:00
let endTime = timeObjectToLocal(parseTimeObject(this.end_time));
timeRange.push(endTime);
let obj = {
id: this.id,
title: this.title,
description: this.description,
2022-09-23 19:33:29 +01:00
strategy: this.strategy,
intervalDay: this.interval_day,
2022-09-23 19:33:29 +01:00
active: !!this.active,
dateRange: dateRange,
timeRange: timeRange,
weekdays: (this.weekdays) ? JSON.parse(this.weekdays) : [],
daysOfMonth: (this.days_of_month) ? JSON.parse(this.days_of_month) : [],
2022-10-11 11:23:17 +01:00
timeslotList: [],
};
2022-10-11 11:23:17 +01:00
const timeslotList = await this.getTimeslotList();
for (let timeslot of timeslotList) {
obj.timeslotList.push(await timeslot.toPublicJSON());
}
if (!isArray(obj.weekdays)) {
obj.weekdays = [];
}
if (!isArray(obj.daysOfMonth)) {
obj.daysOfMonth = [];
}
2022-10-10 18:45:30 +01:00
// Maintenance Status
if (!obj.active) {
obj.status = "inactive";
2022-10-11 11:23:17 +01:00
} else if (obj.strategy === "manual") {
obj.status = "under-maintenance";
} else if (obj.timeslotList.length > 0) {
2022-10-10 18:45:30 +01:00
for (let timeslot of obj.timeslotList) {
if (dayjs.utc(timeslot.start_date) <= dayjs.utc() && dayjs.utc(timeslot.end_date) >= dayjs.utc()) {
obj.status = "under-maintenance";
break;
}
}
if (!obj.status) {
obj.status = "scheduled";
}
} else if (obj.timeslotList.length === 0) {
obj.status = "ended";
} else {
obj.status = "unknown";
}
return obj;
}
2022-10-10 18:45:30 +01:00
/**
* Only get future or current timeslots only
* @returns {Promise<[]>}
*/
async getTimeslotList() {
2022-10-11 11:23:17 +01:00
return R.convertToBeans("maintenance_timeslot", await R.getAll(`
2022-10-10 18:45:30 +01:00
SELECT maintenance_timeslot.*
FROM maintenance_timeslot, maintenance
WHERE maintenance_timeslot.maintenance_id = maintenance.id
AND maintenance.id = ?
AND ${Maintenance.getActiveAndFutureMaintenanceSQLCondition()}
`, [
this.id
2022-10-11 11:23:17 +01:00
]));
2022-10-10 18:45:30 +01:00
}
/**
2022-04-30 14:50:05 +01:00
* Return an object that ready to parse to JSON
* @param {string} timezone If not specified, the timeRange will be in UTC
2022-04-30 14:50:05 +01:00
* @returns {Object}
*/
async toJSON(timezone = null) {
return this.toPublicJSON(timezone);
}
2022-10-10 13:48:11 +01:00
static jsonToBean(bean, obj) {
if (obj.id) {
bean.id = obj.id;
}
// Apply timezone offset to timeRange, as it cannot apply automatically.
2022-10-10 13:48:11 +01:00
if (obj.timeRange[0]) {
timeObjectToUTC(obj.timeRange[0]);
if (obj.timeRange[1]) {
timeObjectToUTC(obj.timeRange[1]);
}
}
bean.title = obj.title;
bean.description = obj.description;
bean.strategy = obj.strategy;
bean.interval_day = obj.intervalDay;
bean.active = obj.active;
if (obj.dateRange[0]) {
bean.start_date = localToUTC(obj.dateRange[0]);
if (obj.dateRange[1]) {
bean.end_date = localToUTC(obj.dateRange[1]);
}
}
bean.start_time = parseTimeFromTimeObject(obj.timeRange[0]);
bean.end_time = parseTimeFromTimeObject(obj.timeRange[1]);
bean.weekdays = JSON.stringify(obj.weekdays);
bean.days_of_month = JSON.stringify(obj.daysOfMonth);
return bean;
}
/**
* SQL conditions for active maintenance
* @returns {string}
*/
static getActiveMaintenanceSQLCondition() {
return `
(maintenance_timeslot.start_date <= DATETIME('now')
AND maintenance_timeslot.end_date >= DATETIME('now')
AND maintenance.active = 1)
2022-10-10 13:48:11 +01:00
OR
(maintenance.strategy = 'manual' AND active = 1)
`;
}
2022-10-10 18:45:30 +01:00
/**
* SQL conditions for active and future maintenance
* @returns {string}
*/
static getActiveAndFutureMaintenanceSQLCondition() {
return `
2022-10-11 11:23:17 +01:00
((maintenance_timeslot.end_date >= DATETIME('now')
2022-10-10 18:45:30 +01:00
AND maintenance.active = 1)
OR
2022-10-11 11:23:17 +01:00
(maintenance.strategy = 'manual' AND active = 1))
2022-10-10 18:45:30 +01:00
`;
}
}
module.exports = Maintenance;