aggregation

This commit is contained in:
Alex Holliday committed 2026-02-22 21:15:02 +00:00
1 parent ef5629d8e4
commit b4f8fa7d72
5 files changed
+62 -1

No files matched your search

+9
View File
@@ -160,6 +160,14 @@ export interface PageSpeedGroupedCheck {
totalChecks: number; totalChecks: number;
} }
export interface GroupedGeoCheck {
bucketDate: string;
continent: string;
avgResponseTime: number;
totalChecks: number;
uptimePercentage: number;
}
export interface LatestCheck { export interface LatestCheck {
status: boolean; status: boolean;
responseTime: number; responseTime: number;
@@ -236,6 +244,7 @@ export interface UptimeChecksResult {
groupedDownChecks: GroupedCheck[]; groupedDownChecks: GroupedCheck[];
uptimePercentage: number; uptimePercentage: number;
avgResponseTime: number; avgResponseTime: number;
groupedGeoChecks?: GroupedGeoCheck[];
} }
export interface ChecksSummary { export interface ChecksSummary {
+1 -1
View File
@@ -180,7 +180,7 @@ const MonitorSchema = new Schema<MonitorDocument>(
}, },
geoCheckInterval: { geoCheckInterval: {
type: Number, type: Number,
default: 5, default: 300000,
}, },
recentChecks: { recentChecks: {
type: [checkSnapshotSchema], type: [checkSnapshotSchema],
@@ -532,12 +532,41 @@ class MongoChecksRepository implements IChecksRepository {
{ $sort: { _id: 1 } }, { $sort: { _id: 1 } },
{ $project: { bucketDate: "$_id", avgResponseTime: 1, totalChecks: 1, _id: 0 } }, { $project: { bucketDate: "$_id", avgResponseTime: 1, totalChecks: 1, _id: 0 } },
], ],
groupedGeoChecks: [
{ $match: { geoResults: { $exists: true, $ne: null, $not: { $size: 0 } } } },
{ $unwind: "$geoResults" },
{
$group: {
_id: {
date: { $dateToString: { format: dateString, date: "$createdAt" } },
continent: "$geoResults.continent",
},
avgResponseTime: { $avg: "$geoResults.timings.total" },
totalChecks: { $sum: 1 },
upChecks: { $sum: { $cond: [{ $eq: ["$geoResults.status", true] }, 1, 0] } },
},
},
{
$project: {
_id: 0,
bucketDate: "$_id.date",
continent: "$_id.continent",
avgResponseTime: 1,
totalChecks: 1,
uptimePercentage: {
$cond: [{ $eq: ["$totalChecks", 0] }, 0, { $divide: ["$upChecks", "$totalChecks"] }],
},
},
},
{ $sort: { bucketDate: 1, continent: 1 } },
],
}, },
}, },
]); ]);
const uptimePercentage = result?.uptimePercentage?.[0]?.percentage ?? 0; const uptimePercentage = result?.uptimePercentage?.[0]?.percentage ?? 0;
const avgResponseTime = result?.groupedAvgResponseTime?.[0]?.avgResponseTime ?? 0; const avgResponseTime = result?.groupedAvgResponseTime?.[0]?.avgResponseTime ?? 0;
const groupedGeoChecks = result?.groupedGeoChecks ?? [];
return { return {
monitorType, monitorType,
@@ -546,6 +575,7 @@ class MongoChecksRepository implements IChecksRepository {
groupedDownChecks: result?.groupedDownChecks ?? [], groupedDownChecks: result?.groupedDownChecks ?? [],
uptimePercentage, uptimePercentage,
avgResponseTime, avgResponseTime,
groupedGeoChecks,
}; };
}; };
+9
View File
@@ -202,6 +202,14 @@ export interface PageSpeedGroupedCheck {
totalChecks: number; totalChecks: number;
} }
export interface GroupedGeoCheck {
bucketDate: string;
continent: string;
avgResponseTime: number;
totalChecks: number;
uptimePercentage: number;
}
export interface UptimeChecksResult { export interface UptimeChecksResult {
monitorType: Exclude<MonitorType, "hardware" | "pagespeed">; monitorType: Exclude<MonitorType, "hardware" | "pagespeed">;
groupedChecks: GroupedCheck[]; groupedChecks: GroupedCheck[];
@@ -209,6 +217,7 @@ export interface UptimeChecksResult {
groupedDownChecks: GroupedCheck[]; groupedDownChecks: GroupedCheck[];
uptimePercentage: number; uptimePercentage: number;
avgResponseTime: number; avgResponseTime: number;
groupedGeoChecks?: GroupedGeoCheck[];
} }
export interface ChecksSummary { export interface ChecksSummary {
+13
View File
@@ -1,5 +1,6 @@
import joi, { type CustomHelpers } from "joi"; import joi, { type CustomHelpers } from "joi";
import { type UserRole, UserRoles } from "@/types/user.js"; import { type UserRole, UserRoles } from "@/types/user.js";
import { GeoCheckLocations } from "@/types/monitor.js";
//**************************************** //****************************************
// Custom Validators // Custom Validators
@@ -172,6 +173,12 @@ const createMonitorBodyValidation = joi.object({
gameId: joi.string().allow(""), gameId: joi.string().allow(""),
selectedDisks: joi.array().items(joi.string()).optional(), selectedDisks: joi.array().items(joi.string()).optional(),
group: joi.string().max(50).trim().allow(null, "").optional(), group: joi.string().max(50).trim().allow(null, "").optional(),
geoCheckEnabled: joi.boolean().optional(),
geoCheckLocations: joi
.array()
.items(joi.string().valid(...GeoCheckLocations))
.optional(),
geoCheckInterval: joi.number().min(300000).optional(),
}); });
const createMonitorsBodyValidation = joi.array().items( const createMonitorsBodyValidation = joi.array().items(
@@ -203,6 +210,12 @@ const editMonitorBodyValidation = joi
gameId: joi.string().allow(""), gameId: joi.string().allow(""),
selectedDisks: joi.array().items(joi.string()).optional(), selectedDisks: joi.array().items(joi.string()).optional(),
group: joi.string().max(50).trim().allow(null, "").optional(), group: joi.string().max(50).trim().allow(null, "").optional(),
geoCheckEnabled: joi.boolean().optional(),
geoCheckLocations: joi
.array()
.items(joi.string().valid(...GeoCheckLocations))
.optional(),
geoCheckInterval: joi.number().min(5).optional(),
}) })
.options({ stripUnknown: true }); .options({ stripUnknown: true });