mirror of
https://github.com/louislam/uptime-kuma.git
synced 2025-08-08 11:42:03 +08:00
- Maintenance standardize datetime format to YYYY-MM-DD hh:mm:ss
- Import dayjs extensions one time only - Maintenance activeCondition centralize
This commit is contained in:
@@ -1,8 +1,4 @@
|
||||
const dayjs = require("dayjs");
|
||||
const utc = require("dayjs/plugin/utc");
|
||||
let timezone = require("dayjs/plugin/timezone");
|
||||
dayjs.extend(utc);
|
||||
dayjs.extend(timezone);
|
||||
const { BeanModel } = require("redbean-node/dist/bean-model");
|
||||
|
||||
/**
|
||||
|
@@ -1,12 +1,9 @@
|
||||
const dayjs = require("dayjs");
|
||||
const utc = require("dayjs/plugin/utc");
|
||||
let timezone = require("dayjs/plugin/timezone");
|
||||
dayjs.extend(utc);
|
||||
dayjs.extend(timezone);
|
||||
const { BeanModel } = require("redbean-node/dist/bean-model");
|
||||
const { parseTimeObject, parseTimeFromTimeObject } = require("../../src/util");
|
||||
const { isArray } = require("chart.js/helpers");
|
||||
const { timeObjectToUTC, timeObjectToLocal } = require("../util-server");
|
||||
const { R } = require("redbean-node");
|
||||
|
||||
class Maintenance extends BeanModel {
|
||||
|
||||
@@ -20,17 +17,18 @@ class Maintenance extends BeanModel {
|
||||
|
||||
let dateTimeRange = [];
|
||||
if (this.start_datetime) {
|
||||
dateTimeRange.push( this.start_datetime);
|
||||
|
||||
dateTimeRange.push(dayjs.utc(this.start_datetime).toISOString());
|
||||
if (this.end_datetime) {
|
||||
dateTimeRange.push( this.end_datetime);
|
||||
dateTimeRange.push(dayjs.utc(this.end_datetime).toISOString());
|
||||
}
|
||||
}
|
||||
|
||||
let dateRange = [];
|
||||
if (this.start_date) {
|
||||
dateRange.push( this.start_date);
|
||||
dateRange.push(dayjs.utc(this.start_date).toISOString());
|
||||
if (this.end_date) {
|
||||
dateRange.push( this.end_date);
|
||||
dateRange.push(dayjs.utc(this.end_date).toISOString());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,18 +104,18 @@ class Maintenance extends BeanModel {
|
||||
bean.active = obj.active;
|
||||
|
||||
if (obj.dateRange[0]) {
|
||||
bean.start_date = obj.dateRange[0];
|
||||
bean.start_date = R.isoDate(dayjs(obj.dateRange[0]).utc());
|
||||
|
||||
if (obj.dateRange[1]) {
|
||||
bean.end_date = obj.dateRange[1];
|
||||
bean.end_date = R.isoDate(dayjs(obj.dateRange[1]).utc());
|
||||
}
|
||||
}
|
||||
|
||||
if (obj.dateTimeRange[0]) {
|
||||
bean.start_datetime = obj.dateTimeRange[0];
|
||||
bean.start_datetime = R.isoDateTime(dayjs(obj.dateTimeRange[0]).utc());
|
||||
|
||||
if (obj.dateTimeRange[1]) {
|
||||
bean.end_datetime = obj.dateTimeRange[1];
|
||||
bean.end_datetime = R.isoDateTime(dayjs(obj.dateTimeRange[1]).utc());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -129,6 +127,22 @@ class Maintenance extends BeanModel {
|
||||
|
||||
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)
|
||||
AND
|
||||
(maintenance.strategy = 'manual' AND active = 1)
|
||||
|
||||
`;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Maintenance;
|
||||
|
@@ -1,6 +1,7 @@
|
||||
const { BeanModel } = require("redbean-node/dist/bean-model");
|
||||
const { R } = require("redbean-node");
|
||||
const dayjs = require("dayjs");
|
||||
const { log } = require("../../src/util");
|
||||
|
||||
class MaintenanceTimeslot extends BeanModel {
|
||||
|
||||
@@ -30,7 +31,9 @@ class MaintenanceTimeslot extends BeanModel {
|
||||
]);
|
||||
}
|
||||
|
||||
if (maintenance.strategy === "single") {
|
||||
if (maintenance.strategy === "manual") {
|
||||
log.debug("maintenance", "No need to generate timeslot for manual type");
|
||||
} else if (maintenance.strategy === "single") {
|
||||
let bean = R.dispense("maintenance_timeslot");
|
||||
bean.maintenance_id = maintenance.id;
|
||||
bean.start_date = maintenance.start_datetime;
|
||||
|
@@ -1,9 +1,5 @@
|
||||
const https = require("https");
|
||||
const dayjs = require("dayjs");
|
||||
const utc = require("dayjs/plugin/utc");
|
||||
let timezone = require("dayjs/plugin/timezone");
|
||||
dayjs.extend(utc);
|
||||
dayjs.extend(timezone);
|
||||
const axios = require("axios");
|
||||
const { Prometheus } = require("../prometheus");
|
||||
const { log, UP, DOWN, PENDING, MAINTENANCE, flipStatus, TimeLogger } = require("../../src/util");
|
||||
@@ -17,6 +13,7 @@ const version = require("../../package.json").version;
|
||||
const apicache = require("../modules/apicache");
|
||||
const { UptimeKumaServer } = require("../uptime-kuma-server");
|
||||
const { CacheableDnsHttpAgent } = require("../cacheable-dns-http-agent");
|
||||
const Maintenance = require("./maintenance");
|
||||
|
||||
/**
|
||||
* status:
|
||||
@@ -1105,6 +1102,7 @@ class Monitor extends BeanModel {
|
||||
* @returns {Promise<boolean>}
|
||||
*/
|
||||
static async isUnderMaintenance(monitorID) {
|
||||
let activeCondition = Maintenance.getActiveMaintenanceSQLCondition();
|
||||
const maintenance = await R.getRow(`
|
||||
SELECT COUNT(*) AS count
|
||||
FROM monitor_maintenance mm
|
||||
@@ -1113,8 +1111,7 @@ class Monitor extends BeanModel {
|
||||
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')
|
||||
AND ${activeCondition}
|
||||
LIMIT 1`, [ monitorID ]);
|
||||
return maintenance.count !== 0;
|
||||
}
|
||||
|
@@ -2,6 +2,7 @@ const { BeanModel } = require("redbean-node/dist/bean-model");
|
||||
const { R } = require("redbean-node");
|
||||
const cheerio = require("cheerio");
|
||||
const { UptimeKumaServer } = require("../uptime-kuma-server");
|
||||
const Maintenance = require("./maintenance");
|
||||
|
||||
class StatusPage extends BeanModel {
|
||||
|
||||
@@ -271,14 +272,14 @@ class StatusPage extends BeanModel {
|
||||
try {
|
||||
const publicMaintenanceList = [];
|
||||
|
||||
let activeCondition = Maintenance.getActiveMaintenanceSQLCondition();
|
||||
let maintenanceBeanList = R.convertToBeans("maintenance", await R.getAll(`
|
||||
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 = ?
|
||||
AND maintenance_timeslot.maintenance.id = m.id
|
||||
AND msp.status_page_id = ?
|
||||
AND ${activeCondition}
|
||||
ORDER BY m.end_date
|
||||
`, [ statusPageId ]));
|
||||
|
||||
|
@@ -33,6 +33,11 @@ log.info("server", "Importing Node libraries");
|
||||
const fs = require("fs");
|
||||
|
||||
log.info("server", "Importing 3rd-party libraries");
|
||||
|
||||
const dayjs = require("dayjs");
|
||||
dayjs.extend(require("dayjs/plugin/utc"));
|
||||
dayjs.extend(require("dayjs/plugin/timezone"));
|
||||
|
||||
log.debug("server", "Importing express");
|
||||
const express = require("express");
|
||||
const expressStaticGzip = require("express-static-gzip");
|
||||
|
@@ -6,11 +6,7 @@ const { UptimeKumaServer } = require("../uptime-kuma-server");
|
||||
const Maintenance = require("../model/maintenance");
|
||||
const server = UptimeKumaServer.getInstance();
|
||||
const dayjs = require("dayjs");
|
||||
const utc = require("dayjs/plugin/utc");
|
||||
let timezone = require("dayjs/plugin/timezone");
|
||||
const MaintenanceTimeslot = require("../model/maintenance_timeslot");
|
||||
dayjs.extend(utc);
|
||||
dayjs.extend(timezone);
|
||||
|
||||
/**
|
||||
* Handlers for Maintenance
|
||||
|
@@ -22,10 +22,6 @@ const {
|
||||
},
|
||||
} = require("node-radius-utils");
|
||||
const dayjs = require("dayjs");
|
||||
const utc = require("dayjs/plugin/utc");
|
||||
let timezone = require("dayjs/plugin/timezone");
|
||||
dayjs.extend(utc);
|
||||
dayjs.extend(timezone);
|
||||
|
||||
// From ping-lite
|
||||
exports.WIN = /^win/.test(process.platform);
|
||||
|
Reference in New Issue
Block a user