conitnent based query

This commit is contained in:
Alex Holliday committed 2026-02-24 22:30:28 +00:00
1 parent c3e4b5b95f
commit 64fdd04cc7
6 files changed
+51 -6

No files matched your search

+2 -1
View File
@@ -116,8 +116,9 @@ const UptimeDetailsPage = () => {
} }
const params = new URLSearchParams(); const params = new URLSearchParams();
params.append("dateRange", dateRange); params.append("dateRange", dateRange);
params.append("continent", selectedLocation);
return `/monitors/${monitorId}/geo-checks?${params.toString()}`; return `/monitors/${monitorId}/geo-checks?${params.toString()}`;
}, [monitorId, monitor?.type, monitor?.geoCheckEnabled, dateRange]); }, [monitorId, monitor?.type, monitor?.geoCheckEnabled, dateRange, selectedLocation]);
const { data: _geoChecksData, isLoading: _geoChecksIsLoading } = const { data: _geoChecksData, isLoading: _geoChecksIsLoading } =
useGet<GeoChecksResult>(geoChecksUrl, {}, { keepPreviousData: true }); useGet<GeoChecksResult>(geoChecksUrl, {}, { keepPreviousData: true });
@@ -27,6 +27,7 @@ import {
} from "./controllerUtils.js"; } from "./controllerUtils.js";
import { AppError } from "@/utils/AppError.js"; import { AppError } from "@/utils/AppError.js";
import { IMonitorService } from "@/service/index.js"; import { IMonitorService } from "@/service/index.js";
import { GeoContinent } from "@/types/geoCheck.js";
const SERVICE_NAME = "monitorController"; const SERVICE_NAME = "monitorController";
class MonitorController { class MonitorController {
@@ -141,12 +142,14 @@ class MonitorController {
const monitorId = requireString(req?.params?.monitorId, "Monitor ID"); const monitorId = requireString(req?.params?.monitorId, "Monitor ID");
const dateRange = requireString(req?.query?.dateRange, "dateRange"); const dateRange = requireString(req?.query?.dateRange, "dateRange");
const continent = optionalString(req?.query?.continent, "continent") as GeoContinent | undefined;
const teamId = requireTeamId(req?.user?.teamId); const teamId = requireTeamId(req?.user?.teamId);
const data = await this.monitorService.getGeoChecksByMonitorId({ const data = await this.monitorService.getGeoChecksByMonitorId({
teamId, teamId,
monitorId, monitorId,
dateRange, dateRange,
continent,
}); });
return res.status(200).json({ return res.status(200).json({
@@ -1,9 +1,16 @@
import type { GeoCheck, GroupedGeoCheck } from "@/types/geoCheck.js"; import type { GeoCheck, GroupedGeoCheck } from "@/types/geoCheck.js";
import type { GeoContinent } from "@/types/geoCheck.js";
export interface IGeoChecksRepository { export interface IGeoChecksRepository {
createGeoChecks(geoChecks: Omit<GeoCheck, "id" | "__v" | "createdAt" | "updatedAt">[]): Promise<GeoCheck[]>; createGeoChecks(geoChecks: Omit<GeoCheck, "id" | "__v" | "createdAt" | "updatedAt">[]): Promise<GeoCheck[]>;
findByMonitorIdAndDateRange(monitorId: string, startDate: Date, endDate: Date): Promise<GeoCheck[]>; findByMonitorIdAndDateRange(monitorId: string, startDate: Date, endDate: Date): Promise<GeoCheck[]>;
findGroupedByMonitorIdAndDateRange(monitorId: string, startDate: Date, endDate: Date, dateFormat: string): Promise<GroupedGeoCheck[]>; findGroupedByMonitorIdAndDateRange(
monitorId: string,
startDate: Date,
endDate: Date,
dateFormat: string,
continent?: GeoContinent
): Promise<GroupedGeoCheck[]>;
deleteByMonitorId(monitorId: string): Promise<number>; deleteByMonitorId(monitorId: string): Promise<number>;
deleteByTeamId(teamId: string): Promise<number>; deleteByTeamId(teamId: string): Promise<number>;
} }
@@ -117,7 +117,13 @@ class MongoGeoChecksRepository implements IGeoChecksRepository {
} }
}; };
findGroupedByMonitorIdAndDateRange = async (monitorId: string, startDate: Date, endDate: Date, dateFormat: string): Promise<GroupedGeoCheck[]> => { findGroupedByMonitorIdAndDateRange = async (
monitorId: string,
startDate: Date,
endDate: Date,
dateFormat: string,
continent?: string
): Promise<GroupedGeoCheck[]> => {
try { try {
const pipeline: any[] = [ const pipeline: any[] = [
// Match geo checks for this monitor in date range // Match geo checks for this monitor in date range
@@ -134,6 +140,16 @@ class MongoGeoChecksRepository implements IGeoChecksRepository {
{ {
$unwind: "$results", $unwind: "$results",
}, },
// Filter by continent if specified
...(continent
? [
{
$match: {
"results.location.continent": continent,
},
},
]
: []),
// Group by date bucket and continent // Group by date bucket and continent
{ {
$group: { $group: {
+20 -3
View File
@@ -8,6 +8,7 @@ import type {
PageSpeedDetailsResult, PageSpeedDetailsResult,
GamesMap, GamesMap,
} from "@/types/monitor.js"; } from "@/types/monitor.js";
import type { GeoContinent } from "@/types/geoCheck.js";
import type { import type {
IChecksRepository, IChecksRepository,
IGeoChecksRepository, IGeoChecksRepository,
@@ -37,7 +38,7 @@ export interface IMonitorService {
getUptimeDetailsById(args: { teamId: string; monitorId: string; dateRange: string; normalize?: boolean }): Promise<UptimeDetailsResult>; getUptimeDetailsById(args: { teamId: string; monitorId: string; dateRange: string; normalize?: boolean }): Promise<UptimeDetailsResult>;
getHardwareDetailsById(args: { teamId: string; monitorId: string; dateRange: string }): Promise<HardwareDetailsResult>; getHardwareDetailsById(args: { teamId: string; monitorId: string; dateRange: string }): Promise<HardwareDetailsResult>;
getPageSpeedDetailsById(args: { teamId: string; monitorId: string; dateRange: string }): Promise<PageSpeedDetailsResult>; getPageSpeedDetailsById(args: { teamId: string; monitorId: string; dateRange: string }): Promise<PageSpeedDetailsResult>;
getGeoChecksByMonitorId(args: { teamId: string; monitorId: string; dateRange: string }): Promise<any>; getGeoChecksByMonitorId(args: { teamId: string; monitorId: string; dateRange: string; continent?: GeoContinent }): Promise<any>;
getMonitorById(args: { teamId: string; monitorId: string }): Promise<Monitor>; getMonitorById(args: { teamId: string; monitorId: string }): Promise<Monitor>;
getMonitorsByTeamId(args: { getMonitorsByTeamId(args: {
teamId: string; teamId: string;
@@ -322,7 +323,17 @@ export class MonitorService implements IMonitorService {
}; };
}; };
getGeoChecksByMonitorId = async ({ teamId, monitorId, dateRange }: { teamId: string; monitorId: string; dateRange: string }): Promise<any> => { getGeoChecksByMonitorId = async ({
teamId,
monitorId,
dateRange,
continent,
}: {
teamId: string;
monitorId: string;
dateRange: string;
continent?: GeoContinent;
}): Promise<any> => {
const monitor = await this.monitorsRepository.findById(monitorId, teamId); const monitor = await this.monitorsRepository.findById(monitorId, teamId);
if (!monitor) { if (!monitor) {
throw new AppError({ message: `Monitor with ID ${monitorId} not found.`, status: 404 }); throw new AppError({ message: `Monitor with ID ${monitorId} not found.`, status: 404 });
@@ -334,7 +345,13 @@ export class MonitorService implements IMonitorService {
const rangeKey = (dateRange as DateRangeKey) ?? "recent"; const rangeKey = (dateRange as DateRangeKey) ?? "recent";
const { start, end } = this.getDateRange(rangeKey); const { start, end } = this.getDateRange(rangeKey);
const groupedGeoChecks = await this.geoChecksRepository.findGroupedByMonitorIdAndDateRange(monitor.id, start, end, this.getDateFormat(rangeKey)); const groupedGeoChecks = await this.geoChecksRepository.findGroupedByMonitorIdAndDateRange(
monitor.id,
start,
end,
this.getDateFormat(rangeKey),
continent
);
return { groupedGeoChecks }; return { groupedGeoChecks };
}; };
+1
View File
@@ -113,6 +113,7 @@ const getMonitorByIdQueryValidation = joi.object({
dateRange: joi.string().valid("recent", "hour", "day", "week", "month", "all"), dateRange: joi.string().valid("recent", "hour", "day", "week", "month", "all"),
numToDisplay: joi.number(), numToDisplay: joi.number(),
normalize: joi.boolean(), normalize: joi.boolean(),
continent: joi.string().valid(...GeoContinents),
}); });
const getMonitorsByTeamIdParamValidation = joi.object({}); const getMonitorsByTeamIdParamValidation = joi.object({});