Skip to content

Commit

Permalink
feat: implement overall monitor status calculation and update Overvie…
Browse files Browse the repository at this point in the history
…w component
  • Loading branch information
simlarsen committed Jan 10, 2025
1 parent b71a657 commit 15e49c1
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 61 deletions.
60 changes: 59 additions & 1 deletion Common/Server/API/StatusPageAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -2284,4 +2290,56 @@ export default class StatusPageAPI extends BaseAPI<

return response;
}


public getOverallMonitorStatus(
statusPageResources: Array<StatusPageResource>,
monitorStatuses: Array<MonitorStatus>,
monitorGroupCurrentStatuses: Dictionary<ObjectID>,
): MonitorStatus | null {
let currentStatus: MonitorStatus | null =
monitorStatuses.length > 0 && monitorStatuses[0]
? monitorStatuses[0]
: null;

const dict: Dictionary<number> = {};

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;
}

}
66 changes: 6 additions & 60 deletions StatusPage/src/Pages/Overview/Overview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -256,13 +256,14 @@ const Overview: FunctionComponent<PageComponentProps> = (
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);
Expand Down Expand Up @@ -365,61 +366,6 @@ const Overview: FunctionComponent<PageComponentProps> = (
return <></>;
};

type GetOverallMonitorStatusFunction = (
statusPageResources: Array<StatusPageResource>,
monitorStatuses: Array<MonitorStatus>,
monitorGroupCurrentStatuses: Dictionary<ObjectID>,
) => MonitorStatus | null;

const getOverallMonitorStatus: GetOverallMonitorStatusFunction = (
statusPageResources: Array<StatusPageResource>,
monitorStatuses: Array<MonitorStatus>,
monitorGroupCurrentStatuses: Dictionary<ObjectID>,
): MonitorStatus | null => {
let currentStatus: MonitorStatus | null =
monitorStatuses.length > 0 && monitorStatuses[0]
? monitorStatuses[0]
: null;

const dict: Dictionary<number> = {};

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 <PageLoader isVisible={true} />;
Expand Down

0 comments on commit 15e49c1

Please sign in to comment.