Rewrite team monitors query

This commit is contained in:
Alex Holliday committed 2025-01-08 13:44:24 -08:00
1 parent 87f9c35c74
commit 979110dd77
1 file changed
+144 -185
+144 -185
View File
@@ -505,202 +505,162 @@ const getMonitorById = async (monitorId) => {
} }
}; };
/** const getMonitorsByTeamId = async (req) => {
* Get monitors and Summary by TeamID let { limit, type, page, rowsPerPage, filter, field, order } = req.query;
* @async
* @param {Express.Request} req
* @param {Express.Response} res
* @returns {Promise<Array<Monitor>>}
* @throws {Error}
*/
const getMonitorsSummaryByTeamId = async (teamId, type) => { // Parse ints
try { limit = parseInt(limit);
const monitorCounts = await Monitor.aggregate([ page = parseInt(page);
{ rowsPerPage = parseInt(rowsPerPage);
$match: {
type: { $in: type }, // Build the match stage
}, const matchStage = { teamId: ObjectId.createFromHexString(req.params.teamId) };
}, if (type !== undefined) {
{ matchStage.type = Array.isArray(type) ? { $in: type } : type;
$facet: {
total: [{ $count: "count" }],
up: [{ $match: { status: true } }, { $count: "count" }],
down: [{ $match: { status: false } }, { $count: "count" }],
paused: [{ $match: { isActive: false } }, { $count: "count" }],
},
},
{
$project: {
total: { $arrayElemAt: ["$total.count", 0] },
up: { $arrayElemAt: ["$up.count", 0] },
down: { $arrayElemAt: ["$down.count", 0] },
paused: { $arrayElemAt: ["$paused.count", 0] },
},
},
]);
return { monitorCounts: monitorCounts[0] };
} catch (error) {
error.service = SERVICE_NAME;
error.method = "getMonitorsAndSummaryByTeamId";
throw error;
} }
};
/** const skip = page && rowsPerPage ? page * rowsPerPage : 0;
* Get monitors by TeamID
* @async
* @param {Express.Request} req
* @param {Express.Response} res
* @returns {Promise<Array<Monitor>>}
* @throws {Error}
*/
const getMonitorsByTeamId = async (req, res) => {
try {
let {
limit,
type,
status,
checkOrder,
normalize,
page,
rowsPerPage,
filter,
field,
order,
} = req.query;
const monitorQuery = { teamId: req.params.teamId }; const sort = { [field]: order === "asc" ? 1 : -1 };
const monitorCount = await Monitor.countDocuments(monitorQuery);
if (type !== undefined) { if (filter !== undefined) {
monitorQuery.type = Array.isArray(type) ? { $in: type } : type; matchStage.$or = [
} { name: { $regex: filter, $options: "i" } },
// Add filter if provided { url: { $regex: filter, $options: "i" } },
// $options: "i" makes the search case-insensitive ];
if (filter !== undefined) { }
monitorQuery.$or = [
{ name: { $regex: filter, $options: "i" } },
{ url: { $regex: filter, $options: "i" } },
];
}
// Pagination const results = await Monitor.aggregate([
const skip = page && rowsPerPage ? page * rowsPerPage : 0; { $match: matchStage },
{
// Build Sort option $facet: {
const sort = { [field]: order === "asc" ? 1 : -1 }; summary: [
{
const matchStage = { teamId: new ObjectId(req.params.teamId) }; $group: {
if (type !== undefined) { _id: null,
matchStage.type = Array.isArray(type) ? { $in: type } : type; totalMonitors: { $sum: 1 },
} upMonitors: {
if (filter !== undefined) { $sum: {
matchStage.$or = [ $cond: [{ $eq: ["$status", true] }, 1, 0],
{ name: { $regex: filter, $options: "i" } },
{ url: { $regex: filter, $options: "i" } },
];
}
let result = await Monitor.aggregate([
{ $match: matchStage },
{ $skip: parseInt(skip) },
...(rowsPerPage ? [{ $limit: parseInt(rowsPerPage) }] : []),
{ $sort: sort },
{
$lookup: {
from: "checks",
let: { monitorId: "$_id" },
pipeline: [
{
$match: {
$expr: { $eq: ["$monitorId", "$$monitorId"] },
...(status && { status }),
},
},
{ $sort: { createdAt: checkOrder === "asc" ? 1 : -1 } },
{ $limit: parseInt(limit) || 0 },
],
as: "standardchecks",
},
},
{
$lookup: {
from: "pagespeedchecks",
let: { monitorId: "$_id" },
pipeline: [
{
$match: {
$expr: { $eq: ["$monitorId", "$$monitorId"] },
...(status && { status }),
},
},
{ $sort: { createdAt: checkOrder === "asc" ? 1 : -1 } },
{ $limit: parseInt(limit) || 0 },
],
as: "pagespeedchecks",
},
},
{
$lookup: {
from: "hardwarechecks",
let: { monitorId: "$_id" },
pipeline: [
{
$match: {
$expr: { $eq: ["$monitorId", "$$monitorId"] },
...(status && { status }),
},
},
{ $sort: { createdAt: checkOrder === "asc" ? 1 : -1 } },
{ $limit: parseInt(limit) || 0 },
],
as: "hardwarechecks",
},
},
{
$addFields: {
checks: {
$switch: {
branches: [
{
case: { $in: ["$type", ["http", "ping", "docker", "port"]] },
then: "$standardchecks",
}, },
{ },
case: { $eq: ["$type", "pagespeed"] }, downMonitors: {
then: "$pagespeedchecks", $sum: {
$cond: [{ $eq: ["$status", false] }, 1, 0],
}, },
{ },
case: { $eq: ["$type", "hardware"] }, pausedMonitors: {
then: "$hardwarechecks", $sum: {
$cond: [{ $eq: ["$isActive", false] }, 1, 0],
}, },
], },
default: [],
}, },
}, },
}, {
$project: {
_id: 0,
},
},
],
monitors: [
{ $sort: sort },
{ $skip: skip },
...(rowsPerPage ? [{ $limit: rowsPerPage }] : []),
{
$lookup: {
from: "checks",
let: { monitorId: "$_id" },
pipeline: [
{
$match: {
$expr: { $eq: ["$monitorId", "$$monitorId"] },
},
},
{ $sort: { createdAt: -1 } },
...(limit ? [{ $limit: limit }] : []),
],
as: "standardchecks",
},
},
{
$lookup: {
from: "pagespeedchecks",
let: { monitorId: "$_id" },
pipeline: [
{
$match: {
$expr: { $eq: ["$monitorId", "$$monitorId"] },
},
},
{ $sort: { createdAt: -1 } },
...(limit ? [{ $limit: limit }] : []),
],
as: "pagespeedchecks",
},
},
{
$lookup: {
from: "hardwarechecks",
let: { monitorId: "$_id" },
pipeline: [
{
$match: {
$expr: { $eq: ["$monitorId", "$$monitorId"] },
},
},
{ $sort: { createdAt: -1 } },
...(limit ? [{ $limit: limit }] : []),
],
as: "hardwarechecks",
},
},
{
$addFields: {
checks: {
$switch: {
branches: [
{
case: { $in: ["$type", ["http", "ping", "docker", "port"]] },
then: "$standardchecks",
},
{
case: { $eq: ["$type", "pagespeed"] },
then: "$pagespeedchecks",
},
{
case: { $eq: ["$type", "hardware"] },
then: "$hardwarechecks",
},
],
default: [],
},
},
},
},
{
$project: {
standardchecks: 0,
pagespeedchecks: 0,
hardwarechecks: 0,
},
},
],
}, },
{ },
$project: { {
standardchecks: 0, $project: {
pagespeedchecks: 0, summary: { $arrayElemAt: ["$summary", 0] },
hardwarechecks: 0, monitors: 1,
},
}, },
]); },
if (normalize) { ]);
result = result.map((monitor) => {
monitor.checks = NormalizeData(monitor.checks, 10, 100); let { monitors, summary } = results[0];
return monitor; monitors = monitors.map((monitor) => {
}); monitor.checks = NormalizeData(monitor.checks, 10, 100);
} return monitor;
return { monitors: result, monitorCount }; });
} catch (error) { return { monitors, summary };
error.service = SERVICE_NAME;
error.method = "getMonitorsByTeamId";
throw error;
}
}; };
/** /**
@@ -832,9 +792,8 @@ export {
getAllMonitorsWithUptimeStats, getAllMonitorsWithUptimeStats,
getMonitorStatsById, getMonitorStatsById,
getMonitorById, getMonitorById,
getUptimeDetailsById,
getMonitorsSummaryByTeamId,
getMonitorsByTeamId, getMonitorsByTeamId,
getUptimeDetailsById,
createMonitor, createMonitor,
deleteMonitor, deleteMonitor,
deleteAllMonitors, deleteAllMonitors,