extract getStartState

This commit is contained in:
Alex Holliday committed 2025-12-19 19:10:06 +00:00
1 parent 9fa2980204
commit 26ed713923
4 files changed
+25 -55

No files matched your search

+2 -18
View File
@@ -9,7 +9,7 @@ import type {
import mongoose from "mongoose"; import mongoose from "mongoose";
import ApiError from "@/utils/ApiError.js"; import ApiError from "@/utils/ApiError.js";
import { getChildLogger } from "@/logger/Logger.js"; import { getChildLogger } from "@/logger/Logger.js";
import { stat } from "node:fs"; import { getStartDate } from "@/utils/TimeUtils.js";
const SERVICE_NAME = "CheckService"; const SERVICE_NAME = "CheckService";
const logger = getChildLogger(SERVICE_NAME); const logger = getChildLogger(SERVICE_NAME);
@@ -42,22 +42,6 @@ class CheckService implements ICheckService {
this.SERVICE_NAME = SERVICE_NAME; this.SERVICE_NAME = SERVICE_NAME;
} }
private getStartDate(range: string): Date {
const now = new Date();
switch (range) {
case "2h":
return new Date(now.getTime() - 2 * 60 * 60 * 1000);
case "24h":
return new Date(now.getTime() - 24 * 60 * 60 * 1000);
case "7d":
return new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000);
case "30d":
return new Date(now.getTime() - 30 * 24 * 60 * 60 * 1000);
default:
throw new ApiError("Invalid range parameter", 400);
}
}
private isCapturePayload = (payload: any): payload is ICapturePayload => { private isCapturePayload = (payload: any): payload is ICapturePayload => {
if (!payload || typeof payload !== "object") return false; if (!payload || typeof payload !== "object") return false;
@@ -241,7 +225,7 @@ class CheckService implements ICheckService {
range: string range: string
) => { ) => {
let match; let match;
const startDate = this.getStartDate(range); const startDate = getStartDate(range);
if (monitorId) { if (monitorId) {
const authorized = await Monitor.exists({ const authorized = await Monitor.exists({
@@ -9,8 +9,8 @@ import {
import type { ResolutionType } from "@/db/models/index.js"; import type { ResolutionType } from "@/db/models/index.js";
import mongoose from "mongoose"; import mongoose from "mongoose";
import { getChildLogger } from "@/logger/Logger.js"; import { getChildLogger } from "@/logger/Logger.js";
import ApiError from "@/utils/ApiError.js";
import { ThresholdEvaluationResult } from "@/services/infrastructure/StatusService.js"; import { ThresholdEvaluationResult } from "@/services/infrastructure/StatusService.js";
import { getStartDate } from "@/utils/TimeUtils.js";
type IncidentPopulated = Omit<IIncident, "monitorId" | "resolvedBy"> & { type IncidentPopulated = Omit<IIncident, "monitorId" | "resolvedBy"> & {
monitorId: IMonitor; monitorId: IMonitor;
@@ -66,24 +66,6 @@ class IncidentService implements IIncidentService {
this.SERVICE_NAME = SERVICE_NAME; this.SERVICE_NAME = SERVICE_NAME;
} }
private getStartDate(range: string): Date {
const now = new Date();
switch (range) {
case "all":
return new Date(0);
case "2h":
return new Date(now.getTime() - 2 * 60 * 60 * 1000);
case "24h":
return new Date(now.getTime() - 24 * 60 * 60 * 1000);
case "7d":
return new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000);
case "30d":
return new Date(now.getTime() - 30 * 24 * 60 * 60 * 1000);
default:
throw new ApiError("Invalid range parameter", 400);
}
}
handleStatusChange = async (updatedMonitor: IMonitor, lastCheck: ICheck) => { handleStatusChange = async (updatedMonitor: IMonitor, lastCheck: ICheck) => {
if (updatedMonitor.status === "down") { if (updatedMonitor.status === "down") {
const incident = await this.create( const incident = await this.create(
@@ -216,7 +198,7 @@ class IncidentService implements IIncidentService {
resolved?: boolean, resolved?: boolean,
resolutionType?: ResolutionType resolutionType?: ResolutionType
) => { ) => {
const startDate = this.getStartDate(range); const startDate = getStartDate(range);
const match = { const match = {
teamId: teamId, teamId: teamId,
...(monitorId && { monitorId }), ...(monitorId && { monitorId }),
+2 -17
View File
@@ -7,6 +7,7 @@ import { IJobQueue } from "@/services/infrastructure/JobQueue.js";
import { MonitorWithChecksResponse } from "@/types/index.js"; import { MonitorWithChecksResponse } from "@/types/index.js";
import { MonitorStatus, MonitorType } from "@/db/models/monitors/Monitor.js"; import { MonitorStatus, MonitorType } from "@/db/models/monitors/Monitor.js";
import { Entitlements } from "@/types/entitlements.js"; import { Entitlements } from "@/types/entitlements.js";
import { getStartDate } from "@/utils/TimeUtils.js";
export interface IImportedMonitor { export interface IImportedMonitor {
name: string; name: string;
@@ -239,22 +240,6 @@ class MonitorService implements IMonitorService {
return monitor; return monitor;
}; };
private getStartDate(range: string): Date {
const now = new Date();
switch (range) {
case "1h":
return new Date(now.getTime() - 1 * 60 * 60 * 1000);
case "24h":
return new Date(now.getTime() - 24 * 60 * 60 * 1000);
case "7d":
return new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000);
case "30d":
return new Date(now.getTime() - 30 * 24 * 60 * 60 * 1000);
default:
throw new ApiError("Invalid range parameter", 400);
}
}
private getBaseGroup = (): Record<string, any> => { private getBaseGroup = (): Record<string, any> => {
return { return {
_id: { $dateTrunc: { date: "$createdAt", unit: "minute" } }, _id: { $dateTrunc: { date: "$createdAt", unit: "minute" } },
@@ -681,7 +666,7 @@ class MonitorService implements IMonitorService {
throw new ApiError("Monitor not found", 404); throw new ApiError("Monitor not found", 404);
} }
const startDate = this.getStartDate(range); const startDate = getStartDate(range);
if (range === "24h" || range === "7d" || range === "30d") { if (range === "24h" || range === "7d" || range === "30d") {
return await this.getEmbedChecksOtherRanges(monitor, range, startDate); return await this.getEmbedChecksOtherRanges(monitor, range, startDate);
+19
View File
@@ -0,0 +1,19 @@
import ApiError from "@/utils/ApiError.js";
export const getStartDate = (range: string): Date => {
const now = new Date();
switch (range) {
case "all":
return new Date(0);
case "1h":
return new Date(now.getTime() - 1 * 60 * 60 * 1000);
case "24h":
return new Date(now.getTime() - 24 * 60 * 60 * 1000);
case "7d":
return new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000);
case "30d":
return new Date(now.getTime() - 30 * 24 * 60 * 60 * 1000);
default:
throw new ApiError("Invalid range parameter", 400);
}
};