diff --git a/server/model/monitor.js b/server/model/monitor.js index b6140c137..4779dd0ea 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -29,7 +29,7 @@ class Monitor extends BeanModel { * Only show necessary data to public */ async toPublicJSON() { - const maintenance = await R.getAll("SELECT mm.*, maintenance.start_date, maintenance.end_date FROM monitor_maintenance mm JOIN maintenance ON mm.maintenance_id = maintenance.id WHERE mm.monitor_id = ? AND datetime(maintenance.start_date) <= datetime('now', 'localtime') AND datetime(maintenance.end_date) >= datetime('now', 'localtime')", [this.id]); + const maintenance = await R.getAll("SELECT mm.*, maintenance.start_date, maintenance.end_date FROM monitor_maintenance mm JOIN maintenance ON mm.maintenance_id = maintenance.id WHERE mm.monitor_id = ? AND datetime(maintenance.start_date) <= datetime('now') AND datetime(maintenance.end_date) >= datetime('now')", [this.id]); return { id: this.id, @@ -54,7 +54,7 @@ class Monitor extends BeanModel { } const tags = await R.getAll("SELECT mt.*, tag.name, tag.color FROM monitor_tag mt JOIN tag ON mt.tag_id = tag.id WHERE mt.monitor_id = ?", [this.id]); - const maintenance = await R.getAll("SELECT mm.*, maintenance.start_date, maintenance.end_date FROM monitor_maintenance mm JOIN maintenance ON mm.maintenance_id = maintenance.id WHERE mm.monitor_id = ? AND datetime(maintenance.start_date) <= datetime('now', 'localtime') AND datetime(maintenance.end_date) >= datetime('now', 'localtime')", [this.id]); + const maintenance = await R.getAll("SELECT mm.*, maintenance.start_date, maintenance.end_date FROM monitor_maintenance mm JOIN maintenance ON mm.maintenance_id = maintenance.id WHERE mm.monitor_id = ? AND datetime(maintenance.start_date) <= datetime('now') AND datetime(maintenance.end_date) >= datetime('now')", [this.id]); return { id: this.id, @@ -142,7 +142,7 @@ class Monitor extends BeanModel { bean.time = R.isoDateTime(dayjs.utc()); bean.status = DOWN; - const maintenance = await R.getAll("SELECT mm.*, maintenance.start_date, maintenance.end_date FROM monitor_maintenance mm JOIN maintenance ON mm.maintenance_id = maintenance.id WHERE mm.monitor_id = ? AND datetime(maintenance.start_date) <= datetime('now', 'localtime') AND datetime(maintenance.end_date) >= datetime('now', 'localtime')", [this.id]); + const maintenance = await R.getAll("SELECT mm.*, maintenance.start_date, maintenance.end_date FROM monitor_maintenance mm JOIN maintenance ON mm.maintenance_id = maintenance.id WHERE mm.monitor_id = ? AND datetime(maintenance.start_date) <= datetime('now') AND datetime(maintenance.end_date) >= datetime('now')", [this.id]); if (this.isUpsideDown()) { bean.status = flipStatus(bean.status); diff --git a/server/routers/api-router.js b/server/routers/api-router.js index 472a837d0..408b94502 100644 --- a/server/routers/api-router.js +++ b/server/routers/api-router.js @@ -51,7 +51,7 @@ router.get("/api/push/:pushToken", async (request, response) => { duration = dayjs(bean.time).diff(dayjs(previousHeartbeat.time), "second"); } - const maintenance = await R.getAll("SELECT mm.*, maintenance.start_date, maintenance.end_date FROM monitor_maintenance mm JOIN maintenance ON mm.maintenance_id = maintenance.id WHERE mm.monitor_id = ? AND datetime(maintenance.start_date) <= datetime('now', 'localtime') AND datetime(maintenance.end_date) >= datetime('now', 'localtime')", [monitor.id]); + const maintenance = await R.getAll("SELECT mm.*, maintenance.start_date, maintenance.end_date FROM monitor_maintenance mm JOIN maintenance ON mm.maintenance_id = maintenance.id WHERE mm.monitor_id = ? AND datetime(maintenance.start_date) <= datetime('now') AND datetime(maintenance.end_date) >= datetime('now')", [monitor.id]); if (maintenance.length !== 0) { msg = "Monitor under maintenance"; status = MAINTENANCE; @@ -149,8 +149,8 @@ router.get("/api/status-page/maintenance-list", async (_request, response) => { let maintenanceBeanList = R.convertToBeans("maintenance", await R.getAll(` SELECT maintenance.* FROM maintenance - WHERE datetime(maintenance.start_date) <= datetime('now', 'localtime') - AND datetime(maintenance.end_date) >= datetime('now', 'localtime') + WHERE datetime(maintenance.start_date) <= datetime('now') + AND datetime(maintenance.end_date) >= datetime('now') ORDER BY maintenance.end_date `)); diff --git a/src/components/MonitorList.vue b/src/components/MonitorList.vue index 4e6610ad7..696a02e7a 100644 --- a/src/components/MonitorList.vue +++ b/src/components/MonitorList.vue @@ -25,7 +25,7 @@ {{ $t("No Maintenance, please") }} {{ $t("add one") }} - +
@@ -66,7 +66,7 @@ import HeartbeatBar from "../components/HeartbeatBar.vue"; import Uptime from "../components/Uptime.vue"; import Tag from "../components/Tag.vue"; -import {getMaintenanceRelativeURL, getMonitorRelativeURL } from "../util.ts"; +import { getMaintenanceRelativeURL, getMonitorRelativeURL } from "../util.ts"; export default { components: { @@ -90,18 +90,17 @@ export default { let result = Object.values(this.$root.maintenanceList); result.sort((m1, m2) => { - const now = Date.now(); - if (Date.parse(m1.end_date) >= now !== Date.parse(m2.end_date) >= now) { - if (Date.parse(m2.end_date) < now) { + if (this.$root.isActiveMaintenance(m1.end_date) !== this.$root.isActiveMaintenance(m2.end_date)) { + if (!this.$root.isActiveMaintenance(m2.end_date)) { return -1; } - if (Date.parse(m1.end_date) < now) { + if (!this.$root.isActiveMaintenance(m1.end_date)) { return 1; } } - if (Date.parse(m1.end_date) >= now && Date.parse(m2.end_date) >= now) { + if (this.$root.isActiveMaintenance(m1.end_date) && this.$root.isActiveMaintenance(m2.end_date)) { if (Date.parse(m1.end_date) < Date.parse(m2.end_date)) { return -1; } @@ -111,7 +110,7 @@ export default { } } - if (Date.parse(m1.end_date) < now && Date.parse(m2.end_date) < now) { + if (!this.$root.isActiveMaintenance(m1.end_date) && !this.$root.isActiveMaintenance(m2.end_date)) { if (Date.parse(m1.end_date) < Date.parse(m2.end_date)) { return 1; } diff --git a/src/mixins/datetime.js b/src/mixins/datetime.js index 08689520f..3f4749af6 100644 --- a/src/mixins/datetime.js +++ b/src/mixins/datetime.js @@ -18,6 +18,14 @@ export default { }, methods: { + isActiveMaintenance(endDate) { + return (dayjs.utc(endDate).unix() >= dayjs.utc().unix()); + }, + + toUTC(value) { + return dayjs.tz(value, this.timezone).utc().format(); + }, + datetime(value) { return this.datetimeFormat(value, "YYYY-MM-DD HH:mm:ss"); }, @@ -26,10 +34,10 @@ export default { const inputDate = new Date(value); const now = new Date(Date.now()); - if (inputDate.getFullYear() === now.getFullYear() && inputDate.getMonth() === now.getMonth() && inputDate.getDay() === now.getDay()) - return this.datetimeMaintenanceFormat(value, "HH:mm"); + if (inputDate.getFullYear() === now.getUTCFullYear() && inputDate.getMonth() === now.getUTCMonth() && inputDate.getDay() === now.getUTCDay()) + return this.datetimeFormat(value, "HH:mm"); else - return this.datetimeMaintenanceFormat(value, "YYYY-MM-DD HH:mm"); + return this.datetimeFormat(value, "YYYY-MM-DD HH:mm"); }, date(value) { @@ -52,13 +60,6 @@ export default { } return ""; }, - - datetimeMaintenanceFormat(value, format) { - if (value !== undefined && value !== "") { - return dayjs(value).format(format); - } - return ""; - } }, computed: { diff --git a/src/pages/EditMaintenance.vue b/src/pages/EditMaintenance.vue index 6b6f80577..e50affaad 100644 --- a/src/pages/EditMaintenance.vue +++ b/src/pages/EditMaintenance.vue @@ -50,14 +50,14 @@
- +
- +
@@ -156,6 +156,8 @@ export default { } else if (this.isEdit) { this.$root.getSocket().emit("getMaintenance", this.$route.params.id, (res) => { if (res.ok) { + res.maintenance.start_date = this.$root.datetimeFormat(res.maintenance.start_date, "YYYY-MM-DDTHH:mm"); + res.maintenance.end_date = this.$root.datetimeFormat(res.maintenance.end_date, "YYYY-MM-DDTHH:mm"); this.maintenance = res.maintenance; this.$root.getSocket().emit("getMonitorMaintenance", this.$route.params.id, (res) => { @@ -182,9 +184,11 @@ export default { return this.processing = false; } + this.maintenance.start_date = this.$root.toUTC(this.maintenance.start_date); + this.maintenance.end_date = this.$root.toUTC(this.maintenance.end_date); + if (this.isAdd) { this.$root.addMaintenance(this.maintenance, async (res) => { - if (res.ok) { await this.addMonitorMaintenance(res.maintenanceID, () => { toast.success(res.msg); @@ -206,8 +210,7 @@ export default { this.$root.toastRes(res); this.init(); }); - } - else { + } else { this.processing = false; toast.error(res.msg); } diff --git a/src/pages/StatusPage.vue b/src/pages/StatusPage.vue index 11716b454..fdddba74a 100644 --- a/src/pages/StatusPage.vue +++ b/src/pages/StatusPage.vue @@ -152,7 +152,7 @@
- {{ $t("End") }}: {{ $root.datetimeMaintenance(maintenanceItem.end_date) }} ({{ dateFromNowMaintenance(maintenanceItem.start_date) }})
+ {{ $t("End") }}: {{ $root.datetimeMaintenance(maintenanceItem.end_date) }} ({{ dateFromNow(maintenanceItem.start_date) }})
@@ -620,10 +620,6 @@ export default { return dayjs.utc(date).fromNow(); }, - dateFromNowMaintenance(date) { - return dayjs(date).fromNow(); - }, - } };