From 0d3414c6d6089f7b41f6bb4b1729f01ab2cb5e62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Kr=C3=BDda?= Date: Sun, 23 Jan 2022 15:22:00 +0100 Subject: [PATCH 001/134] A complete maintenance planning system has been created --- db/patch-maintenance-table.sql | 25 +++ package-lock.json | 54 ++++--- server/database.js | 1 + server/model/heartbeat.js | 1 + server/model/maintenance.js | 38 +++++ server/model/monitor.js | 64 +++++++- server/routers/api-router.js | 38 ++++- server/server.js | 217 +++++++++++++++++++++++++ src/assets/app.scss | 1 + src/assets/vars.scss | 1 + src/components/HeartbeatBar.vue | 6 +- src/components/MonitorList.vue | 91 ++++++++++- src/components/PingChart.vue | 10 +- src/components/PublicGroupList.vue | 4 + src/components/Status.vue | 8 + src/components/Uptime.vue | 8 + src/icon.js | 2 + src/languages/en.js | 2 + src/languages/zh-TW.js | 1 - src/layouts/Layout.vue | 26 ++- src/mixins/datetime.js | 17 ++ src/mixins/socket.js | 36 ++++- src/pages/Dashboard.vue | 1 + src/pages/DashboardHome.vue | 22 ++- src/pages/Details.vue | 4 + src/pages/EditMaintenance.vue | 247 +++++++++++++++++++++++++++++ src/pages/EditMonitor.vue | 2 +- src/pages/MaintenanceDetails.vue | 141 ++++++++++++++++ src/pages/StatusPage.vue | 64 +++++++- src/router.js | 22 ++- src/util.js | 10 +- src/util.ts | 8 +- 32 files changed, 1121 insertions(+), 51 deletions(-) create mode 100644 db/patch-maintenance-table.sql create mode 100644 server/model/maintenance.js create mode 100644 src/pages/EditMaintenance.vue create mode 100644 src/pages/MaintenanceDetails.vue diff --git a/db/patch-maintenance-table.sql b/db/patch-maintenance-table.sql new file mode 100644 index 000000000..ee4a7f882 --- /dev/null +++ b/db/patch-maintenance-table.sql @@ -0,0 +1,25 @@ +-- You should not modify if this have pushed to Github, unless it does serious wrong with the db. +BEGIN TRANSACTION; + +CREATE TABLE maintenance +( + id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, + title VARCHAR(150), + description TEXT, + user_id INTEGER REFERENCES user ON UPDATE CASCADE ON DELETE SET NULL, + start_date DATETIME, + end_date DATETIME +); + +CREATE TABLE monitor_maintenance +( + id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, + monitor_id INTEGER NOT NULL, + maintenance_id INTEGER NOT NULL, + CONSTRAINT FK_maintenance FOREIGN KEY (maintenance_id) REFERENCES maintenance (id) ON DELETE CASCADE ON UPDATE CASCADE, + CONSTRAINT FK_monitor FOREIGN KEY (monitor_id) REFERENCES monitor (id) ON DELETE CASCADE ON UPDATE CASCADE +); + +create index maintenance_user_id on maintenance (user_id); + +COMMIT; diff --git a/package-lock.json b/package-lock.json index fc21a63f9..7ab00b753 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14914,7 +14914,8 @@ "@fortawesome/vue-fontawesome": { "version": "3.0.0-5", "resolved": "https://registry.npmjs.org/@fortawesome/vue-fontawesome/-/vue-fontawesome-3.0.0-5.tgz", - "integrity": "sha512-aNmBT4bOecrFsZTog1l6AJDQHPP3ocXV+WQ3Ogy8WZCqstB/ahfhH4CPu5i4N9Hw0MBKXqE+LX+NbUxcj8cVTw==" + "integrity": "sha512-aNmBT4bOecrFsZTog1l6AJDQHPP3ocXV+WQ3Ogy8WZCqstB/ahfhH4CPu5i4N9Hw0MBKXqE+LX+NbUxcj8cVTw==", + "requires": {} }, "@gar/promisify": { "version": "1.1.2", @@ -16117,7 +16118,8 @@ "version": "1.9.4", "resolved": "https://registry.npmjs.org/@vitejs/plugin-vue/-/plugin-vue-1.9.4.tgz", "integrity": "sha512-0CZqaCoChriPTTtGkERy1LGPcYjGFpi2uYRhBPIkqJqUGV5JnJFhQAgh6oH9j5XZHfrRaisX8W0xSpO4T7S78A==", - "dev": true + "dev": true, + "requires": {} }, "@vue/compiler-core": { "version": "3.2.22", @@ -16277,7 +16279,8 @@ "version": "5.3.2", "resolved": "https://registry.npmjs.org/acorn-jsx/-/acorn-jsx-5.3.2.tgz", "integrity": "sha512-rq9s+JNhf0IChjtDXxllJ7g41oZk5SlXtp0LHwyA5cejwn7vKmKp4pPri6YEePv2PU65sAsegbXtIinmDFDXgQ==", - "dev": true + "dev": true, + "requires": {} }, "acorn-walk": { "version": "7.2.0", @@ -16766,7 +16769,8 @@ "bootstrap": { "version": "5.1.3", "resolved": "https://registry.npmjs.org/bootstrap/-/bootstrap-5.1.3.tgz", - "integrity": "sha512-fcQztozJ8jToQWXxVuEyXWW+dSo8AiXWKwiSSrKWsRB/Qt+Ewwza+JWoLKiTuQLaEPhdNAJ7+Dosc9DOIqNy7Q==" + "integrity": "sha512-fcQztozJ8jToQWXxVuEyXWW+dSo8AiXWKwiSSrKWsRB/Qt+Ewwza+JWoLKiTuQLaEPhdNAJ7+Dosc9DOIqNy7Q==", + "requires": {} }, "brace-expansion": { "version": "1.1.11", @@ -16958,7 +16962,8 @@ "chartjs-adapter-dayjs": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/chartjs-adapter-dayjs/-/chartjs-adapter-dayjs-1.0.0.tgz", - "integrity": "sha512-EnbVqTJGFKLpg1TROLdCEufrzbmIa2oeLGx8O2Wdjw2EoMudoOo9+YFu+6CM0Z0hQ/v3yq/e/Y6efQMu22n8Jg==" + "integrity": "sha512-EnbVqTJGFKLpg1TROLdCEufrzbmIa2oeLGx8O2Wdjw2EoMudoOo9+YFu+6CM0Z0hQ/v3yq/e/Y6efQMu22n8Jg==", + "requires": {} }, "check-password-strength": { "version": "2.0.3", @@ -17548,7 +17553,8 @@ "ws": { "version": "8.2.3", "resolved": "https://registry.npmjs.org/ws/-/ws-8.2.3.tgz", - "integrity": "sha512-wBuoj1BDpC6ZQ1B7DWQBYVLphPWkm8i9Y0/3YdHjHKHiohOJ1ws+3OccDWtH+PoC9DZD5WOTrJvNbWvjS6JWaA==" + "integrity": "sha512-wBuoj1BDpC6ZQ1B7DWQBYVLphPWkm8i9Y0/3YdHjHKHiohOJ1ws+3OccDWtH+PoC9DZD5WOTrJvNbWvjS6JWaA==", + "requires": {} } } }, @@ -17571,7 +17577,8 @@ "ws": { "version": "8.2.3", "resolved": "https://registry.npmjs.org/ws/-/ws-8.2.3.tgz", - "integrity": "sha512-wBuoj1BDpC6ZQ1B7DWQBYVLphPWkm8i9Y0/3YdHjHKHiohOJ1ws+3OccDWtH+PoC9DZD5WOTrJvNbWvjS6JWaA==" + "integrity": "sha512-wBuoj1BDpC6ZQ1B7DWQBYVLphPWkm8i9Y0/3YdHjHKHiohOJ1ws+3OccDWtH+PoC9DZD5WOTrJvNbWvjS6JWaA==", + "requires": {} } } }, @@ -20015,7 +20022,8 @@ "version": "1.2.2", "resolved": "https://registry.npmjs.org/jest-pnp-resolver/-/jest-pnp-resolver-1.2.2.tgz", "integrity": "sha512-olV41bKSMm8BdnuMsewT4jqlZ8+3TCARAXjZGT9jcoSnrfUnRCqnMoF9XEeoWjbzObpqF9dRhHQj0Xb9QdF6/w==", - "dev": true + "dev": true, + "requires": {} }, "jest-puppeteer": { "version": "6.0.0", @@ -21774,12 +21782,14 @@ "version": "6.0.0", "resolved": "https://registry.npmjs.org/postcss-safe-parser/-/postcss-safe-parser-6.0.0.tgz", "integrity": "sha512-FARHN8pwH+WiS2OPCxJI8FuRJpTVnn6ZNFiqAM2aeW2LwTHWWmWgIyKC6cUo0L8aeKiF/14MNvnpls6R2PBeMQ==", - "dev": true + "dev": true, + "requires": {} }, "postcss-scss": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/postcss-scss/-/postcss-scss-4.0.2.tgz", - "integrity": "sha512-xfdkU128CkKKKVAwkyt0M8OdnelJ3MRcIRAPPQkRpoPeuzWY3RIeg7piRCpZ79MK7Q16diLXMMAD9dN5mauPlQ==" + "integrity": "sha512-xfdkU128CkKKKVAwkyt0M8OdnelJ3MRcIRAPPQkRpoPeuzWY3RIeg7piRCpZ79MK7Q16diLXMMAD9dN5mauPlQ==", + "requires": {} }, "postcss-selector-parser": { "version": "6.0.8", @@ -21979,7 +21989,8 @@ "version": "7.4.6", "resolved": "https://registry.npmjs.org/ws/-/ws-7.4.6.tgz", "integrity": "sha512-YmhHDO4MzaDLB+M9ym/mDA5z0naX8j7SIlT8f8z+I0VtzsRbekxEutHSme7NPS2qE8StCYQNUnfWdXta/Yu85A==", - "dev": true + "dev": true, + "requires": {} } } }, @@ -23080,7 +23091,8 @@ "version": "6.0.0", "resolved": "https://registry.npmjs.org/stylelint-config-recommended/-/stylelint-config-recommended-6.0.0.tgz", "integrity": "sha512-ZorSSdyMcxWpROYUvLEMm0vSZud2uB7tX1hzBZwvVY9SV/uly4AvvJPPhCcymZL3fcQhEQG5AELmrxWqtmzacw==", - "dev": true + "dev": true, + "requires": {} }, "stylelint-config-standard": { "version": "24.0.0", @@ -23653,17 +23665,20 @@ "vue-confirm-dialog": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/vue-confirm-dialog/-/vue-confirm-dialog-1.0.2.tgz", - "integrity": "sha512-gTo1bMDWOLd/6ihmWv8VlPxhc9QaKoE5YqlsKydUOfrrQ3Q3taljF6yI+1TMtAtJLrvZ8DYrePhgBhY1VCJzbQ==" + "integrity": "sha512-gTo1bMDWOLd/6ihmWv8VlPxhc9QaKoE5YqlsKydUOfrrQ3Q3taljF6yI+1TMtAtJLrvZ8DYrePhgBhY1VCJzbQ==", + "requires": {} }, "vue-contenteditable": { "version": "3.0.4", "resolved": "https://registry.npmjs.org/vue-contenteditable/-/vue-contenteditable-3.0.4.tgz", - "integrity": "sha512-CmtqT4zHQwLoJEyNVaXUjjUFPUVYlXXBHfSbRCHCUjODMqrn6L293LM1nc1ELx8epitZZvecTfIqOLlSzB3d+w==" + "integrity": "sha512-CmtqT4zHQwLoJEyNVaXUjjUFPUVYlXXBHfSbRCHCUjODMqrn6L293LM1nc1ELx8epitZZvecTfIqOLlSzB3d+w==", + "requires": {} }, "vue-demi": { "version": "0.10.1", "resolved": "https://registry.npmjs.org/vue-demi/-/vue-demi-0.10.1.tgz", - "integrity": "sha512-L6Oi+BvmMv6YXvqv5rJNCFHEKSVu7llpWWJczqmAQYOdmPPw5PNYoz1KKS//Fxhi+4QP64dsPjtmvnYGo1jemA==" + "integrity": "sha512-L6Oi+BvmMv6YXvqv5rJNCFHEKSVu7llpWWJczqmAQYOdmPPw5PNYoz1KKS//Fxhi+4QP64dsPjtmvnYGo1jemA==", + "requires": {} }, "vue-eslint-parser": { "version": "7.11.0", @@ -23735,7 +23750,8 @@ "vue-demi": { "version": "0.11.4", "resolved": "https://registry.npmjs.org/vue-demi/-/vue-demi-0.11.4.tgz", - "integrity": "sha512-/3xFwzSykLW2HiiLie43a+FFgNOcokbBJ+fzvFXd0r2T8MYohqvphUyDQ8lbAwzQ3Dlcrb1c9ykifGkhSIAk6A==" + "integrity": "sha512-/3xFwzSykLW2HiiLie43a+FFgNOcokbBJ+fzvFXd0r2T8MYohqvphUyDQ8lbAwzQ3Dlcrb1c9ykifGkhSIAk6A==", + "requires": {} } } }, @@ -23750,7 +23766,8 @@ "vue-toastification": { "version": "2.0.0-rc.5", "resolved": "https://registry.npmjs.org/vue-toastification/-/vue-toastification-2.0.0-rc.5.tgz", - "integrity": "sha512-q73e5jy6gucEO/U+P48hqX+/qyXDozAGmaGgLFm5tXX4wJBcVsnGp4e/iJqlm9xzHETYOilUuwOUje2Qg1JdwA==" + "integrity": "sha512-q73e5jy6gucEO/U+P48hqX+/qyXDozAGmaGgLFm5tXX4wJBcVsnGp4e/iJqlm9xzHETYOilUuwOUje2Qg1JdwA==", + "requires": {} }, "vuedraggable": { "version": "4.1.0", @@ -23929,7 +23946,8 @@ "version": "7.5.5", "resolved": "https://registry.npmjs.org/ws/-/ws-7.5.5.tgz", "integrity": "sha512-BAkMFcAzl8as1G/hArkxOxq3G7pjUqQ3gzYbLL0/5zNkph70e+lCoxBGnm6AW1+/aiNeV4fnKqZ8m4GZewmH2w==", - "dev": true + "dev": true, + "requires": {} }, "xml-name-validator": { "version": "3.0.0", diff --git a/server/database.js b/server/database.js index afcace705..6645e537a 100644 --- a/server/database.js +++ b/server/database.js @@ -53,6 +53,7 @@ class Database { "patch-2fa-invalidate-used-token.sql": true, "patch-notification_sent_history.sql": true, "patch-monitor-basic-auth.sql": true, + "patch-maintenance-table.sql": true, } /** diff --git a/server/model/heartbeat.js b/server/model/heartbeat.js index e0a77c069..617ac598c 100644 --- a/server/model/heartbeat.js +++ b/server/model/heartbeat.js @@ -10,6 +10,7 @@ const { BeanModel } = require("redbean-node/dist/bean-model"); * 0 = DOWN * 1 = UP * 2 = PENDING + * 3 = MAINTENANCE */ class Heartbeat extends BeanModel { diff --git a/server/model/maintenance.js b/server/model/maintenance.js new file mode 100644 index 000000000..4958a203e --- /dev/null +++ b/server/model/maintenance.js @@ -0,0 +1,38 @@ +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"); + +class Maintenance extends BeanModel { + + /** + * Return a object that ready to parse to JSON for public + * Only show necessary data to public + */ + async toPublicJSON() { + return { + id: this.id, + title: this.title, + description: this.description, + start_date: this.start_date, + end_date: this.end_date + }; + } + + /** + * Return a object that ready to parse to JSON + */ + async toJSON() { + return { + id: this.id, + title: this.title, + description: this.description, + start_date: this.start_date, + end_date: this.end_date + }; + } +} + +module.exports = Maintenance; diff --git a/server/model/monitor.js b/server/model/monitor.js index c4441d63e..cd62ec6bc 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -6,7 +6,7 @@ dayjs.extend(utc); dayjs.extend(timezone); const axios = require("axios"); const { Prometheus } = require("../prometheus"); -const { debug, UP, DOWN, PENDING, flipStatus, TimeLogger } = require("../../src/util"); +const { debug, UP, DOWN, PENDING, MAINTENANCE, flipStatus, TimeLogger} = require("../../src/util"); const { tcping, ping, dnsResolve, checkCertificate, checkStatusCode, getTotalClientInRoom, setting, errorLog } = require("../util-server"); const { R } = require("redbean-node"); const { BeanModel } = require("redbean-node/dist/bean-model"); @@ -20,6 +20,7 @@ const apicache = require("../modules/apicache"); * 0 = DOWN * 1 = UP * 2 = PENDING + * 3 = MAINTENANCE */ class Monitor extends BeanModel { @@ -28,9 +29,12 @@ 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]); + return { id: this.id, name: this.name, + maintenance: (maintenance.length !== 0), }; } @@ -50,6 +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]); return { id: this.id, @@ -79,6 +84,7 @@ class Monitor extends BeanModel { pushToken: this.pushToken, notificationIDList, tags: tags, + maintenance: (maintenance.length !== 0), }; } @@ -136,6 +142,8 @@ 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]); + if (this.isUpsideDown()) { bean.status = flipStatus(bean.status); } @@ -148,7 +156,11 @@ class Monitor extends BeanModel { } try { - if (this.type === "http" || this.type === "keyword") { + if (maintenance.length !== 0) { + bean.msg = "Monitor under maintenance"; + bean.status = MAINTENANCE; + } + else if (this.type === "http" || this.type === "keyword") { // Do not do any queries/high loading things before the "bean.ping" let startTime = dayjs().valueOf(); @@ -387,8 +399,13 @@ class Monitor extends BeanModel { if (isImportant) { bean.important = true; - debug(`[${this.name}] sendNotification`); - await Monitor.sendNotification(isFirstBeat, this, bean); + if (Monitor.isImportantForNotification(isFirstBeat, previousBeat?.status, bean.status)) { + debug(`[${this.name}] sendNotification`); + await Monitor.sendNotification(isFirstBeat, this, bean); + } + else { + debug(`[${this.name}] will not sendNotification because it is (or was) under maintenance`); + } // Clear Status Page Cache debug(`[${this.name}] apicache clear`); @@ -405,6 +422,8 @@ class Monitor extends BeanModel { beatInterval = this.retryInterval; } console.warn(`Monitor #${this.id} '${this.name}': Pending: ${bean.msg} | Max retries: ${this.maxretries} | Retry: ${retries} | Retry Interval: ${beatInterval} seconds | Type: ${this.type}`); + } else if (bean.status === MAINTENANCE) { + console.warn(`Monitor #${this.id} '${this.name}': Under Maintenance | Type: ${this.type}`); } else { console.warn(`Monitor #${this.id} '${this.name}': Failing: ${bean.msg} | Interval: ${beatInterval} seconds | Type: ${this.type}`); } @@ -598,7 +617,7 @@ class Monitor extends BeanModel { -- SUM all uptime duration, also trim off the beat out of time window SUM( CASE - WHEN (status = 1) + WHEN (status = 1 OR status = 3) THEN CASE WHEN (JULIANDAY(\`time\`) - JULIANDAY(?)) * 86400 < duration @@ -659,11 +678,42 @@ class Monitor extends BeanModel { // DOWN -> PENDING = this case not exists // DOWN -> DOWN = not important // * DOWN -> UP = important - let isImportant = isFirstBeat || + // MAINTENANCE -> MAINTENANCE = not important + // * MAINTENANCE -> UP = important + // * MAINTENANCE -> DOWN = important + // * DOWN -> MAINTENANCE = important + // * UP -> MAINTENANCE = important + return isFirstBeat || + (previousBeatStatus === DOWN && currentBeatStatus === MAINTENANCE) || + (previousBeatStatus === UP && currentBeatStatus === MAINTENANCE) || + (previousBeatStatus === MAINTENANCE && currentBeatStatus === DOWN) || + (previousBeatStatus === MAINTENANCE && currentBeatStatus === UP) || + (previousBeatStatus === UP && currentBeatStatus === DOWN) || + (previousBeatStatus === DOWN && currentBeatStatus === UP) || + (previousBeatStatus === PENDING && currentBeatStatus === DOWN); + } + + static isImportantForNotification(isFirstBeat, previousBeatStatus, currentBeatStatus) { + // * ? -> ANY STATUS = important [isFirstBeat] + // UP -> PENDING = not important + // * UP -> DOWN = important + // UP -> UP = not important + // PENDING -> PENDING = not important + // * PENDING -> DOWN = important + // PENDING -> UP = not important + // DOWN -> PENDING = this case not exists + // DOWN -> DOWN = not important + // * DOWN -> UP = important + // MAINTENANCE -> MAINTENANCE = not important + // MAINTENANCE -> UP = not important + // * MAINTENANCE -> DOWN = important + // DOWN -> MAINTENANCE = not important + // UP -> MAINTENANCE = not important + return isFirstBeat || + (previousBeatStatus === MAINTENANCE && currentBeatStatus === DOWN) || (previousBeatStatus === UP && currentBeatStatus === DOWN) || (previousBeatStatus === DOWN && currentBeatStatus === UP) || (previousBeatStatus === PENDING && currentBeatStatus === DOWN); - return isImportant; } static async sendNotification(isFirstBeat, monitor, bean) { diff --git a/server/routers/api-router.js b/server/routers/api-router.js index 1920cef71..19e4fcad3 100644 --- a/server/routers/api-router.js +++ b/server/routers/api-router.js @@ -5,7 +5,7 @@ const server = require("../server"); const apicache = require("../modules/apicache"); const Monitor = require("../model/monitor"); const dayjs = require("dayjs"); -const { UP, flipStatus, debug } = require("../../src/util"); +const { UP, MAINTENANCE, flipStatus, debug} = require("../../src/util"); let router = express.Router(); let cache = apicache.middleware; @@ -51,6 +51,12 @@ 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]); + if (maintenance.length !== 0) { + msg = "Monitor under maintenance"; + status = MAINTENANCE; + } + debug("PreviousStatus: " + previousStatus); debug("Current Status: " + status); @@ -70,7 +76,7 @@ router.get("/api/push/:pushToken", async (request, response) => { ok: true, }); - if (bean.important) { + if (Monitor.isImportantForNotification(isFirstBeat, previousStatus, status)) { await Monitor.sendNotification(isFirstBeat, monitor, bean); } @@ -131,6 +137,34 @@ router.get("/api/status-page/incident", async (_, response) => { } }); +// Status Page - Maintenance List +// Can fetch only if published +router.get("/api/status-page/maintenance-list", async (_request, response) => { + allowDevAllOrigin(response); + + try { + await checkPublished(); + const publicMaintenanceList = []; + + 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') + ORDER BY maintenance.end_date + `)); + + for (const bean of maintenanceBeanList) { + publicMaintenanceList.push(await bean.toPublicJSON()); + } + + response.json(publicMaintenanceList); + + } catch (error) { + send403(response, error.message); + } +}); + // Status Page - Monitor List // Can fetch only if published router.get("/api/status-page/monitor-list", cache("5 minutes"), async (_request, response) => { diff --git a/server/server.js b/server/server.js index 153cac4fd..2b6933d71 100644 --- a/server/server.js +++ b/server/server.js @@ -132,6 +132,7 @@ const { sendNotificationList, sendHeartbeatList, sendImportantHeartbeatList, sen const { statusPageSocketHandler } = require("./socket-handlers/status-page-socket-handler"); const databaseSocketHandler = require("./socket-handlers/database-socket-handler"); const TwoFA = require("./2fa"); +const apicache = require("./modules/apicache"); app.use(express.json()); @@ -162,6 +163,12 @@ let jwtSecret = null; */ let monitorList = {}; +/** +* Main maintenance list +* @type {{}} +*/ +let maintenanceList = {}; + /** * Show Setup Page * @type {boolean} @@ -625,6 +632,101 @@ exports.entryPage = "dashboard"; } }); + // Add a new maintenance + socket.on("addMaintenance", async (maintenance, callback) => { + try { + checkLogin(socket); + let bean = R.dispense("maintenance"); + + bean.import(maintenance); + bean.user_id = socket.userID; + let maintenanceID = await R.store(bean); + + await sendMaintenanceList(socket); + + callback({ + ok: true, + msg: "Added Successfully.", + maintenanceID, + }); + + } catch (e) { + callback({ + ok: false, + msg: e.message, + }); + } + }); + + // Edit a maintenance + socket.on("editMaintenance", async (maintenance, callback) => { + try { + checkLogin(socket); + + let bean = await R.findOne("maintenance", " id = ? ", [ maintenance.id ]); + + if (bean.user_id !== socket.userID) { + throw new Error("Permission denied."); + } + + bean.title = maintenance.title; + bean.description = maintenance.description; + bean.start_date = maintenance.start_date; + bean.end_date = maintenance.end_date; + + await R.store(bean); + + await sendMaintenanceList(socket); + + callback({ + ok: true, + msg: "Saved.", + maintenanceID: bean.id, + }); + + } catch (e) { + console.error(e); + callback({ + ok: false, + msg: e.message, + }); + } + }); + + // Add a new monitor_maintenance + socket.on("addMonitorMaintenance", async (maintenanceID, monitors, callback) => { + try { + checkLogin(socket); + + await R.exec("DELETE FROM monitor_maintenance WHERE maintenance_id = ?", [ + maintenanceID + ]); + + for await (const monitor of monitors) { + let bean = R.dispense("monitor_maintenance"); + + bean.import({ + monitor_id: monitor.id, + maintenance_id: maintenanceID + }); + await R.store(bean); + } + + apicache.clear(); + + callback({ + ok: true, + msg: "Added Successfully.", + }); + + } catch (e) { + callback({ + ok: false, + msg: e.message, + }); + } + }); + socket.on("getMonitorList", async (callback) => { try { checkLogin(socket); @@ -641,6 +743,22 @@ exports.entryPage = "dashboard"; } }); + socket.on("getMaintenanceList", async (callback) => { + try { + checkLogin(socket); + await sendMaintenanceList(socket); + callback({ + ok: true, + }); + } catch (e) { + console.error(e); + callback({ + ok: false, + msg: e.message, + }); + } + }); + socket.on("getMonitor", async (monitorID, callback) => { try { checkLogin(socket); @@ -665,6 +783,54 @@ exports.entryPage = "dashboard"; } }); + socket.on("getMaintenance", async (maintenanceID, callback) => { + try { + checkLogin(socket); + + console.log(`Get Maintenance: ${maintenanceID} User ID: ${socket.userID}`); + + let bean = await R.findOne("maintenance", " id = ? AND user_id = ? ", [ + maintenanceID, + socket.userID, + ]); + + callback({ + ok: true, + maintenance: await bean.toJSON(), + }); + + } catch (e) { + callback({ + ok: false, + msg: e.message, + }); + } + }); + + socket.on("getMonitorMaintenance", async (maintenanceID, callback) => { + try { + checkLogin(socket); + + console.log(`Get Monitors for Maintenance: ${maintenanceID} User ID: ${socket.userID}`); + + let monitors = await R.getAll("SELECT monitor.id, monitor.name FROM monitor_maintenance mm JOIN monitor ON mm.monitor_id = monitor.id WHERE mm.maintenance_id = ? ", [ + maintenanceID, + ]); + + callback({ + ok: true, + monitors, + }); + + } catch (e) { + console.error(e); + callback({ + ok: false, + msg: e.message, + }); + } + }); + socket.on("getMonitorBeats", async (monitorID, period, callback) => { try { checkLogin(socket); @@ -769,6 +935,36 @@ exports.entryPage = "dashboard"; } }); + socket.on("deleteMaintenance", async (maintenanceID, callback) => { + try { + checkLogin(socket); + + console.log(`Delete Maintenance: ${maintenanceID} User ID: ${socket.userID}`); + + if (maintenanceID in maintenanceList) { + delete maintenanceList[maintenanceID]; + } + + await R.exec("DELETE FROM maintenance WHERE id = ? AND user_id = ? ", [ + maintenanceID, + socket.userID, + ]); + + callback({ + ok: true, + msg: "Deleted Successfully.", + }); + + await sendMaintenanceList(socket); + + } catch (e) { + callback({ + ok: false, + msg: e.message, + }); + } + }); + socket.on("getTags", async (callback) => { try { checkLogin(socket); @@ -1394,11 +1590,18 @@ async function sendMonitorList(socket) { return list; } +async function sendMaintenanceList(socket) { + let list = await getMaintenanceJSONList(socket.userID); + io.to(socket.userID).emit("maintenanceList", list); + return list; +} + async function afterLogin(socket, user) { socket.userID = user.id; socket.join(user.id); let monitorList = await sendMonitorList(socket); + sendMaintenanceList(socket); sendNotificationList(socket); await sleep(500); @@ -1430,6 +1633,20 @@ async function getMonitorJSONList(userID) { return result; } +async function getMaintenanceJSONList(userID) { + let result = {}; + + let maintenanceList = await R.find("maintenance", " user_id = ? ORDER BY end_date DESC, title", [ + userID, + ]); + + for (let maintenance of maintenanceList) { + result[maintenance.id] = await maintenance.toJSON(); + } + + return result; +} + async function initDatabase(testMode = false) { if (! fs.existsSync(Database.path)) { console.log("Copying Database"); diff --git a/src/assets/app.scss b/src/assets/app.scss index cec644676..73b9d6311 100644 --- a/src/assets/app.scss +++ b/src/assets/app.scss @@ -273,6 +273,7 @@ textarea.form-control { &.bg-info, &.bg-warning, &.bg-danger, + &.bg-maintenance, &.bg-light { color: $dark-font-color2; } diff --git a/src/assets/vars.scss b/src/assets/vars.scss index 91ab917e5..e48a6efb6 100644 --- a/src/assets/vars.scss +++ b/src/assets/vars.scss @@ -1,6 +1,7 @@ $primary: #5cdd8b; $danger: #dc3545; $warning: #f8a306; +$maintenance: #1747f5; $link-color: #111; $border-radius: 50rem; diff --git a/src/components/HeartbeatBar.vue b/src/components/HeartbeatBar.vue index be0b122ed..abeed7cbe 100644 --- a/src/components/HeartbeatBar.vue +++ b/src/components/HeartbeatBar.vue @@ -5,7 +5,7 @@ v-for="(beat, index) in shortBeatList" :key="index" class="beat" - :class="{ 'empty' : (beat === 0), 'down' : (beat.status === 0), 'pending' : (beat.status === 2) }" + :class="{ 'empty' : (beat === 0), 'down' : (beat.status === 0), 'pending' : (beat.status === 2), 'maintenance' : (beat.status === 3) }" :style="beatStyle" :title="getBeatTitle(beat)" /> @@ -200,6 +200,10 @@ export default { background-color: $warning; } + &.maintenance { + background-color: $maintenance; + } + &:not(.empty):hover { transition: all ease-in-out 0.15s; opacity: 0.8; diff --git a/src/components/MonitorList.vue b/src/components/MonitorList.vue index ef51e89cd..d943efff3 100644 --- a/src/components/MonitorList.vue +++ b/src/components/MonitorList.vue @@ -1,7 +1,12 @@ @@ -282,7 +298,6 @@ export default { margin-top: 5px; } - .bg-maintenance { background-color: $maintenance; } diff --git a/src/components/Uptime.vue b/src/components/Uptime.vue index 226dfae32..d663db88a 100644 --- a/src/components/Uptime.vue +++ b/src/components/Uptime.vue @@ -15,7 +15,7 @@ export default { computed: { uptime() { - + if (this.type === "maintenance") { return this.$t("Maintenance"); } @@ -31,9 +31,9 @@ export default { color() { if (this.type === "maintenance" || this.monitor.maintenance) { - return "maintenance" + return "maintenance"; } - + if (this.lastHeartBeat.status === 0) { return "danger"; } diff --git a/src/mixins/datetime.js b/src/mixins/datetime.js index 3f4749af6..c64615624 100644 --- a/src/mixins/datetime.js +++ b/src/mixins/datetime.js @@ -34,10 +34,11 @@ export default { const inputDate = new Date(value); const now = new Date(Date.now()); - if (inputDate.getFullYear() === now.getUTCFullYear() && inputDate.getMonth() === now.getUTCMonth() && inputDate.getDay() === now.getUTCDay()) + if (inputDate.getFullYear() === now.getUTCFullYear() && inputDate.getMonth() === now.getUTCMonth() && inputDate.getDay() === now.getUTCDay()) { return this.datetimeFormat(value, "HH:mm"); - else + } else { return this.datetimeFormat(value, "YYYY-MM-DD HH:mm"); + } }, date(value) { diff --git a/src/mixins/socket.js b/src/mixins/socket.js index 8d419706d..6d4311b1f 100644 --- a/src/mixins/socket.js +++ b/src/mixins/socket.js @@ -464,8 +464,7 @@ export default { text: this.$t("Maintenance"), color: "maintenance", }; - } - else if (! lastHeartBeat) { + } else if (! lastHeartBeat) { result[monitorID] = unknown; } else if (lastHeartBeat.status === 1) { result[monitorID] = { @@ -505,8 +504,7 @@ export default { if (monitor && monitor.maintenance) { result.maintenance++; - } - else if (monitor && ! monitor.active) { + } else if (monitor && ! monitor.active) { result.pause++; } else if (beat) { if (beat.status === 1) { diff --git a/src/pages/Dashboard.vue b/src/pages/Dashboard.vue index e99422a41..207e5b3a1 100644 --- a/src/pages/Dashboard.vue +++ b/src/pages/Dashboard.vue @@ -8,12 +8,12 @@