date range extraction, minor fixes

This commit is contained in:
Alex Holliday committed 2026-02-25 20:13:07 +00:00
1 parent 885acf6fe6
commit ba6e931c59
5 files changed
+41 -39

No files matched your search

@@ -15,18 +15,10 @@ import type {
} from "@/types/index.js";
import { CheckModel, MonitorModel, type CheckDocument } from "@/db/models/index.js";
import mongoose from "mongoose";
import { getDateForRange } from "@/utils/dataUtils.js";
const SERVICE_NAME = "StatusService";
const dateRangeLookup: Record<string, Date | undefined> = {
recent: new Date(new Date().setHours(new Date().getHours() - 2)),
hour: new Date(new Date().setHours(new Date().getHours() - 1)),
day: new Date(new Date().setDate(new Date().getDate() - 1)),
week: new Date(new Date().setDate(new Date().getDate() - 7)),
month: new Date(new Date().setMonth(new Date().getMonth() - 1)),
all: undefined,
};
export type LatestChecksMap = Record<string, Check[]>;
type DateRange = { start: Date; end: Date };
type HardwareUpChecks = { totalChecks: number };
@@ -248,9 +240,9 @@ class MongoChecksRepository implements IChecksRepository {
const matchStage: Record<string, any> = {
"metadata.monitorId": new mongoose.Types.ObjectId(monitorId),
...(typeof status !== "undefined" && { status }),
...(dateRangeLookup[dateRange] && {
...(getDateForRange(dateRange) && {
createdAt: {
$gte: dateRangeLookup[dateRange],
$gte: getDateForRange(dateRange),
},
}),
};
@@ -299,9 +291,9 @@ class MongoChecksRepository implements IChecksRepository {
findByTeamId = async (sortOrder: string, dateRange: string, filter: string, page: number, rowsPerPage: number, teamId: string) => {
const matchStage: Record<string, any> = {
"metadata.teamId": new mongoose.Types.ObjectId(teamId),
...(dateRangeLookup[dateRange] && {
...(getDateForRange(dateRange) && {
createdAt: {
$gte: dateRangeLookup[dateRange],
$gte: getDateForRange(dateRange),
},
}),
};
@@ -386,9 +378,9 @@ class MongoChecksRepository implements IChecksRepository {
findSummaryByTeamId = async (teamId: string, dateRange: string) => {
const baseMatch = {
"metadata.teamId": new mongoose.Types.ObjectId(teamId),
...(dateRangeLookup[dateRange] && {
...(getDateForRange(dateRange) && {
createdAt: {
$gte: dateRangeLookup[dateRange],
$gte: getDateForRange(dateRange),
},
}),
};
@@ -3,16 +3,10 @@ import type { GeoCheck, GeoCheckMetadata, GeoCheckResult, GroupedGeoCheck, GeoCo
import type { GeoChecksQueryResult, FlatGeoChecksQueryResult } from "./IGeoChecksRepository.js";
import { GeoCheckModel, type GeoCheckDocument } from "@/db/models/index.js";
import mongoose from "mongoose";
import { getDateForRange } from "@/utils/dataUtils.js";
const SERVICE_NAME = "GeoChecksRepository";
const dateRangeLookup: Record<string, Date> = {
recent: new Date(Date.now() - 60 * 60 * 1000),
day: new Date(Date.now() - 24 * 60 * 60 * 1000),
week: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000),
month: new Date(Date.now() - 30 * 24 * 60 * 60 * 1000),
};
class MongoGeoChecksRepository implements IGeoChecksRepository {
static SERVICE_NAME = SERVICE_NAME;
@@ -116,9 +110,9 @@ class MongoGeoChecksRepository implements IGeoChecksRepository {
try {
const matchStage: Record<string, any> = {
"metadata.monitorId": new mongoose.Types.ObjectId(monitorId),
...(dateRangeLookup[dateRange] && {
...(getDateForRange(dateRange) && {
createdAt: {
$gte: dateRangeLookup[dateRange],
$gte: getDateForRange(dateRange),
},
}),
};
+2 -10
View File
@@ -3,21 +3,13 @@ import type { Monitor } from "@/types/monitor.js";
import type { MonitorStatusResponse } from "@/types/network.js";
import { AppError } from "@/utils/AppError.js";
import { ParseBoolean } from "@/utils/utils.js";
import { getDateForRange } from "@/utils/dataUtils.js";
import type { IIncidentsRepository, IMonitorsRepository, IUsersRepository } from "@/repositories/index.js";
import type { Incident } from "@/types/index.js";
import type { MonitorActionDecision } from "@/service/infrastructure/SuperSimpleQueue/SuperSimpleQueueHelper.js";
import type { INotificationMessageBuilder } from "@/service/infrastructure/notificationMessageBuilder.js";
import type { ILogger } from "@/utils/logger.js";
const dateRangeLookup: Record<string, Date | undefined> = {
recent: new Date(new Date().setHours(new Date().getHours() - 2)),
hour: new Date(new Date().setHours(new Date().getHours() - 1)),
day: new Date(new Date().setDate(new Date().getDate() - 1)),
week: new Date(new Date().setDate(new Date().getDate() - 7)),
month: new Date(new Date().setMonth(new Date().getMonth() - 1)),
all: undefined,
};
class IncidentService {
static SERVICE_NAME = SERVICE_NAME;
@@ -177,7 +169,7 @@ class IncidentService {
throw new AppError({ message: "No team ID in request", service: SERVICE_NAME, method: "getIncidentsByTeam", status: 400 });
}
const startDate = dateRangeLookup[dateRange];
const startDate = getDateForRange(dateRange);
const parsedPage = Number.isFinite(parseInt(page)) ? parseInt(page) : 0;
const parsedRowsPerPage = Number.isFinite(parseInt(rowsPerPage)) ? parseInt(rowsPerPage) : 20;
@@ -145,7 +145,7 @@ class SuperSimpleQueue implements ISuperSimpleQueue {
pauseJob = async (monitor: any) => {
const result = await this.scheduler.pauseJob(monitor.id);
if (result === false) {
throw new Error("Failed to resume monitor");
throw new Error("Failed to pause monitor");
}
await this.scheduler.pauseJob(`${monitor.id}-geo`);
this.logger.debug({
@@ -160,10 +160,9 @@ class SuperSimpleQueue implements ISuperSimpleQueue {
if (result === false) {
throw new Error("Failed to resume monitor");
}
const geoResult = await this.scheduler.resumeJob(`${monitor.id}-geo`);
if (geoResult === false) {
throw new Error("Failed to resume geo check for monitor");
}
await this.scheduler.resumeJob(`${monitor.id}-geo`);
this.logger.debug({
message: `Resumed monitor ${monitor.id}`,
service: SERVICE_NAME,
+25
View File
@@ -1,5 +1,30 @@
import type { GroupedCheck, NormalizedCheck, NormalizedUptimeCheck, HasResponseTime } from "@/types/index.js";
/**
* Get a Date object for a given date range string.
* Returns a date in the past based on the range, or undefined for 'all'.
* This is calculated fresh on each call to ensure accuracy.
*/
export const getDateForRange = (dateRange: string): Date | undefined => {
const now = Date.now();
switch (dateRange) {
case "recent":
return new Date(now - 2 * 60 * 60 * 1000); // 2 hours
case "hour":
return new Date(now - 60 * 60 * 1000); // 1 hour
case "day":
return new Date(now - 24 * 60 * 60 * 1000); // 1 day
case "week":
return new Date(now - 7 * 24 * 60 * 60 * 1000); // 7 days
case "month":
return new Date(now - 30 * 24 * 60 * 60 * 1000); // 30 days
case "all":
return undefined;
default:
return undefined;
}
};
const calculatePercentile = <T extends HasResponseTime>(arr: T[], percentile: number): number => {
const sorted = arr.slice().sort((a, b) => a.responseTime - b.responseTime);
const index = (percentile / 100) * (sorted.length - 1);