diff --git a/Server/db/mongo/modules/monitorModule.js b/Server/db/mongo/modules/monitorModule.js index 27bb409b3..d36fb71d5 100644 --- a/Server/db/mongo/modules/monitorModule.js +++ b/Server/db/mongo/modules/monitorModule.js @@ -322,63 +322,6 @@ const getUptimeDetailsById = async (req) => { const { dateRange, normalize } = req.query; const dates = getDateRange(dateRange); - const currentTime = new Date(); - - // Build pipeline - // const aggregateResults = await Check.aggregate([ - // { - // $match: { monitorId: monitor._id }, - // }, - // { - // $sort: { createdAt: 1 }, - // }, - // { - // $group: { - // _id: null, - // latestCheck: { $last: "$$ROOT" }, - // lastFalseCheck: { - // $last: { - // $cond: [{ $eq: ["$status", false] }, "$$ROOT", null], - // }, - // }, - // firstCheck: { $first: "$$ROOT" }, - // averageResponseTime: { $avg: "$responseTime" }, - // upChecks: { - // $sum: { - // $cond: [{ $eq: ["$status", true] }, 1, 0], - // }, - // }, - // totalChecks: { $sum: 1 }, - // }, - // }, - // { - // $project: { - // timeSinceLastCheck: { - // $subtract: [currentTime, "$latestCheck.createdAt"], - // }, - // latestResponseTime: "$latestCheck.responseTime", - // averageResponseTime: "$averageResponseTime", - // latestStatus: "$latestCheck.status", - // uptimeDuration: { - // $cond: [ - // { $eq: ["$latestCheck.status", false] }, - // 0, - // { - // $subtract: [ - // currentTime, - // { - // $ifNull: ["$lastFalseCheck.createdAt", "$firstCheck.createdAt"], - // }, - // ], - // }, - // ], - // }, - // uptimeDecimal: { - // $divide: ["$upChecks", "$totalChecks"], - // }, - // }, - // }, - // ]); const formatLookup = { day: "%Y-%m-%d-%H", @@ -387,214 +330,6 @@ const getUptimeDetailsById = async (req) => { }; const dateString = formatLookup[dateRange]; - - // const groupedChecks = await Check.aggregate([ - // { - // $match: { - // monitorId: monitor._id, - // }, - // }, - // { - // $sort: { - // createdAt: 1, - // }, - // }, - // { - // $facet: { - // aggregateData: [ - // { - // $group: { - // _id: null, - // avgResponseTime: { - // $avg: "$responseTime", - // }, - // firstCheck: { - // $first: "$$ROOT", - // }, - // lastCheck: { - // $last: "$$ROOT", - // }, - // }, - // }, - // ], - // uptimeDuration: [ - // { - // $match: { - // status: false, - // }, - // }, - // { - // $sort: { - // createdAt: 1, - // }, - // }, - // { - // $group: { - // _id: null, - // lastFalseCheck: { - // $last: "$$ROOT", - // }, - // }, - // }, - // ], - // upChecksAggregate: [ - // { - // $match: { - // status: true, - // createdAt: { $gte: dates.start, $lte: dates.end }, - // }, - // }, - // { - // $group: { - // _id: null, - // avgResponseTime: { - // $avg: "$responseTime", - // }, - // totalChecks: { - // $sum: 1, - // }, - // }, - // }, - // ], - // upChecks: [ - // { - // $match: { - // status: true, - // createdAt: { $gte: dates.start, $lte: dates.end }, - // }, - // }, - // { - // $group: { - // _id: { - // $dateToString: { - // format: dateString, - // date: "$createdAt", - // }, - // }, - // totalChecks: { - // $sum: 1, - // }, - // avgResponseTime: { - // $avg: "$responseTime", - // }, - // }, - // }, - // ], - // downChecksAggregate: [ - // { - // $match: { - // status: false, - // createdAt: { $gte: dates.start, $lte: dates.end }, - // }, - // }, - // { - // $group: { - // _id: null, - // avgResponseTime: { - // $avg: "$responseTime", - // }, - // totalChecks: { - // $sum: 1, - // }, - // }, - // }, - // ], - // downChecks: [ - // { - // $match: { - // status: false, - // createdAt: { $gte: dates.start, $lte: dates.end }, - // }, - // }, - // { - // $group: { - // _id: { - // $dateToString: { - // format: dateString, - // date: "$createdAt", - // }, - // }, - // totalChecks: { - // $sum: 1, - // }, - // avgResponseTime: { - // $avg: "$responseTime", - // }, - // }, - // }, - // ], - // }, - // }, - // { - // $project: { - // avgResponseTime: { - // $arrayElemAt: ["$aggregateData.avgResponseTime", 0], - // }, - // timeSinceLastCheck: { - // $let: { - // vars: { - // lastCheck: { - // $arrayElemAt: ["$aggregateData.lastCheck", 0], - // }, - // }, - // in: { - // $cond: [ - // { - // $ifNull: ["$$lastCheck", false], - // }, - // { - // $subtract: [new Date(), "$$lastCheck.createdAt"], - // }, - // 0, - // ], - // }, - // }, - // }, - // timeSinceLastFalseCheck: { - // $let: { - // vars: { - // lastFalseCheck: { - // $arrayElemAt: ["$uptimeDuration.lastFalseCheck", 0], - // }, - // firstCheck: { - // $arrayElemAt: ["$aggregateData.firstCheck", 0], - // }, - // }, - // in: { - // $cond: [ - // { - // $ifNull: ["$$lastFalseCheck", false], - // }, - // { - // $subtract: [new Date(), "$$lastFalseCheck.createdAt"], - // }, - // { - // $cond: [ - // { - // $ifNull: ["$$firstCheck", false], - // }, - // { - // $subtract: [new Date(), "$$firstCheck.createdAt"], - // }, - // 0, - // ], - // }, - // ], - // }, - // }, - // }, - // upChecksAggregate: { - // $arrayElemAt: ["$upChecksAggregate", 0], - // }, - // upChecks: "$upChecks", - // downChecksAggregate: { - // $arrayElemAt: ["$downChecksAggregate", 0], - // }, - // downChecks: "$downChecks", - // }, - // }, - // ]); - const monitorData = await Check.aggregate([ { $match: {