From aef0a6620586c5b65cd5098b456ae21eb64ef560 Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Fri, 18 Mar 2022 17:56:46 +0800 Subject: [PATCH] [Status Page] Simplify show tags logic --- server/model/group.js | 4 +-- server/model/monitor.js | 18 ++++++++--- server/routers/api-router.js | 22 ++----------- src/components/PublicGroupList.vue | 5 ++- src/pages/StatusPage.vue | 50 +++++++++--------------------- 5 files changed, 36 insertions(+), 63 deletions(-) diff --git a/server/model/group.js b/server/model/group.js index 567f3865b..3e1f2d449 100644 --- a/server/model/group.js +++ b/server/model/group.js @@ -3,12 +3,12 @@ const { R } = require("redbean-node"); class Group extends BeanModel { - async toPublicJSON() { + async toPublicJSON(showTags = false) { let monitorBeanList = await this.getMonitorList(); let monitorList = []; for (let bean of monitorBeanList) { - monitorList.push(await bean.toPublicJSON()); + monitorList.push(await bean.toPublicJSON(showTags)); } return { diff --git a/server/model/monitor.js b/server/model/monitor.js index b4a805980..bafc0d2dd 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -24,18 +24,22 @@ const apicache = require("../modules/apicache"); class Monitor extends BeanModel { /** - * Return a object that ready to parse to JSON for public + * Return an object that ready to parse to JSON for public * Only show necessary data to public */ - async toPublicJSON() { - return { + async toPublicJSON(showTags = false) { + let obj = { id: this.id, name: this.name, }; + if (showTags) { + obj.tags = await this.getTags(); + } + return obj; } /** - * Return a object that ready to parse to JSON + * Return an object that ready to parse to JSON */ async toJSON() { @@ -49,7 +53,7 @@ class Monitor extends BeanModel { notificationIDList[bean.notification_id] = true; } - 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 tags = await this.getTags(); return { id: this.id, @@ -82,6 +86,10 @@ class Monitor extends BeanModel { }; } + async getTags() { + return 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]); + } + /** * Encode user and password to Base64 encoding * for HTTP "basic" auth, as per RFC-7617 diff --git a/server/routers/api-router.js b/server/routers/api-router.js index e9076e990..ad8870847 100644 --- a/server/routers/api-router.js +++ b/server/routers/api-router.js @@ -113,30 +113,14 @@ router.get("/api/status-page/:slug", cache("5 minutes"), async (request, respons // Public Group List const publicGroupList = []; - const tagsVisible = !!statusPage.show_tags; + const showTags = !!statusPage.show_tags; + debug("Show Tags???" + showTags); const list = await R.find("group", " public = 1 AND status_page_id = ? ORDER BY weight ", [ statusPage.id ]); for (let groupBean of list) { - let monitorGroup = await groupBean.toPublicJSON(); - if (tagsVisible) { - monitorGroup.monitorList = await Promise.all(monitorGroup.monitorList.map(async (monitor) => { - // Includes tags as an array in response, allows for tags to be displayed on public status page - const tags = await R.getAll( - `SELECT monitor_tag.monitor_id, monitor_tag.value, tag.name, tag.color - FROM monitor_tag - JOIN tag - ON monitor_tag.tag_id = tag.id - WHERE monitor_tag.monitor_id = ?`, [monitor.id] - ); - return { - ...monitor, - tags: tags - }; - })); - } - + let monitorGroup = await groupBean.toPublicJSON(showTags); publicGroupList.push(monitorGroup); } diff --git a/src/components/PublicGroupList.vue b/src/components/PublicGroupList.vue index f30edcef5..37440e310 100644 --- a/src/components/PublicGroupList.vue +++ b/src/components/PublicGroupList.vue @@ -41,7 +41,7 @@ {{ monitor.element.name }} -
+
@@ -76,6 +76,9 @@ export default { type: Boolean, required: true, }, + showTags: { + type: Boolean, + } }, data() { return { diff --git a/src/pages/StatusPage.vue b/src/pages/StatusPage.vue index 3c7e3e956..abd5ca1a1 100644 --- a/src/pages/StatusPage.vue +++ b/src/pages/StatusPage.vue @@ -223,7 +223,7 @@ 👀 {{ $t("statusPageNothing") }} - +