add DU details query

This commit is contained in:
Alex Holliday committed 2025-02-05 12:45:14 -08:00
1 parent cdb276d567
commit 3d731137df
2 files changed
+230 -1

No files matched your search

+185 -1
View File
@@ -534,4 +534,188 @@ const buildHardwareDetailsPipeline = (monitor, dates, dateString) => {
];
};
export { buildUptimeDetailsPipeline, buildHardwareDetailsPipeline };
const buildDistributedUptimeDetailsPipeline = (monitor, dates, dateString) => {
return [
{
$match: {
monitorId: monitor._id,
},
},
{
$sort: {
createdAt: 1,
},
},
{
$facet: {
aggregateData: [
{
$group: {
_id: null,
avgResponseTime: {
$avg: "$responseTime",
},
lastCheck: {
$last: "$$ROOT",
},
totalChecks: {
$sum: 1,
},
uptBurnt: {
$sum: "$uptBurnt",
},
},
},
],
// For the response time chart, should return checks for date window
// Grouped by: {day: hour}, {week: day}, {month: day}
groupedMapChecks: [
{
$match: {
createdAt: { $gte: dates.start, $lte: dates.end },
},
},
{
$group: {
_id: {
date: {
$dateToString: {
format: dateString,
date: "$createdAt",
},
},
city: "$city",
lat: "$location.lat",
lng: "$location.lng",
},
city: { $first: "$city" }, // Add this line to include city in output
lat: { $first: "$location.lat" },
lng: { $first: "$location.lng" },
avgResponseTime: {
$avg: "$responseTime",
},
totalChecks: {
$sum: 1,
},
},
},
{
$sort: {
"_id.date": 1,
},
},
],
groupedChecks: [
{
$match: {
createdAt: { $gte: dates.start, $lte: dates.end },
},
},
{
$group: {
_id: {
$dateToString: {
format: dateString,
date: "$createdAt",
},
},
avgResponseTime: {
$avg: "$responseTime",
},
totalChecks: {
$sum: 1,
},
},
},
{
$sort: {
_id: 1,
},
},
],
// Average response time for the date window
groupAvgResponseTime: [
{
$match: {
createdAt: { $gte: dates.start, $lte: dates.end },
},
},
{
$group: {
_id: null,
avgResponseTime: {
$avg: "$responseTime",
},
},
},
],
latestChecks: [
{
$sort: { createdAt: -1 }, // Sort by newest first
},
{
$limit: 5, // Get only the first 5 documents
},
{
$project: {
responseTime: 1,
city: 1,
countryCode: 1,
uptBurnt: { $toString: "$uptBurnt" },
},
},
],
},
},
{
$project: {
totalUptBurnt: {
$toString: {
$arrayElemAt: ["$aggregateData.uptBurnt", 0],
},
},
avgResponseTime: {
$arrayElemAt: ["$aggregateData.avgResponseTime", 0],
},
totalChecks: {
$arrayElemAt: ["$aggregateData.totalChecks", 0],
},
latestResponseTime: {
$arrayElemAt: ["$aggregateData.lastCheck.responseTime", 0],
},
timeSinceLastCheck: {
$let: {
vars: {
lastCheck: {
$arrayElemAt: ["$aggregateData.lastCheck", 0],
},
},
in: {
$cond: [
{
$ifNull: ["$$lastCheck", false],
},
{
$subtract: [new Date(), "$$lastCheck.createdAt"],
},
0,
],
},
},
},
groupedMapChecks: "$groupedMapChecks",
groupedChecks: "$groupedChecks",
groupedAvgResponseTime: {
$arrayElemAt: ["$groupAvgResponseTime", 0],
},
latestChecks: "$latestChecks",
},
},
];
};
export {
buildUptimeDetailsPipeline,
buildHardwareDetailsPipeline,
buildDistributedUptimeDetailsPipeline,
};