- 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:
Louis Lam 2022-09-28 00:20:17 +08:00
parent 4002b9f577
commit b1465c0282
14 changed files with 51 additions and 61 deletions

View file

@ -1,8 +1,4 @@
const dayjs = require("dayjs"); 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 { BeanModel } = require("redbean-node/dist/bean-model");
/** /**

View file

@ -1,12 +1,9 @@
const dayjs = require("dayjs"); 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 { BeanModel } = require("redbean-node/dist/bean-model");
const { parseTimeObject, parseTimeFromTimeObject } = require("../../src/util"); const { parseTimeObject, parseTimeFromTimeObject } = require("../../src/util");
const { isArray } = require("chart.js/helpers"); const { isArray } = require("chart.js/helpers");
const { timeObjectToUTC, timeObjectToLocal } = require("../util-server"); const { timeObjectToUTC, timeObjectToLocal } = require("../util-server");
const { R } = require("redbean-node");
class Maintenance extends BeanModel { class Maintenance extends BeanModel {
@ -20,17 +17,18 @@ class Maintenance extends BeanModel {
let dateTimeRange = []; let dateTimeRange = [];
if (this.start_datetime) { if (this.start_datetime) {
dateTimeRange.push( this.start_datetime);
dateTimeRange.push(dayjs.utc(this.start_datetime).toISOString());
if (this.end_datetime) { if (this.end_datetime) {
dateTimeRange.push( this.end_datetime); dateTimeRange.push(dayjs.utc(this.end_datetime).toISOString());
} }
} }
let dateRange = []; let dateRange = [];
if (this.start_date) { if (this.start_date) {
dateRange.push( this.start_date); dateRange.push(dayjs.utc(this.start_date).toISOString());
if (this.end_date) { 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; bean.active = obj.active;
if (obj.dateRange[0]) { if (obj.dateRange[0]) {
bean.start_date = obj.dateRange[0]; bean.start_date = R.isoDate(dayjs(obj.dateRange[0]).utc());
if (obj.dateRange[1]) { if (obj.dateRange[1]) {
bean.end_date = obj.dateRange[1]; bean.end_date = R.isoDate(dayjs(obj.dateRange[1]).utc());
} }
} }
if (obj.dateTimeRange[0]) { if (obj.dateTimeRange[0]) {
bean.start_datetime = obj.dateTimeRange[0]; bean.start_datetime = R.isoDateTime(dayjs(obj.dateTimeRange[0]).utc());
if (obj.dateTimeRange[1]) { 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; 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; module.exports = Maintenance;

View file

@ -1,6 +1,7 @@
const { BeanModel } = require("redbean-node/dist/bean-model"); const { BeanModel } = require("redbean-node/dist/bean-model");
const { R } = require("redbean-node"); const { R } = require("redbean-node");
const dayjs = require("dayjs"); const dayjs = require("dayjs");
const { log } = require("../../src/util");
class MaintenanceTimeslot extends BeanModel { 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"); let bean = R.dispense("maintenance_timeslot");
bean.maintenance_id = maintenance.id; bean.maintenance_id = maintenance.id;
bean.start_date = maintenance.start_datetime; bean.start_date = maintenance.start_datetime;

View file

@ -1,9 +1,5 @@
const https = require("https"); const https = require("https");
const dayjs = require("dayjs"); 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 axios = require("axios");
const { Prometheus } = require("../prometheus"); const { Prometheus } = require("../prometheus");
const { log, UP, DOWN, PENDING, MAINTENANCE, flipStatus, TimeLogger } = require("../../src/util"); 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 apicache = require("../modules/apicache");
const { UptimeKumaServer } = require("../uptime-kuma-server"); const { UptimeKumaServer } = require("../uptime-kuma-server");
const { CacheableDnsHttpAgent } = require("../cacheable-dns-http-agent"); const { CacheableDnsHttpAgent } = require("../cacheable-dns-http-agent");
const Maintenance = require("./maintenance");
/** /**
* status: * status:
@ -1105,6 +1102,7 @@ class Monitor extends BeanModel {
* @returns {Promise<boolean>} * @returns {Promise<boolean>}
*/ */
static async isUnderMaintenance(monitorID) { static async isUnderMaintenance(monitorID) {
let activeCondition = Maintenance.getActiveMaintenanceSQLCondition();
const maintenance = await R.getRow(` const maintenance = await R.getRow(`
SELECT COUNT(*) AS count SELECT COUNT(*) AS count
FROM monitor_maintenance mm FROM monitor_maintenance mm
@ -1113,8 +1111,7 @@ class Monitor extends BeanModel {
JOIN maintenance_timeslot JOIN maintenance_timeslot
ON maintenance_timeslot.maintenance_id = maintenance.id ON maintenance_timeslot.maintenance_id = maintenance.id
WHERE mm.monitor_id = ? WHERE mm.monitor_id = ?
AND maintenance_timeslot.start_date <= DATETIME('now') AND ${activeCondition}
AND maintenance_timeslot.end_date >= DATETIME('now')
LIMIT 1`, [ monitorID ]); LIMIT 1`, [ monitorID ]);
return maintenance.count !== 0; return maintenance.count !== 0;
} }

View file

@ -2,6 +2,7 @@ const { BeanModel } = require("redbean-node/dist/bean-model");
const { R } = require("redbean-node"); const { R } = require("redbean-node");
const cheerio = require("cheerio"); const cheerio = require("cheerio");
const { UptimeKumaServer } = require("../uptime-kuma-server"); const { UptimeKumaServer } = require("../uptime-kuma-server");
const Maintenance = require("./maintenance");
class StatusPage extends BeanModel { class StatusPage extends BeanModel {
@ -271,14 +272,14 @@ class StatusPage extends BeanModel {
try { try {
const publicMaintenanceList = []; const publicMaintenanceList = [];
let activeCondition = Maintenance.getActiveMaintenanceSQLCondition();
let maintenanceBeanList = R.convertToBeans("maintenance", await R.getAll(` let maintenanceBeanList = R.convertToBeans("maintenance", await R.getAll(`
SELECT m.* SELECT m.*
FROM maintenance m, maintenance_status_page msp, maintenance_timeslot FROM maintenance m, maintenance_status_page msp, maintenance_timeslot
WHERE msp.maintenance_id = m.id WHERE msp.maintenance_id = m.id
AND maintenance_timeslot.maintenance.id = m.id AND maintenance_timeslot.maintenance.id = m.id
AND maintenance_timeslot.start_date <= DATETIME('now') AND msp.status_page_id = ?
AND maintenance_timeslot.end_date >= DATETIME('now') AND ${activeCondition}
AND msp.status_page_id = ?
ORDER BY m.end_date ORDER BY m.end_date
`, [ statusPageId ])); `, [ statusPageId ]));

View file

@ -33,6 +33,11 @@ log.info("server", "Importing Node libraries");
const fs = require("fs"); const fs = require("fs");
log.info("server", "Importing 3rd-party libraries"); 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"); log.debug("server", "Importing express");
const express = require("express"); const express = require("express");
const expressStaticGzip = require("express-static-gzip"); const expressStaticGzip = require("express-static-gzip");

View file

@ -6,11 +6,7 @@ const { UptimeKumaServer } = require("../uptime-kuma-server");
const Maintenance = require("../model/maintenance"); const Maintenance = require("../model/maintenance");
const server = UptimeKumaServer.getInstance(); const server = UptimeKumaServer.getInstance();
const dayjs = require("dayjs"); const dayjs = require("dayjs");
const utc = require("dayjs/plugin/utc");
let timezone = require("dayjs/plugin/timezone");
const MaintenanceTimeslot = require("../model/maintenance_timeslot"); const MaintenanceTimeslot = require("../model/maintenance_timeslot");
dayjs.extend(utc);
dayjs.extend(timezone);
/** /**
* Handlers for Maintenance * Handlers for Maintenance

View file

@ -22,10 +22,6 @@ const {
}, },
} = require("node-radius-utils"); } = require("node-radius-utils");
const dayjs = require("dayjs"); 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 // From ping-lite
exports.WIN = /^win/.test(process.platform); exports.WIN = /^win/.test(process.platform);

View file

@ -4,12 +4,6 @@
<script> <script>
import dayjs from "dayjs"; import dayjs from "dayjs";
import relativeTime from "dayjs/plugin/relativeTime";
import timezone from "dayjs/plugin/timezone"; // dependent on utc plugin
import utc from "dayjs/plugin/utc";
dayjs.extend(utc);
dayjs.extend(timezone);
dayjs.extend(relativeTime);
export default { export default {
props: { props: {

View file

@ -16,18 +16,14 @@
</div> </div>
</template> </template>
<script lang="ts"> <script lang="js">
import { BarController, BarElement, Chart, Filler, LinearScale, LineController, LineElement, PointElement, TimeScale, Tooltip } from "chart.js"; import { BarController, BarElement, Chart, Filler, LinearScale, LineController, LineElement, PointElement, TimeScale, Tooltip } from "chart.js";
import "chartjs-adapter-dayjs"; import "chartjs-adapter-dayjs";
import dayjs from "dayjs"; import dayjs from "dayjs";
import timezone from "dayjs/plugin/timezone";
import utc from "dayjs/plugin/utc";
import { LineChart } from "vue-chart-3"; import { LineChart } from "vue-chart-3";
import { useToast } from "vue-toastification"; import { useToast } from "vue-toastification";
import { DOWN, PENDING, MAINTENANCE, log } from "../util.ts"; import { DOWN, PENDING, MAINTENANCE, log } from "../util.ts";
dayjs.extend(utc);
dayjs.extend(timezone);
const toast = useToast(); const toast = useToast();
Chart.register(LineController, BarController, LineElement, PointElement, TimeScale, BarElement, LinearScale, Tooltip, Filler); Chart.register(LineController, BarController, LineElement, PointElement, TimeScale, BarElement, LinearScale, Tooltip, Filler);

View file

@ -145,11 +145,7 @@
<script> <script>
import HiddenInput from "../../components/HiddenInput.vue"; import HiddenInput from "../../components/HiddenInput.vue";
import dayjs from "dayjs"; import dayjs from "dayjs";
import utc from "dayjs/plugin/utc";
import timezone from "dayjs/plugin/timezone";
import { timezoneList } from "../../util-frontend"; import { timezoneList } from "../../util-frontend";
dayjs.extend(utc);
dayjs.extend(timezone);
export default { export default {
components: { components: {

View file

@ -16,6 +16,13 @@ import theme from "./mixins/theme";
import lang from "./mixins/lang"; import lang from "./mixins/lang";
import { router } from "./router"; import { router } from "./router";
import { appName } from "./util.ts"; import { appName } from "./util.ts";
import dayjs from "dayjs";
import timezone from "dayjs/plugin/timezone";
import utc from "dayjs/plugin/utc";
import relativeTime from "dayjs/plugin/relativeTime";
dayjs.extend(utc);
dayjs.extend(timezone);
dayjs.extend(relativeTime);
const app = createApp({ const app = createApp({
mixins: [ mixins: [

View file

@ -1,10 +1,4 @@
import dayjs from "dayjs"; import dayjs from "dayjs";
import relativeTime from "dayjs/plugin/relativeTime";
import timezone from "dayjs/plugin/timezone";
import utc from "dayjs/plugin/utc";
dayjs.extend(utc);
dayjs.extend(timezone);
dayjs.extend(relativeTime);
/** /**
* DateTime Mixin * DateTime Mixin

View file

@ -1,12 +1,7 @@
import dayjs from "dayjs"; import dayjs from "dayjs";
import timezone from "dayjs/plugin/timezone";
import utc from "dayjs/plugin/utc";
import timezones from "timezones-list"; import timezones from "timezones-list";
import { localeDirection, currentLocale } from "./i18n"; import { localeDirection, currentLocale } from "./i18n";
dayjs.extend(utc);
dayjs.extend(timezone);
/** /**
* Returns the offset from UTC in hours for the current locale. * Returns the offset from UTC in hours for the current locale.
* @returns {number} The offset from UTC in hours. * @returns {number} The offset from UTC in hours.