update queries to return consistent values

This commit is contained in:
Alex Holliday
2025-01-31 07:33:29 -08:00
parent abd0006fb1
commit 22b20409fc
3 changed files with 17 additions and 10 deletions
+6 -2
View File
@@ -123,8 +123,12 @@ const getChecksByMonitor = async (req) => {
},
{
$project: {
checksCount: { $arrayElemAt: ["$summary.checksCount", 0] },
checks: "$checks",
checksCount: {
$ifNull: [{ $arrayElemAt: ["$summary.checksCount", 0] }, 0],
},
checks: {
$ifNull: ["$checks", []],
},
},
},
]);
+1
View File
@@ -340,6 +340,7 @@ const getUptimeDetailsById = async (req) => {
};
const dateString = formatLookup[dateRange];
const results = await Check.aggregate(
buildUptimeDetailsPipeline(monitor, dates, dateString)
);
@@ -218,18 +218,20 @@ const buildUptimeDetailsPipeline = (monitor, dates, dateString) => {
{
$project: {
uptimeStreak: {
$cond: [
{ $eq: [{ $size: { $first: "$uptimeStreak.streak.checks" } }, 0] },
0,
{
$subtract: [
new Date(),
$let: {
vars: {
checks: { $ifNull: [{ $first: "$uptimeStreak.streak.checks" }, []] },
},
in: {
$cond: [
{ $eq: [{ $size: "$$checks" }, 0] },
0,
{
$last: { $first: "$uptimeStreak.streak.checks.createdAt" },
$subtract: [new Date(), { $last: "$$checks.createdAt" }],
},
],
},
],
},
},
avgResponseTime: {
$arrayElemAt: ["$aggregateData.avgResponseTime", 0],