conditional sort, bounded end date

This commit is contained in:
Alex Holliday committed 2025-12-18 19:17:32 +00:00
1 parent aa346abf5b
commit 58f5896d15
1 file changed
+17 -9
+17 -9
View File
@@ -581,12 +581,13 @@ class MonitorService implements IMonitorService {
} }
// Route for raw checks (2h) // Route for raw checks (2h)
const endDate = new Date();
const matchStage: { const matchStage: {
"metadata.monitorId": mongoose.Types.ObjectId; "metadata.monitorId": mongoose.Types.ObjectId;
createdAt: { $gte: Date }; createdAt: { $gte: Date; $lt: Date };
} = { } = {
"metadata.monitorId": monitor._id, "metadata.monitorId": monitor._id,
createdAt: { $gte: startDate }, createdAt: { $gte: startDate, $lt: endDate },
}; };
const dateFormat = "%Y-%m-%dT%H:%M:00Z"; const dateFormat = "%Y-%m-%dT%H:%M:00Z";
@@ -615,12 +616,17 @@ class MonitorService implements IMonitorService {
finalProjection = { _id: 1, count: 1, avgResponseTime: 1 }; finalProjection = { _id: 1, count: 1, avgResponseTime: 1 };
} }
const checks = await Check.aggregate([ const pipeline: any[] = [
{ { $match: matchStage },
$match: matchStage,
},
{ $sort: { createdAt: 1 } },
{ $project: { ...projectStage, status: 1 } }, { $project: { ...projectStage, status: 1 } },
];
// For infrastructure monitors we rely on $last in group, which requires ordering by createdAt
if (monitor.type === "infrastructure") {
pipeline.push({ $sort: { createdAt: 1 } });
}
pipeline.push(
{ {
$group: { $group: {
...groupClause, ...groupClause,
@@ -655,8 +661,10 @@ class MonitorService implements IMonitorService {
], ],
}, },
}, },
}, }
]); );
const checks = await Check.aggregate(pipeline);
// Get monitor stats // Get monitor stats
const monitorStats = await MonitorStats.findOne({ const monitorStats = await MonitorStats.findOne({