Add group checks to DB query

This commit is contained in:
Alex Holliday committed 2024-12-31 13:42:25 -08:00
1 parent 7f601b1e11
commit bbae5052c8
1 file changed
+66 -1
+66 -1
View File
@@ -4,7 +4,7 @@ import PageSpeedCheck from "../../models/PageSpeedCheck.js";
import HardwareCheck from "../../models/HardwareCheck.js"; import HardwareCheck from "../../models/HardwareCheck.js";
import { errorMessages } from "../../../utils/messages.js"; import { errorMessages } from "../../../utils/messages.js";
import Notification from "../../models/Notification.js"; import Notification from "../../models/Notification.js";
import { NormalizeData } from "../../../utils/dataUtils.js"; import { NormalizeData, NormalizeDataUptimeDetails } from "../../../utils/dataUtils.js";
import fs from "fs"; import fs from "fs";
import path from "path"; import path from "path";
import { fileURLToPath } from "url"; import { fileURLToPath } from "url";
@@ -647,6 +647,49 @@ const getUptimeDetailsById = async (req) => {
}, },
}, },
], ],
groupChecks: [
{
$match: {
createdAt: { $gte: dates.start, $lte: dates.end },
},
},
{
$group: {
_id: {
$dateToString: {
format: dateString,
date: "$createdAt",
},
},
avgResponseTime: {
$avg: "$responseTime",
},
totalChecks: {
$sum: 1,
},
},
},
{
$sort: {
_id: 1,
},
},
],
groupAggregate: [
{
$match: {
createdAt: { $gte: dates.start, $lte: dates.end },
},
},
{
$group: {
_id: null,
avgResponseTime: {
$avg: "$responseTime",
},
},
},
],
upChecksAggregate: [ upChecksAggregate: [
{ {
$match: { $match: {
@@ -687,6 +730,9 @@ const getUptimeDetailsById = async (req) => {
}, },
}, },
}, },
{
$sort: { _id: 1 },
},
], ],
downChecksAggregate: [ downChecksAggregate: [
{ {
@@ -728,6 +774,9 @@ const getUptimeDetailsById = async (req) => {
}, },
}, },
}, },
{
$sort: { _id: 1 },
},
], ],
}, },
}, },
@@ -739,6 +788,9 @@ const getUptimeDetailsById = async (req) => {
totalChecks: { totalChecks: {
$arrayElemAt: ["$aggregateData.totalChecks", 0], $arrayElemAt: ["$aggregateData.totalChecks", 0],
}, },
latestResponseTime: {
$arrayElemAt: ["$aggregateData.lastCheck.responseTime", 0],
},
timeSinceLastCheck: { timeSinceLastCheck: {
$let: { $let: {
vars: { vars: {
@@ -792,6 +844,10 @@ const getUptimeDetailsById = async (req) => {
}, },
}, },
}, },
groupChecks: "$groupChecks",
groupAggregate: {
$arrayElemAt: ["$groupAggregate", 0],
},
upChecksAggregate: { upChecksAggregate: {
$arrayElemAt: ["$upChecksAggregate", 0], $arrayElemAt: ["$upChecksAggregate", 0],
}, },
@@ -804,6 +860,12 @@ const getUptimeDetailsById = async (req) => {
}, },
]); ]);
const normalizedGroupChecks = NormalizeDataUptimeDetails(
monitorData[0].groupChecks,
10,
100
);
const monitorStats = { const monitorStats = {
...monitor.toObject(), ...monitor.toObject(),
stats: { stats: {
@@ -811,6 +873,9 @@ const getUptimeDetailsById = async (req) => {
totalChecks: monitorData[0].totalChecks, totalChecks: monitorData[0].totalChecks,
timeSinceLastCheck: monitorData[0].timeSinceLastCheck, timeSinceLastCheck: monitorData[0].timeSinceLastCheck,
timeSinceLastFalseCheck: monitorData[0].timeSinceLastFalseCheck, timeSinceLastFalseCheck: monitorData[0].timeSinceLastFalseCheck,
latestResponseTime: monitorData[0].latestResponseTime,
groupChecks: normalizedGroupChecks,
groupAggregate: monitorData[0].groupAggregate,
upChecksAggregate: monitorData[0].upChecksAggregate, upChecksAggregate: monitorData[0].upChecksAggregate,
upChecks: monitorData[0].upChecks, upChecks: monitorData[0].upChecks,
downChecksAggregate: monitorData[0].downChecksAggregate, downChecksAggregate: monitorData[0].downChecksAggregate,