Add indeterminate state to monitors

This commit is contained in:
Alex Holliday committed 2024-09-02 10:45:07 -07:00
1 parent aa81e47fae
commit 071a7fc9cf
7 files changed
+57 -30

No files matched your search

+7 -1
View File
@@ -126,6 +126,7 @@ ColoredLabel.propTypes = {
*/ */
const StatusLabel = ({ status, text, customStyles }) => { const StatusLabel = ({ status, text, customStyles }) => {
console.log(status);
const theme = useTheme(); const theme = useTheme();
const colors = { const colors = {
up: { up: {
@@ -138,6 +139,11 @@ const StatusLabel = ({ status, text, customStyles }) => {
bgColor: theme.palette.error.bg, bgColor: theme.palette.error.bg,
borderColor: theme.palette.error.light, borderColor: theme.palette.error.light,
}, },
unknown: {
dotColor: theme.palette.unresolved.main,
bgColor: theme.palette.unresolved.bg,
borderColor: theme.palette.unresolved.light,
},
"cannot resolve": { "cannot resolve": {
dotColor: theme.palette.unresolved.main, dotColor: theme.palette.unresolved.main,
bgColor: theme.palette.unresolved.bg, bgColor: theme.palette.unresolved.bg,
@@ -170,7 +176,7 @@ const StatusLabel = ({ status, text, customStyles }) => {
}; };
StatusLabel.propTypes = { StatusLabel.propTypes = {
status: PropTypes.oneOf(["up", "down", "cannot resolve"]), status: PropTypes.oneOf(["up", "down", "unknown", "cannot resolve"]),
text: PropTypes.string, text: PropTypes.string,
customStyles: PropTypes.object, customStyles: PropTypes.object,
}; };
+2 -1
View File
@@ -53,7 +53,8 @@ Host.propTypes = {
params: PropTypes.shape({ params: PropTypes.shape({
title: PropTypes.string, title: PropTypes.string,
percentageColor: PropTypes.string, percentageColor: PropTypes.string,
percentage: PropTypes.number, percentage: PropTypes.string,
url: PropTypes.string,
}).isRequired, }).isRequired,
}; };
@@ -47,7 +47,12 @@ export const buildData = (monitors, isAdmin, navigate) => {
monitor.status === true monitor.status === true
? theme.palette.success.main ? theme.palette.success.main
: theme.palette.error.text, : theme.palette.error.text,
status: monitor.status === true ? "up" : "down", status:
monitor.status === undefined
? "unknown"
: monitor.status === true
? "up"
: "down",
}; };
// Reverse checks so latest check is on the right // Reverse checks so latest check is on the right
@@ -86,6 +91,3 @@ export const buildData = (monitors, isAdmin, navigate) => {
}); });
return data; return data;
}; };
+1
View File
@@ -359,6 +359,7 @@ const pauseMonitor = async (req, res, next) => {
await req.jobQueue.addJob(monitor._id, monitor); await req.jobQueue.addJob(monitor._id, monitor);
} }
monitor.isActive = !monitor.isActive; monitor.isActive = !monitor.isActive;
monitor.status = undefined;
monitor.save(); monitor.save();
return res.status(200).json({ return res.status(200).json({
success: true, success: true,
+1 -1
View File
@@ -24,7 +24,7 @@ const MonitorSchema = mongoose.Schema(
}, },
status: { status: {
type: Boolean, type: Boolean,
default: true, default: undefined,
}, },
type: { type: {
type: String, type: String,
-1
View File
@@ -238,7 +238,6 @@ class JobQueue {
console.log("Adding job", payload.url); console.log("Adding job", payload.url);
// Execute job immediately // Execute job immediately
await this.queue.add(jobName, payload); await this.queue.add(jobName, payload);
await this.queue.add(jobName, payload, { await this.queue.add(jobName, payload, {
repeat: { repeat: {
every: payload.interval, every: payload.interval,
+40 -22
View File
@@ -15,10 +15,31 @@ class NetworkService {
this.NETWORK_ERROR = 5000; this.NETWORK_ERROR = 5000;
} }
async handleNotification(job, isAlive) { async handleNotification(monitor, isAlive) {
let template =
isAlive === true ? "serverIsUpTemplate" : "serverIsDownTemplate";
let status = isAlive === true ? "up" : "down";
const notifications = await this.db.getNotificationsByMonitorId(
monitor._id
);
for (const notification of notifications) {
if (notification.type === "email") {
await this.emailService.buildAndSendEmail(
template,
{ monitorName: monitor.name, monitorUrl: monitor.url },
notification.address,
`Monitor ${monitor.name} is ${status}`
);
}
}
}
async handleStatusUpdate(job, isAlive) {
try { try {
const { _id } = job.data; const { _id } = job.data;
const monitor = await this.db.getMonitorById(_id); const monitor = await this.db.getMonitorById(_id);
if (monitor === null || monitor === undefined) { if (monitor === null || monitor === undefined) {
logger.error(`Null Monitor: ${_id}`, { logger.error(`Null Monitor: ${_id}`, {
method: "handleNotification", method: "handleNotification",
@@ -28,25 +49,22 @@ class NetworkService {
return; return;
} }
// If monitor status changes, update monitor status and send notification // Two general cases:
if (monitor.status !== isAlive) { // 1. Monitor status is undefined
monitor.status = !monitor.status; // 2. Monitor status is defined
// If the monitor status is undefined, set it's status to isAlive. if isAlive is false, send notification
if (monitor.status === undefined) {
monitor.status = isAlive;
await monitor.save(); await monitor.save();
if (isAlive === false) {
let template = this.handleNotification(monitor, isAlive);
isAlive === true ? "serverIsUpTemplate" : "serverIsDownTemplate"; }
let status = isAlive === true ? "up" : "down"; } else {
if (monitor.status !== isAlive) {
const notifications = await this.db.getNotificationsByMonitorId(_id); monitor.status = !monitor.status;
for (const notification of notifications) { await monitor.save();
if (notification.type === "email") { this.handleNotification(monitor, isAlive);
await this.emailService.buildAndSendEmail(
template,
{ monitorName: monitor.name, monitorUrl: monitor.url },
notification.address,
`Monitor ${monitor.name} is ${status}`
);
}
} }
} }
} catch (error) { } catch (error) {
@@ -113,7 +131,7 @@ class NetworkService {
}; };
return await this.logAndStoreCheck(checkData, this.db.createCheck); return await this.logAndStoreCheck(checkData, this.db.createCheck);
} finally { } finally {
this.handleNotification(job, isAlive); this.handleStatusUpdate(job, isAlive);
} }
} }
@@ -161,7 +179,7 @@ class NetworkService {
return await this.logAndStoreCheck(checkData, this.db.createCheck); return await this.logAndStoreCheck(checkData, this.db.createCheck);
} finally { } finally {
this.handleNotification(job, isAlive); this.handleStatusUpdate(job, isAlive);
} }
} }
@@ -241,7 +259,7 @@ class NetworkService {
}; };
this.logAndStoreCheck(checkData, this.db.createPageSpeedCheck); this.logAndStoreCheck(checkData, this.db.createPageSpeedCheck);
} finally { } finally {
this.handleNotification(job, isAlive); this.handleStatusUpdate(job, isAlive);
} }
} }