mirror of
https://github.com/louislam/uptime-kuma.git
synced 2025-08-08 11:42:03 +08:00
WIP
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
const { BeanModel } = require("redbean-node/dist/bean-model");
|
||||
const { parseTimeObject, parseTimeFromTimeObject, isoToUTCDateTime, utcToISODateTime, SQL_DATETIME_FORMAT, utcToLocal, localToUTC } = require("../../src/util");
|
||||
const { parseTimeObject, parseTimeFromTimeObject, utcToLocal, localToUTC } = require("../../src/util");
|
||||
const { isArray } = require("chart.js/helpers");
|
||||
const { timeObjectToUTC, timeObjectToLocal } = require("../util-server");
|
||||
|
||||
@@ -11,7 +11,7 @@ class Maintenance extends BeanModel {
|
||||
* @param {string} timezone If not specified, the timeRange will be in UTC
|
||||
* @returns {Object}
|
||||
*/
|
||||
async toPublicJSON(timezone = null) {
|
||||
async toPublicJSON() {
|
||||
|
||||
let dateRange = [];
|
||||
if (this.start_date) {
|
||||
@@ -22,21 +22,11 @@ class Maintenance extends BeanModel {
|
||||
}
|
||||
|
||||
let timeRange = [];
|
||||
let startTime = parseTimeObject(this.start_time);
|
||||
let startTime = timeObjectToLocal(parseTimeObject(this.start_time));
|
||||
timeRange.push(startTime);
|
||||
let endTime = parseTimeObject(this.end_time);
|
||||
let endTime = timeObjectToLocal(parseTimeObject(this.end_time));
|
||||
timeRange.push(endTime);
|
||||
|
||||
// Apply timezone offset
|
||||
if (timezone) {
|
||||
if (this.start_time) {
|
||||
timeObjectToLocal(startTime, timezone);
|
||||
}
|
||||
if (this.end_time) {
|
||||
timeObjectToLocal(endTime, timezone);
|
||||
}
|
||||
}
|
||||
|
||||
let obj = {
|
||||
id: this.id,
|
||||
title: this.title,
|
||||
@@ -70,18 +60,16 @@ class Maintenance extends BeanModel {
|
||||
return this.toPublicJSON(timezone);
|
||||
}
|
||||
|
||||
static jsonToBean(bean, obj, timezone) {
|
||||
static jsonToBean(bean, obj) {
|
||||
if (obj.id) {
|
||||
bean.id = obj.id;
|
||||
}
|
||||
|
||||
// Apply timezone offset to timeRange, as it cannot apply automatically.
|
||||
if (timezone) {
|
||||
if (obj.timeRange[0]) {
|
||||
timeObjectToUTC(obj.timeRange[0], timezone);
|
||||
if (obj.timeRange[1]) {
|
||||
timeObjectToUTC(obj.timeRange[1], timezone);
|
||||
}
|
||||
if (obj.timeRange[0]) {
|
||||
timeObjectToUTC(obj.timeRange[0]);
|
||||
if (obj.timeRange[1]) {
|
||||
timeObjectToUTC(obj.timeRange[1]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,7 +106,7 @@ class Maintenance extends BeanModel {
|
||||
(maintenance_timeslot.start_date <= DATETIME('now')
|
||||
AND maintenance_timeslot.end_date >= DATETIME('now')
|
||||
AND maintenance.active = 1)
|
||||
AND
|
||||
OR
|
||||
(maintenance.strategy = 'manual' AND active = 1)
|
||||
|
||||
`;
|
||||
|
@@ -40,6 +40,12 @@ class MaintenanceTimeslot extends BeanModel {
|
||||
bean.end_date = maintenance.end_date;
|
||||
bean.generated_next = true;
|
||||
await R.store(bean);
|
||||
} else if (maintenance.strategy === "recurring-interval") {
|
||||
// TODO
|
||||
} else if (maintenance.strategy === "recurring-weekday") {
|
||||
// TODO
|
||||
} else if (maintenance.strategy === "recurring-day-of-month") {
|
||||
// TODO
|
||||
} else {
|
||||
throw new Error("Unknown maintenance strategy");
|
||||
}
|
||||
|
@@ -5,7 +5,6 @@ const apicache = require("../modules/apicache");
|
||||
const { UptimeKumaServer } = require("../uptime-kuma-server");
|
||||
const Maintenance = require("../model/maintenance");
|
||||
const server = UptimeKumaServer.getInstance();
|
||||
const dayjs = require("dayjs");
|
||||
const MaintenanceTimeslot = require("../model/maintenance_timeslot");
|
||||
|
||||
/**
|
||||
@@ -14,13 +13,13 @@ const MaintenanceTimeslot = require("../model/maintenance_timeslot");
|
||||
*/
|
||||
module.exports.maintenanceSocketHandler = (socket) => {
|
||||
// Add a new maintenance
|
||||
socket.on("addMaintenance", async (maintenance, timezone, callback) => {
|
||||
socket.on("addMaintenance", async (maintenance, callback) => {
|
||||
try {
|
||||
checkLogin(socket);
|
||||
|
||||
log.debug("maintenance", maintenance);
|
||||
|
||||
let bean = Maintenance.jsonToBean(R.dispense("maintenance"), maintenance, timezone);
|
||||
let bean = Maintenance.jsonToBean(R.dispense("maintenance"), maintenance);
|
||||
bean.user_id = socket.userID;
|
||||
let maintenanceID = await R.store(bean);
|
||||
await MaintenanceTimeslot.generateTimeslot(bean);
|
||||
@@ -42,7 +41,7 @@ module.exports.maintenanceSocketHandler = (socket) => {
|
||||
});
|
||||
|
||||
// Edit a maintenance
|
||||
socket.on("editMaintenance", async (maintenance, timezone, callback) => {
|
||||
socket.on("editMaintenance", async (maintenance, callback) => {
|
||||
try {
|
||||
checkLogin(socket);
|
||||
|
||||
@@ -52,7 +51,7 @@ module.exports.maintenanceSocketHandler = (socket) => {
|
||||
throw new Error("Permission denied.");
|
||||
}
|
||||
|
||||
Maintenance.jsonToBean(bean, maintenance, timezone);
|
||||
Maintenance.jsonToBean(bean, maintenance);
|
||||
|
||||
await R.store(bean);
|
||||
await MaintenanceTimeslot.generateTimeslot(bean, null, true);
|
||||
@@ -142,7 +141,7 @@ module.exports.maintenanceSocketHandler = (socket) => {
|
||||
}
|
||||
});
|
||||
|
||||
socket.on("getMaintenance", async (maintenanceID, timezone, callback) => {
|
||||
socket.on("getMaintenance", async (maintenanceID, callback) => {
|
||||
try {
|
||||
checkLogin(socket);
|
||||
|
||||
@@ -155,7 +154,7 @@ module.exports.maintenanceSocketHandler = (socket) => {
|
||||
|
||||
callback({
|
||||
ok: true,
|
||||
maintenance: await bean.toJSON(timezone),
|
||||
maintenance: await bean.toJSON(),
|
||||
});
|
||||
|
||||
} catch (e) {
|
||||
|
@@ -648,8 +648,14 @@ module.exports.send403 = (res, msg = "") => {
|
||||
};
|
||||
|
||||
function timeObjectConvertTimezone(obj, timezone, timeObjectToUTC = true) {
|
||||
// e.g. +08:00
|
||||
let offsetString = dayjs().tz(timezone).format("Z");
|
||||
let offsetString;
|
||||
|
||||
if (timezone) {
|
||||
offsetString = dayjs().tz(timezone).format("Z");
|
||||
} else {
|
||||
offsetString = dayjs().format("Z");
|
||||
}
|
||||
|
||||
let hours = parseInt(offsetString.substring(1, 3));
|
||||
let minutes = parseInt(offsetString.substring(4, 6));
|
||||
|
||||
@@ -680,10 +686,22 @@ function timeObjectConvertTimezone(obj, timezone, timeObjectToUTC = true) {
|
||||
return obj;
|
||||
}
|
||||
|
||||
module.exports.timeObjectToUTC = (obj, timezone) => {
|
||||
/**
|
||||
*
|
||||
* @param {object} obj
|
||||
* @param {string} timezone
|
||||
* @returns {object}
|
||||
*/
|
||||
module.exports.timeObjectToUTC = (obj, timezone = undefined) => {
|
||||
return timeObjectConvertTimezone(obj, timezone, true);
|
||||
};
|
||||
|
||||
module.exports.timeObjectToLocal = (obj, timezone) => {
|
||||
/**
|
||||
*
|
||||
* @param {object} obj
|
||||
* @param {string} timezone
|
||||
* @returns {object}
|
||||
*/
|
||||
module.exports.timeObjectToLocal = (obj, timezone = undefined) => {
|
||||
return timeObjectConvertTimezone(obj, timezone, false);
|
||||
};
|
||||
|
Reference in New Issue
Block a user