From b1465c0282dde17c34171b3978c96cdac2c7dace Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Wed, 28 Sep 2022 00:20:17 +0800 Subject: [PATCH] - Maintenance standardize datetime format to YYYY-MM-DD hh:mm:ss - Import dayjs extensions one time only - Maintenance activeCondition centralize --- server/model/heartbeat.js | 4 -- server/model/maintenance.js | 38 +++++++++++++------ server/model/maintenance_timeslot.js | 5 ++- server/model/monitor.js | 9 ++--- server/model/status_page.js | 9 +++-- server/server.js | 5 +++ .../maintenance-socket-handler.js | 4 -- server/util-server.js | 4 -- src/components/Datetime.vue | 6 --- src/components/PingChart.vue | 6 +-- src/components/settings/General.vue | 4 -- src/main.js | 7 ++++ src/mixins/datetime.js | 6 --- src/util-frontend.js | 5 --- 14 files changed, 51 insertions(+), 61 deletions(-) diff --git a/server/model/heartbeat.js b/server/model/heartbeat.js index 8c9d8645..fa02cae8 100644 --- a/server/model/heartbeat.js +++ b/server/model/heartbeat.js @@ -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"); /** diff --git a/server/model/maintenance.js b/server/model/maintenance.js index 3b07d5f7..945e4d97 100644 --- a/server/model/maintenance.js +++ b/server/model/maintenance.js @@ -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; diff --git a/server/model/maintenance_timeslot.js b/server/model/maintenance_timeslot.js index f749caa5..7917dd69 100644 --- a/server/model/maintenance_timeslot.js +++ b/server/model/maintenance_timeslot.js @@ -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; diff --git a/server/model/monitor.js b/server/model/monitor.js index a13d7051..9df127b0 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -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} */ 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; } diff --git a/server/model/status_page.js b/server/model/status_page.js index 4351db58..0620a1ee 100644 --- a/server/model/status_page.js +++ b/server/model/status_page.js @@ -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 ])); diff --git a/server/server.js b/server/server.js index 4aec2b27..1ad99899 100644 --- a/server/server.js +++ b/server/server.js @@ -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"); diff --git a/server/socket-handlers/maintenance-socket-handler.js b/server/socket-handlers/maintenance-socket-handler.js index 5358b53e..9ae36b5c 100644 --- a/server/socket-handlers/maintenance-socket-handler.js +++ b/server/socket-handlers/maintenance-socket-handler.js @@ -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 diff --git a/server/util-server.js b/server/util-server.js index cc5e478d..1c5f5914 100644 --- a/server/util-server.js +++ b/server/util-server.js @@ -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); diff --git a/src/components/Datetime.vue b/src/components/Datetime.vue index b24ab0b3..84bae503 100644 --- a/src/components/Datetime.vue +++ b/src/components/Datetime.vue @@ -4,12 +4,6 @@