extract date range calculation for conisistency

This commit is contained in:
Alex Holliday committed 2026-07-23 10:25:24 -07:00
1 parent 4a90ab8e78
commit 3af00281c4
9 files changed
+78 -119

No files matched your search

@@ -1,5 +1,6 @@
import type { GeoCheck, GroupedGeoCheck } from "@/domain/geo-checks/geo-check.type.js";
import type { GeoContinent, FlatGeoCheck } from "@/domain/geo-checks/geo-check.type.js";
import { DateRange } from "@/types/query.js";
export interface GeoChecksQueryResult {
geoChecksCount: number;
@@ -16,19 +17,13 @@ export interface IGeoChecksRepository {
findByMonitorId(
monitorId: string,
sortOrder: string,
dateRange: string,
dateRange: DateRange,
page: number,
rowsPerPage: number,
continents?: GeoContinent[]
): Promise<FlatGeoChecksQueryResult>;
findByMonitorIdAndDateRange(monitorId: string, startDate: Date, endDate: Date): Promise<GeoCheck[]>;
findGroupedByMonitorIdAndDateRange(
monitorId: string,
startDate: Date,
endDate: Date,
dateFormat: string,
continents?: GeoContinent[]
): Promise<GroupedGeoCheck[]>;
findGroupedByMonitorIdAndDateRange(monitorId: string, DateRange: DateRange, continents?: GeoContinent[]): Promise<GroupedGeoCheck[]>;
deleteByMonitorId(monitorId: string): Promise<number>;
deleteByTeamId(teamId: string): Promise<number>;
deleteByMonitorIdsNotIn(monitorIds: string[]): Promise<number>;
@@ -3,9 +3,10 @@ import type { GeoCheck, GeoCheckMetadata, GeoCheckResult, GroupedGeoCheck, GeoCo
import type { FlatGeoChecksQueryResult } from "./geo-check.repository.interface.js";
import { GeoCheckMetadataDocument, GeoCheckModel, type GeoCheckDocument } from "@/domain/geo-checks/geo-check.model.js";
import mongoose, { PipelineStage } from "mongoose";
import { getDateForRange } from "@/utils/dataUtils.js";
import { getDateForRange, getDateFormat } from "@/utils/dataUtils.js";
import { ILogger } from "@/utils/logger.js";
import { toStringId, toDateString } from "@/utils/mongoMappers.js";
import { DateRange } from "@/types/query.js";
const SERVICE_NAME = "GeoChecksRepository";
@@ -90,7 +91,7 @@ class MongoGeoChecksRepository implements IGeoChecksRepository {
findByMonitorId = async (
monitorId: string,
sortOrder: string,
dateRange: string,
dateRange: DateRange,
page: number,
rowsPerPage: number,
continents?: GeoContinent[]
@@ -98,11 +99,7 @@ class MongoGeoChecksRepository implements IGeoChecksRepository {
try {
const matchStage: Record<string, unknown> = {
"metadata.monitorId": new mongoose.Types.ObjectId(monitorId),
...(getDateForRange(dateRange) && {
createdAt: {
$gte: getDateForRange(dateRange),
},
}),
createdAt: { $gte: getDateForRange(dateRange) },
};
const convertedSortOrder = sortOrder === "asc" ? 1 : -1;
@@ -214,13 +211,11 @@ class MongoGeoChecksRepository implements IGeoChecksRepository {
}
};
findGroupedByMonitorIdAndDateRange = async (
monitorId: string,
startDate: Date,
endDate: Date,
dateFormat: string,
continents?: GeoContinent[]
): Promise<GroupedGeoCheck[]> => {
findGroupedByMonitorIdAndDateRange = async (monitorId: string, dateRange: DateRange, continents?: GeoContinent[]): Promise<GroupedGeoCheck[]> => {
const start = getDateForRange(dateRange);
const dateString = getDateFormat(dateRange);
const end = new Date();
try {
const pipeline: PipelineStage[] = [
// Match geo checks for this monitor in date range
@@ -228,8 +223,8 @@ class MongoGeoChecksRepository implements IGeoChecksRepository {
$match: {
"metadata.monitorId": new mongoose.Types.ObjectId(monitorId),
createdAt: {
$gte: startDate,
$lte: endDate,
$gte: start,
$lte: end,
},
},
},
@@ -253,7 +248,7 @@ class MongoGeoChecksRepository implements IGeoChecksRepository {
_id: {
bucketDate: {
$dateToString: {
format: dateFormat,
format: dateString,
date: "$createdAt",
},
},
@@ -7,6 +7,7 @@ import type { IMonitorsRepository } from "@/domain/monitors/monitor.repository.i
import type { IGlobalPingService } from "@/service/globalPingService.js";
import type { ILogger } from "@/utils/logger.js";
import { AppError } from "@/utils/AppError.js";
import { DateRange } from "@/types/query.js";
const SERVICE_NAME = "GeoChecksService";
@@ -17,7 +18,7 @@ export interface IGeoChecksService {
monitorId: string;
teamId: string;
sortOrder: string;
dateRange: string;
dateRange: DateRange;
page?: number;
rowsPerPage?: number;
continent?: GeoContinent | GeoContinent[];
@@ -155,7 +156,7 @@ export class GeoChecksService implements IGeoChecksService {
monitorId: string;
teamId: string;
sortOrder: string;
dateRange: string;
dateRange: DateRange;
page?: number;
rowsPerPage?: number;
continent: GeoContinent | GeoContinent[];