Refactored getMonitorStats for clarity

This commit is contained in:
Alex Holliday committed 2024-08-13 10:32:29 -07:00
1 parent 02668aeb08
commit 578eb33b91
3 files changed
+29 -23

No files matched your search

+1 -1
View File
@@ -119,7 +119,7 @@ const DetailsPage = () => {
const fetchMonitor = useCallback(async () => { const fetchMonitor = useCallback(async () => {
try { try {
const res = await axiosInstance.get( const res = await axiosInstance.get(
`/monitors/stats/${monitorId}?filter=${dateRange}&numToDisplay=50&normalize=true`, `/monitors/stats/${monitorId}?dateRange=${dateRange}&numToDisplay=50&normalize=true`,
{ {
headers: { headers: {
Authorization: `Bearer ${authToken}`, Authorization: `Bearer ${authToken}`,
+27 -21
View File
@@ -149,7 +149,7 @@ const getMonitorStatsById = async (req) => {
try { try {
const { monitorId } = req.params; const { monitorId } = req.params;
let { status, limit, sortOrder, filter, numToDisplay, normalize } = let { status, limit, sortOrder, dateRange, numToDisplay, normalize } =
req.query; req.query;
// This effectively removes limit, returning all checks // This effectively removes limit, returning all checks
@@ -161,37 +161,44 @@ const getMonitorStatsById = async (req) => {
// Get monitor // Get monitor
const monitor = await Monitor.findById(monitorId); const monitor = await Monitor.findById(monitorId);
// Determine if this is a pagespeed monitor or an http/ping monitor
let model = let model =
monitor.type === "http" || monitor.type === "ping" monitor.type === "http" || monitor.type === "ping"
? Check ? Check
: PageSpeedCheck; : PageSpeedCheck;
// Build monitor stats object
const monitorStats = { const monitorStats = {
...monitor.toObject(), ...monitor.toObject(),
}; };
// Get all checks to calculate stats
// Start building query
const checksQuery = { monitorId: monitor._id }; const checksQuery = { monitorId: monitor._id };
// Get all checks // Get all checks
const checksAll = await model.find(checksQuery).sort({ const checksAll = await model.find(checksQuery).sort({
createdAt: sortOrder, createdAt: sortOrder,
}); });
if (monitor.type === "http" || monitor.type === "ping") { if (monitor.type === "http" || monitor.type === "ping") {
// Get checks in 24 hours const checksQuery24Hours = {
checksQuery.createdAt = { $gte: filterLookup.day }; ...checksQuery,
const checks24Hours = await model.find(checksQuery).sort({ createdAt: { $gte: filterLookup.day },
createdAt: sortOrder, };
}); const checksQuery30Days = {
// Get checks in 30 days ...checksQuery,
checksQuery.createdAt = { $gte: filterLookup.month }; createdAt: { $gte: filterLookup.month },
const checks30Days = await model.find(checksQuery).sort({ };
createdAt: sortOrder, const checksQuery60Mins = {
}); ...checksQuery,
// Get checks in 60 mins createdAt: { $gte: filterLookup.hour },
checksQuery.createdAt = { $gte: filterLookup.hour }; };
const checks60Mins = await model
.find(checksQuery) const [checks24Hours, checks30Days, checks60Mins] = await Promise.all([
.sort({ createdAt: sortOrder }); model.find(checksQuery24Hours).sort({ createdAt: sortOrder }),
model.find(checksQuery30Days).sort({ createdAt: sortOrder }),
model.find(checksQuery60Mins).sort({ createdAt: sortOrder }),
]);
// HTTP/PING Specific stats // HTTP/PING Specific stats
monitorStats.avgResponseTime24hours = monitorStats.avgResponseTime24hours =
@@ -202,14 +209,13 @@ const getMonitorStatsById = async (req) => {
} }
//Get checks for dateRange //Get checks for dateRange
if (status !== undefined) { if (status !== undefined) {
checksQuery.status = status; checksQuery.status = status;
} }
// Filter checks by "day", "week", or "month" // Filter checks by "day", "week", or "month"
if (filter !== undefined) { if (dateRange !== undefined) {
checksQuery.createdAt = { $gte: filterLookup[filter] }; checksQuery.createdAt = { $gte: filterLookup[dateRange] };
} }
let dateRangeChecks = await model let dateRangeChecks = await model
@@ -236,7 +242,7 @@ const getMonitorStatsById = async (req) => {
dateRangeChecks = NormalizeData(dateRangeChecks, 1, 100); dateRangeChecks = NormalizeData(dateRangeChecks, 1, 100);
} }
// Add common stats // Add common stats and stats that depend on the dateRange
monitorStats.uptimeDuration = calculateUptimeDuration(checksAll); monitorStats.uptimeDuration = calculateUptimeDuration(checksAll);
monitorStats.lastChecked = getLastChecked(checksAll); monitorStats.lastChecked = getLastChecked(checksAll);
monitorStats.latestResponseTime = getLatestResponseTime(checksAll); monitorStats.latestResponseTime = getLatestResponseTime(checksAll);
+1 -1
View File
@@ -126,7 +126,7 @@ const getMonitorByIdQueryValidation = joi.object({
status: joi.boolean(), status: joi.boolean(),
sortOrder: joi.string().valid("asc", "desc"), sortOrder: joi.string().valid("asc", "desc"),
limit: joi.number(), limit: joi.number(),
filter: joi.string().valid("day", "week", "month"), dateRange: joi.string().valid("day", "week", "month"),
numToDisplay: joi.number(), numToDisplay: joi.number(),
normalize: joi.boolean(), normalize: joi.boolean(),
}); });