delete checks and status pages on monitor delete

This commit is contained in:
Alex Holliday committed 2025-02-12 13:52:21 -08:00
1 parent ca6e40042a
commit 508c838e58
2 files changed
+115 -79

No files matched your search

+42 -9
View File
@@ -295,12 +295,45 @@ class MonitorController {
try { try {
const monitor = await this.db.deleteMonitor(req, res, next); const monitor = await this.db.deleteMonitor(req, res, next);
// Delete associated checks,alerts,and notifications // Delete associated checks,alerts,and notifications
try { try {
await this.jobQueue.deleteJob(monitor); const operations = [
await this.db.deleteChecks(monitor._id); { name: "deleteJob", fn: () => this.jobQueue.deleteJob(monitor) },
await this.db.deletePageSpeedChecksByMonitorId(monitor._id); { name: "deleteChecks", fn: () => this.db.deleteChecks(monitor._id) },
await this.db.deleteNotificationsByMonitorId(monitor._id); {
await this.db.deleteHardwareChecksByMonitorId(monitor._id); name: "deletePageSpeedChecks",
fn: () => this.db.deletePageSpeedChecksByMonitorId(monitor._id),
},
{
name: "deleteNotifications",
fn: () => this.db.deleteNotificationsByMonitorId(monitor._id),
},
{
name: "deleteHardwareChecks",
fn: () => this.db.deleteHardwareChecksByMonitorId(monitor._id),
},
{
name: "deleteDistributedUptimeChecks",
fn: () => this.db.deleteDistributedChecksByMonitorId(monitor._id),
},
{
name: "deleteStatusPages",
fn: () => this.db.deleteStatusPagesByMonitorId(monitor._id),
},
];
const results = await Promise.allSettled(operations.map((op) => op.fn()));
results.forEach((result, index) => {
if (result.status === "rejected") {
const operationName = operations[index].name;
logger.error({
message: `Failed to ${operationName} for monitor ${monitor._id}`,
service: SERVICE_NAME,
method: "deleteMonitor",
stack: result.reason.stack,
});
}
});
} catch (error) { } catch (error) {
logger.error({ logger.error({
message: `Error deleting associated records for monitor ${monitor._id} with name ${monitor.name}`, message: `Error deleting associated records for monitor ${monitor._id} with name ${monitor.name}`,
@@ -390,10 +423,10 @@ class MonitorController {
await Promise.all( await Promise.all(
notifications && notifications &&
notifications.map(async (notification) => { notifications.map(async (notification) => {
notification.monitorId = editedMonitor._id; notification.monitorId = editedMonitor._id;
await this.db.createNotification(notification); await this.db.createNotification(notification);
}) })
); );
// Delete the old job(editedMonitor has the same ID as the old monitor) // Delete the old job(editedMonitor has the same ID as the old monitor)
+73 -70
View File
@@ -313,7 +313,7 @@ const calculateGroupStats = (group) => {
avgResponseTime: avgResponseTime:
checksWithResponseTime.length > 0 checksWithResponseTime.length > 0
? checksWithResponseTime.reduce((sum, check) => sum + check.responseTime, 0) / ? checksWithResponseTime.reduce((sum, check) => sum + check.responseTime, 0) /
checksWithResponseTime.length checksWithResponseTime.length
: 0, : 0,
}; };
}; };
@@ -372,7 +372,10 @@ const getUptimeDetailsById = async (req) => {
const getDistributedUptimeDetailsById = async (req) => { const getDistributedUptimeDetailsById = async (req) => {
try { try {
const { monitorId } = req.params; const { monitorId } = req?.params ?? {};
if (typeof monitorId === "undefined") {
throw new Error();
}
const monitor = await Monitor.findById(monitorId); const monitor = await Monitor.findById(monitorId);
if (monitor === null || monitor === undefined) { if (monitor === null || monitor === undefined) {
throw new Error(this.stringService.dbFindMonitorById(monitorId)); throw new Error(this.stringService.dbFindMonitorById(monitorId));
@@ -605,98 +608,98 @@ const getMonitorsByTeamId = async (req) => {
filteredMonitors: [ filteredMonitors: [
...(filter !== undefined ...(filter !== undefined
? [ ? [
{ {
$match: { $match: {
$or: [ $or: [
{ name: { $regex: filter, $options: "i" } }, { name: { $regex: filter, $options: "i" } },
{ url: { $regex: filter, $options: "i" } }, { url: { $regex: filter, $options: "i" } },
], ],
},
}, },
}, ]
]
: []), : []),
{ $sort: sort }, { $sort: sort },
{ $skip: skip }, { $skip: skip },
...(rowsPerPage ? [{ $limit: rowsPerPage }] : []), ...(rowsPerPage ? [{ $limit: rowsPerPage }] : []),
...(limit ...(limit
? [ ? [
{ {
$lookup: { $lookup: {
from: "checks", from: "checks",
let: { monitorId: "$_id" }, let: { monitorId: "$_id" },
pipeline: [ pipeline: [
{ {
$match: { $match: {
$expr: { $eq: ["$monitorId", "$$monitorId"] }, $expr: { $eq: ["$monitorId", "$$monitorId"] },
},
}, },
}, { $sort: { createdAt: -1 } },
{ $sort: { createdAt: -1 } }, ...(limit ? [{ $limit: limit }] : []),
...(limit ? [{ $limit: limit }] : []), ],
], as: "standardchecks",
as: "standardchecks", },
}, },
}, ]
]
: []), : []),
...(limit ...(limit
? [ ? [
{ {
$lookup: { $lookup: {
from: "pagespeedchecks", from: "pagespeedchecks",
let: { monitorId: "$_id" }, let: { monitorId: "$_id" },
pipeline: [ pipeline: [
{ {
$match: { $match: {
$expr: { $eq: ["$monitorId", "$$monitorId"] }, $expr: { $eq: ["$monitorId", "$$monitorId"] },
},
}, },
}, { $sort: { createdAt: -1 } },
{ $sort: { createdAt: -1 } }, ...(limit ? [{ $limit: limit }] : []),
...(limit ? [{ $limit: limit }] : []), ],
], as: "pagespeedchecks",
as: "pagespeedchecks", },
}, },
}, ]
]
: []), : []),
...(limit ...(limit
? [ ? [
{ {
$lookup: { $lookup: {
from: "hardwarechecks", from: "hardwarechecks",
let: { monitorId: "$_id" }, let: { monitorId: "$_id" },
pipeline: [ pipeline: [
{ {
$match: { $match: {
$expr: { $eq: ["$monitorId", "$$monitorId"] }, $expr: { $eq: ["$monitorId", "$$monitorId"] },
},
}, },
}, { $sort: { createdAt: -1 } },
{ $sort: { createdAt: -1 } }, ...(limit ? [{ $limit: limit }] : []),
...(limit ? [{ $limit: limit }] : []), ],
], as: "hardwarechecks",
as: "hardwarechecks", },
}, },
}, ]
]
: []), : []),
...(limit ...(limit
? [ ? [
{ {
$lookup: { $lookup: {
from: "distributeduptimechecks", from: "distributeduptimechecks",
let: { monitorId: "$_id" }, let: { monitorId: "$_id" },
pipeline: [ pipeline: [
{ {
$match: { $match: {
$expr: { $eq: ["$monitorId", "$$monitorId"] }, $expr: { $eq: ["$monitorId", "$$monitorId"] },
},
}, },
}, { $sort: { createdAt: -1 } },
{ $sort: { createdAt: -1 } }, ...(limit ? [{ $limit: limit }] : []),
...(limit ? [{ $limit: limit }] : []), ],
], as: "distributeduptimechecks",
as: "distributeduptimechecks", },
}, },
}, ]
]
: []), : []),
{ {