From 15e49c1b451e84a515dfae45dda13915b480d578 Mon Sep 17 00:00:00 2001 From: Simon Larsen Date: Fri, 10 Jan 2025 14:40:42 +0000 Subject: [PATCH] feat: implement overall monitor status calculation and update Overview component --- Common/Server/API/StatusPageAPI.ts | 60 +++++++++++++++++++- StatusPage/src/Pages/Overview/Overview.tsx | 66 ++-------------------- 2 files changed, 65 insertions(+), 61 deletions(-) diff --git a/Common/Server/API/StatusPageAPI.ts b/Common/Server/API/StatusPageAPI.ts index a248d8d0fcb..59458471a23 100644 --- a/Common/Server/API/StatusPageAPI.ts +++ b/Common/Server/API/StatusPageAPI.ts @@ -725,7 +725,7 @@ export default class StatusPageAPI extends BaseAPI< statusPageResources.find((resource: StatusPageResource) => { return ( resource.monitorGroupId?.toString() === - monitorGroupId.toString() && + monitorGroupId.toString() && (resource.showStatusHistoryChart || resource.showUptimePercent) ); @@ -1093,7 +1093,13 @@ export default class StatusPageAPI extends BaseAPI< }, }); + + const overallStatus: MonitorStatus | null = this.getOverallMonitorStatus(statusPageResources, monitorStatuses, monitorGroupCurrentStatuses); + const response: JSONObject = { + + overallStatus: overallStatus ? BaseModel.toJSON(overallStatus, MonitorStatus) : null, + scheduledMaintenanceEventsPublicNotes: BaseModel.toJSONArray( scheduledMaintenanceEventsPublicNotes, ScheduledMaintenancePublicNote, @@ -2284,4 +2290,56 @@ export default class StatusPageAPI extends BaseAPI< return response; } + + + public getOverallMonitorStatus( + statusPageResources: Array, + monitorStatuses: Array, + monitorGroupCurrentStatuses: Dictionary, + ): MonitorStatus | null { + let currentStatus: MonitorStatus | null = + monitorStatuses.length > 0 && monitorStatuses[0] + ? monitorStatuses[0] + : null; + + const dict: Dictionary = {}; + + for (const resource of statusPageResources) { + if (resource.monitor?.currentMonitorStatusId) { + if ( + !Object.keys(dict).includes( + resource.monitor?.currentMonitorStatusId.toString() || "", + ) + ) { + dict[resource.monitor?.currentMonitorStatusId?.toString()] = 1; + } else { + dict[resource.monitor!.currentMonitorStatusId!.toString()]!++; + } + } + } + + // check status of monitor groups. + + for (const groupId in monitorGroupCurrentStatuses) { + const statusId: ObjectID | undefined = + monitorGroupCurrentStatuses[groupId]; + + if (statusId) { + if (!Object.keys(dict).includes(statusId.toString() || "")) { + dict[statusId.toString()] = 1; + } else { + dict[statusId.toString()]!++; + } + } + } + + for (const monitorStatus of monitorStatuses) { + if (monitorStatus._id && dict[monitorStatus._id]) { + currentStatus = monitorStatus; + } + } + + return currentStatus; + } + } diff --git a/StatusPage/src/Pages/Overview/Overview.tsx b/StatusPage/src/Pages/Overview/Overview.tsx index 3f7e97a1469..83fdd84967c 100644 --- a/StatusPage/src/Pages/Overview/Overview.tsx +++ b/StatusPage/src/Pages/Overview/Overview.tsx @@ -256,13 +256,14 @@ const Overview: FunctionComponent = ( setIncidentStateTimelines(incidentStateTimelines); setScheduledMaintenanceStateTimelines(scheduledMaintenanceStateTimelines); + const overallStatus: MonitorStatus | null = data["overallStatus"] ? BaseModel.fromJSONObject( + (data["overallStatus"] as JSONObject) || {}, + MonitorStatus, + ) : null; + // Parse Data. setCurrentStatus( - getOverallMonitorStatus( - statusPageResources, - monitorStatuses, - monitorGroupCurrentStatuses, - ), + overallStatus ); setIsLoading(false); @@ -365,61 +366,6 @@ const Overview: FunctionComponent = ( return <>; }; - type GetOverallMonitorStatusFunction = ( - statusPageResources: Array, - monitorStatuses: Array, - monitorGroupCurrentStatuses: Dictionary, - ) => MonitorStatus | null; - - const getOverallMonitorStatus: GetOverallMonitorStatusFunction = ( - statusPageResources: Array, - monitorStatuses: Array, - monitorGroupCurrentStatuses: Dictionary, - ): MonitorStatus | null => { - let currentStatus: MonitorStatus | null = - monitorStatuses.length > 0 && monitorStatuses[0] - ? monitorStatuses[0] - : null; - - const dict: Dictionary = {}; - - for (const resource of statusPageResources) { - if (resource.monitor?.currentMonitorStatusId) { - if ( - !Object.keys(dict).includes( - resource.monitor?.currentMonitorStatusId.toString() || "", - ) - ) { - dict[resource.monitor?.currentMonitorStatusId?.toString()] = 1; - } else { - dict[resource.monitor!.currentMonitorStatusId!.toString()]!++; - } - } - } - - // check status of monitor groups. - - for (const groupId in monitorGroupCurrentStatuses) { - const statusId: ObjectID | undefined = - monitorGroupCurrentStatuses[groupId]; - - if (statusId) { - if (!Object.keys(dict).includes(statusId.toString() || "")) { - dict[statusId.toString()] = 1; - } else { - dict[statusId.toString()]!++; - } - } - } - - for (const monitorStatus of monitorStatuses) { - if (monitorStatus._id && dict[monitorStatus._id]) { - currentStatus = monitorStatus; - } - } - - return currentStatus; - }; if (isLoading) { return ;