show all checks

This commit is contained in:
Alex Holliday
2026-02-25 18:43:12 +00:00
parent 88e28b9819
commit 477c30286d
9 changed files with 123 additions and 39 deletions
+7 -2
View File
@@ -142,14 +142,19 @@ class MonitorController {
const monitorId = requireString(req?.params?.monitorId, "Monitor ID");
const dateRange = requireString(req?.query?.dateRange, "dateRange");
const continent = optionalString(req?.query?.continent, "continent") as GeoContinent | undefined;
const continentParam = req?.query?.continent;
const continents = continentParam
? Array.isArray(continentParam)
? (continentParam as GeoContinent[])
: [continentParam as GeoContinent]
: undefined;
const teamId = requireTeamId(req?.user?.teamId);
const data = await this.monitorService.getGeoChecksByMonitorId({
teamId,
monitorId,
dateRange,
continent,
continents,
});
return res.status(200).json({
@@ -14,7 +14,7 @@ export interface IGeoChecksRepository {
dateRange: string,
page: number,
rowsPerPage: number,
continent?: GeoContinent
continents?: GeoContinent[]
): Promise<GeoChecksQueryResult>;
findByMonitorIdAndDateRange(monitorId: string, startDate: Date, endDate: Date): Promise<GeoCheck[]>;
findGroupedByMonitorIdAndDateRange(
@@ -22,7 +22,7 @@ export interface IGeoChecksRepository {
startDate: Date,
endDate: Date,
dateFormat: string,
continent?: GeoContinent
continents?: GeoContinent[]
): Promise<GroupedGeoCheck[]>;
deleteByMonitorId(monitorId: string): Promise<number>;
deleteByTeamId(teamId: string): Promise<number>;
@@ -111,9 +111,16 @@ class MongoGeoChecksRepository implements IGeoChecksRepository {
dateRange: string,
page: number,
rowsPerPage: number,
continent?: GeoContinent
continents?: GeoContinent[]
): Promise<GeoChecksQueryResult> => {
try {
this.logger.debug({
message: "findByMonitorId called",
service: SERVICE_NAME,
method: "findByMonitorId",
details: { monitorId, continents, continentsLength: continents?.length },
});
const matchStage: Record<string, any> = {
"metadata.monitorId": new mongoose.Types.ObjectId(monitorId),
...(dateRangeLookup[dateRange] && {
@@ -130,13 +137,19 @@ class MongoGeoChecksRepository implements IGeoChecksRepository {
skip = page * rowsPerPage;
}
if (continent) {
if (continents && continents.length > 0) {
this.logger.debug({
message: "Filtering by continents",
service: SERVICE_NAME,
method: "findByMonitorId",
details: { continents },
});
const pipeline: any[] = [
{ $match: matchStage },
{ $unwind: "$results" },
{
$match: {
"results.location.continent": continent,
"results.location.continent": { $in: continents },
},
},
{
@@ -162,7 +175,7 @@ class MongoGeoChecksRepository implements IGeoChecksRepository {
GeoCheckModel.aggregate([
{ $match: matchStage },
{ $unwind: "$results" },
{ $match: { "results.location.continent": continent } },
{ $match: { "results.location.continent": { $in: continents } } },
{ $group: { _id: "$_id" } },
{ $count: "count" },
]),
@@ -174,12 +187,44 @@ class MongoGeoChecksRepository implements IGeoChecksRepository {
return { geoChecksCount, geoChecks };
} else {
const [geoChecksCount, docs] = await Promise.all([
GeoCheckModel.countDocuments(matchStage),
GeoCheckModel.find(matchStage).sort({ createdAt: convertedSortOrder }).skip(skip).limit(rowsPerPage).lean() as Promise<GeoCheckDocument[]>,
this.logger.debug({
message: "No continent filtering - returning all checks",
service: SERVICE_NAME,
method: "findByMonitorId",
});
// Still need to unwind to show each location as a separate row
const pipeline: any[] = [
{ $match: matchStage },
{ $unwind: "$results" },
{
$group: {
_id: "$_id",
doc: { $first: "$$ROOT" },
results: { $push: "$results" },
},
},
{
$replaceRoot: {
newRoot: {
$mergeObjects: ["$doc", { results: "$results" }],
},
},
},
{ $sort: { createdAt: convertedSortOrder } },
{ $skip: skip },
{ $limit: rowsPerPage },
];
const [countPipeline, dataResults] = await Promise.all([
GeoCheckModel.aggregate([{ $match: matchStage }, { $unwind: "$results" }, { $group: { _id: "$_id" } }, { $count: "count" }]),
GeoCheckModel.aggregate(pipeline),
]);
return { geoChecksCount, geoChecks: docs.map(this.toEntity) };
const geoChecksCount = countPipeline[0]?.count || 0;
const geoChecks = dataResults.map(this.toEntity);
return { geoChecksCount, geoChecks };
}
} catch (error: any) {
this.logger.error({
@@ -217,7 +262,7 @@ class MongoGeoChecksRepository implements IGeoChecksRepository {
startDate: Date,
endDate: Date,
dateFormat: string,
continent?: string
continents?: GeoContinent[]
): Promise<GroupedGeoCheck[]> => {
try {
const pipeline: any[] = [
@@ -236,11 +281,11 @@ class MongoGeoChecksRepository implements IGeoChecksRepository {
$unwind: "$results",
},
// Filter by continent if specified
...(continent
...(continents && continents.length > 0
? [
{
$match: {
"results.location.continent": continent,
"results.location.continent": { $in: continents },
},
},
]
@@ -177,10 +177,19 @@ class GeoChecksService implements IGeoChecksService {
}
let { sortOrder, dateRange, page, rowsPerPage, continent } = query;
const continents = continent ? (Array.isArray(continent) ? continent : [continent]) : undefined;
this.logger.debug({
message: "getGeoChecksByMonitor query params",
service: SERVICE_NAME,
method: "getGeoChecksByMonitor",
details: { continent, continents, query },
});
const parsedPage = page ? parseInt(page) : page;
const parsedRowsPerPage = rowsPerPage ? parseInt(rowsPerPage) : rowsPerPage;
const result = await this.geoChecksRepository.findByMonitorId(monitorId, sortOrder, dateRange, parsedPage, parsedRowsPerPage, continent);
const result = await this.geoChecksRepository.findByMonitorId(monitorId, sortOrder, dateRange, parsedPage, parsedRowsPerPage, continents);
return result;
};
@@ -38,7 +38,7 @@ export interface IMonitorService {
getUptimeDetailsById(args: { teamId: string; monitorId: string; dateRange: string; normalize?: boolean }): Promise<UptimeDetailsResult>;
getHardwareDetailsById(args: { teamId: string; monitorId: string; dateRange: string }): Promise<HardwareDetailsResult>;
getPageSpeedDetailsById(args: { teamId: string; monitorId: string; dateRange: string }): Promise<PageSpeedDetailsResult>;
getGeoChecksByMonitorId(args: { teamId: string; monitorId: string; dateRange: string; continent?: GeoContinent }): Promise<any>;
getGeoChecksByMonitorId(args: { teamId: string; monitorId: string; dateRange: string; continents?: GeoContinent[] }): Promise<any>;
getMonitorById(args: { teamId: string; monitorId: string }): Promise<Monitor>;
getMonitorsByTeamId(args: {
teamId: string;
@@ -327,12 +327,12 @@ export class MonitorService implements IMonitorService {
teamId,
monitorId,
dateRange,
continent,
continents,
}: {
teamId: string;
monitorId: string;
dateRange: string;
continent?: GeoContinent;
continents?: GeoContinent[];
}): Promise<any> => {
const monitor = await this.monitorsRepository.findById(monitorId, teamId);
if (!monitor) {
@@ -350,7 +350,7 @@ export class MonitorService implements IMonitorService {
start,
end,
this.getDateFormat(rangeKey),
continent
continents
);
return { groupedGeoChecks };
+1 -1
View File
@@ -324,7 +324,7 @@ const getChecksQueryValidation = joi.object({
page: joi.number(),
rowsPerPage: joi.number(),
status: joi.boolean(),
continent: joi.string().valid(...GeoContinents),
continent: joi.alternatives().try(joi.string().valid(...GeoContinents), joi.array().items(joi.string().valid(...GeoContinents))),
});
const getTeamChecksQueryValidation = joi.object({