Skip to content

Commit 26ed713

Browse files
committed
extract getStartState
1 parent 9fa2980 commit 26ed713

File tree

4 files changed

+25
-55
lines changed

4 files changed

+25
-55
lines changed

server/src/services/business/CheckService.ts

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type {
99
import mongoose from "mongoose";
1010
import ApiError from "@/utils/ApiError.js";
1111
import { getChildLogger } from "@/logger/Logger.js";
12-
import { stat } from "node:fs";
12+
import { getStartDate } from "@/utils/TimeUtils.js";
1313

1414
const SERVICE_NAME = "CheckService";
1515
const logger = getChildLogger(SERVICE_NAME);
@@ -42,22 +42,6 @@ class CheckService implements ICheckService {
4242
this.SERVICE_NAME = SERVICE_NAME;
4343
}
4444

45-
private getStartDate(range: string): Date {
46-
const now = new Date();
47-
switch (range) {
48-
case "2h":
49-
return new Date(now.getTime() - 2 * 60 * 60 * 1000);
50-
case "24h":
51-
return new Date(now.getTime() - 24 * 60 * 60 * 1000);
52-
case "7d":
53-
return new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000);
54-
case "30d":
55-
return new Date(now.getTime() - 30 * 24 * 60 * 60 * 1000);
56-
default:
57-
throw new ApiError("Invalid range parameter", 400);
58-
}
59-
}
60-
6145
private isCapturePayload = (payload: any): payload is ICapturePayload => {
6246
if (!payload || typeof payload !== "object") return false;
6347

@@ -241,7 +225,7 @@ class CheckService implements ICheckService {
241225
range: string
242226
) => {
243227
let match;
244-
const startDate = this.getStartDate(range);
228+
const startDate = getStartDate(range);
245229

246230
if (monitorId) {
247231
const authorized = await Monitor.exists({

server/src/services/business/IncidentService.ts

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import {
99
import type { ResolutionType } from "@/db/models/index.js";
1010
import mongoose from "mongoose";
1111
import { getChildLogger } from "@/logger/Logger.js";
12-
import ApiError from "@/utils/ApiError.js";
1312
import { ThresholdEvaluationResult } from "@/services/infrastructure/StatusService.js";
13+
import { getStartDate } from "@/utils/TimeUtils.js";
1414

1515
type IncidentPopulated = Omit<IIncident, "monitorId" | "resolvedBy"> & {
1616
monitorId: IMonitor;
@@ -66,24 +66,6 @@ class IncidentService implements IIncidentService {
6666
this.SERVICE_NAME = SERVICE_NAME;
6767
}
6868

69-
private getStartDate(range: string): Date {
70-
const now = new Date();
71-
switch (range) {
72-
case "all":
73-
return new Date(0);
74-
case "2h":
75-
return new Date(now.getTime() - 2 * 60 * 60 * 1000);
76-
case "24h":
77-
return new Date(now.getTime() - 24 * 60 * 60 * 1000);
78-
case "7d":
79-
return new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000);
80-
case "30d":
81-
return new Date(now.getTime() - 30 * 24 * 60 * 60 * 1000);
82-
default:
83-
throw new ApiError("Invalid range parameter", 400);
84-
}
85-
}
86-
8769
handleStatusChange = async (updatedMonitor: IMonitor, lastCheck: ICheck) => {
8870
if (updatedMonitor.status === "down") {
8971
const incident = await this.create(
@@ -216,7 +198,7 @@ class IncidentService implements IIncidentService {
216198
resolved?: boolean,
217199
resolutionType?: ResolutionType
218200
) => {
219-
const startDate = this.getStartDate(range);
201+
const startDate = getStartDate(range);
220202
const match = {
221203
teamId: teamId,
222204
...(monitorId && { monitorId }),

server/src/services/business/MonitorService.ts

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { IJobQueue } from "@/services/infrastructure/JobQueue.js";
77
import { MonitorWithChecksResponse } from "@/types/index.js";
88
import { MonitorStatus, MonitorType } from "@/db/models/monitors/Monitor.js";
99
import { Entitlements } from "@/types/entitlements.js";
10+
import { getStartDate } from "@/utils/TimeUtils.js";
1011

1112
export interface IImportedMonitor {
1213
name: string;
@@ -239,22 +240,6 @@ class MonitorService implements IMonitorService {
239240
return monitor;
240241
};
241242

242-
private getStartDate(range: string): Date {
243-
const now = new Date();
244-
switch (range) {
245-
case "1h":
246-
return new Date(now.getTime() - 1 * 60 * 60 * 1000);
247-
case "24h":
248-
return new Date(now.getTime() - 24 * 60 * 60 * 1000);
249-
case "7d":
250-
return new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000);
251-
case "30d":
252-
return new Date(now.getTime() - 30 * 24 * 60 * 60 * 1000);
253-
default:
254-
throw new ApiError("Invalid range parameter", 400);
255-
}
256-
}
257-
258243
private getBaseGroup = (): Record<string, any> => {
259244
return {
260245
_id: { $dateTrunc: { date: "$createdAt", unit: "minute" } },
@@ -681,7 +666,7 @@ class MonitorService implements IMonitorService {
681666
throw new ApiError("Monitor not found", 404);
682667
}
683668

684-
const startDate = this.getStartDate(range);
669+
const startDate = getStartDate(range);
685670

686671
if (range === "24h" || range === "7d" || range === "30d") {
687672
return await this.getEmbedChecksOtherRanges(monitor, range, startDate);

server/src/utils/TimeUtils.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import ApiError from "@/utils/ApiError.js";
2+
3+
export const getStartDate = (range: string): Date => {
4+
const now = new Date();
5+
switch (range) {
6+
case "all":
7+
return new Date(0);
8+
case "1h":
9+
return new Date(now.getTime() - 1 * 60 * 60 * 1000);
10+
case "24h":
11+
return new Date(now.getTime() - 24 * 60 * 60 * 1000);
12+
case "7d":
13+
return new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000);
14+
case "30d":
15+
return new Date(now.getTime() - 30 * 24 * 60 * 60 * 1000);
16+
default:
17+
throw new ApiError("Invalid range parameter", 400);
18+
}
19+
};

0 commit comments

Comments
 (0)