validation fix, batch deleteOlderThan
This commit is contained in:
1 parent
77af9d2e93
commit
6ad0a6a29d
11 files changed
+155
-118
No files matched your search
@@ -37,6 +37,7 @@ import {
|
|||||||
} from "@/Features/UI/uiSlice.js";
|
} from "@/Features/UI/uiSlice.js";
|
||||||
import timezones from "@/Utils/timezones.json";
|
import timezones from "@/Utils/timezones.json";
|
||||||
import type { RootState } from "@/Types/state";
|
import type { RootState } from "@/Types/state";
|
||||||
|
import { CHECK_TTL_SENTINEL } from "@/Types/Check";
|
||||||
|
|
||||||
interface Timezone {
|
interface Timezone {
|
||||||
id: string;
|
id: string;
|
||||||
@@ -477,18 +478,18 @@ export const SettingsPage = () => {
|
|||||||
{...field}
|
{...field}
|
||||||
fieldLabel={t("pages.settings.form.retention.option.days.label")}
|
fieldLabel={t("pages.settings.form.retention.option.days.label")}
|
||||||
min={1}
|
min={1}
|
||||||
max={366}
|
max={CHECK_TTL_SENTINEL}
|
||||||
sliderMaxWidth={{ xs: "100%", md: "50%" }}
|
sliderMaxWidth={{ xs: "100%", md: "50%" }}
|
||||||
value={field.value || 30}
|
value={field.value || 30}
|
||||||
onChange={(_, value) => field.onChange(value)}
|
onChange={(_, value) => field.onChange(value)}
|
||||||
valueLabelDisplay="auto"
|
valueLabelDisplay="auto"
|
||||||
valueLabelFormat={(value: number) =>
|
valueLabelFormat={(value: number) =>
|
||||||
value >= 366
|
value >= CHECK_TTL_SENTINEL
|
||||||
? t("pages.settings.form.retention.option.days.unlimited")
|
? t("pages.settings.form.retention.option.days.unlimited")
|
||||||
: `${value}`
|
: `${value}`
|
||||||
}
|
}
|
||||||
formatDisplayValue={(value: number) =>
|
formatDisplayValue={(value: number) =>
|
||||||
value >= 366
|
value >= CHECK_TTL_SENTINEL
|
||||||
? t("pages.settings.form.retention.option.days.unlimited")
|
? t("pages.settings.form.retention.option.days.unlimited")
|
||||||
: `${value}`
|
: `${value}`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
export const CHECK_TTL_SENTINEL = 366;
|
||||||
|
|
||||||
export interface CheckMetadata {
|
export interface CheckMetadata {
|
||||||
monitorId: string;
|
monitorId: string;
|
||||||
teamId: string;
|
teamId: string;
|
||||||
@@ -125,8 +127,6 @@ export interface Check {
|
|||||||
timings?: CheckTimings;
|
timings?: CheckTimings;
|
||||||
statusCode: number;
|
statusCode: number;
|
||||||
message: string;
|
message: string;
|
||||||
ack: boolean;
|
|
||||||
ackAt?: string | null;
|
|
||||||
expiry: string;
|
expiry: string;
|
||||||
cpu?: CheckCpuInfo;
|
cpu?: CheckCpuInfo;
|
||||||
memory?: CheckMemoryInfo;
|
memory?: CheckMemoryInfo;
|
||||||
@@ -243,10 +243,7 @@ export interface ChecksSummary {
|
|||||||
downChecks: number;
|
downChecks: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type CheckSnapshot = Omit<
|
export type CheckSnapshot = Omit<Check, "metadata" | "expiry" | "__v" | "updatedAt"> & {
|
||||||
Check,
|
|
||||||
"metadata" | "ack" | "ackAt" | "expiry" | "__v" | "updatedAt"
|
|
||||||
> & {
|
|
||||||
originalResponseTime: number;
|
originalResponseTime: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { CHECK_TTL_SENTINEL } from "@/Types/Check";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
|
||||||
export const settingsSchema = z.object({
|
export const settingsSchema = z.object({
|
||||||
@@ -11,7 +12,11 @@ export const settingsSchema = z.object({
|
|||||||
systemEmailSecure: z.boolean().optional(),
|
systemEmailSecure: z.boolean().optional(),
|
||||||
systemEmailPool: z.boolean().optional(),
|
systemEmailPool: z.boolean().optional(),
|
||||||
showURL: z.boolean().optional(),
|
showURL: z.boolean().optional(),
|
||||||
checkTTL: z.number().int().min(1, "Please enter a value").max(366, "Maximum 366"),
|
checkTTL: z
|
||||||
|
.number()
|
||||||
|
.int()
|
||||||
|
.min(1, "Please enter a value")
|
||||||
|
.max(CHECK_TTL_SENTINEL, `Maximum ${CHECK_TTL_SENTINEL}`),
|
||||||
pagespeedApiKey: z
|
pagespeedApiKey: z
|
||||||
.string()
|
.string()
|
||||||
.transform((val) => (val.trim() === "" ? undefined : val.trim()))
|
.transform((val) => (val.trim() === "" ? undefined : val.trim()))
|
||||||
|
|||||||
@@ -4,18 +4,19 @@ import {
|
|||||||
getChecksParamValidation,
|
getChecksParamValidation,
|
||||||
getChecksQueryValidation,
|
getChecksQueryValidation,
|
||||||
getTeamChecksQueryValidation,
|
getTeamChecksQueryValidation,
|
||||||
|
getChecksSummaryByTeamIdQueryValidation,
|
||||||
deleteChecksParamValidation,
|
deleteChecksParamValidation,
|
||||||
deleteChecksByTeamIdParamValidation,
|
|
||||||
updateChecksTTLBodyValidation,
|
|
||||||
} from "@/validation/checkValidation.js";
|
} from "@/validation/checkValidation.js";
|
||||||
|
import { ICheckService } from "@/service/index.js";
|
||||||
|
import { requireTeamId } from "@/controllers/controllerUtils.js";
|
||||||
|
|
||||||
const SERVICE_NAME = "checkController";
|
const SERVICE_NAME = "checkController";
|
||||||
|
|
||||||
class CheckController {
|
class CheckController {
|
||||||
static SERVICE_NAME = SERVICE_NAME;
|
static SERVICE_NAME = SERVICE_NAME;
|
||||||
|
|
||||||
private checkService: any;
|
private checkService: ICheckService;
|
||||||
constructor(checkService: any) {
|
constructor(checkService: ICheckService) {
|
||||||
this.checkService = checkService;
|
this.checkService = checkService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -25,13 +26,20 @@ class CheckController {
|
|||||||
|
|
||||||
getChecksByMonitor = async (req: Request, res: Response, next: NextFunction) => {
|
getChecksByMonitor = async (req: Request, res: Response, next: NextFunction) => {
|
||||||
try {
|
try {
|
||||||
getChecksParamValidation.parse(req.params);
|
const validatedParams = getChecksParamValidation.parse(req.params);
|
||||||
getChecksQueryValidation.parse(req.query);
|
const validatedQuery = getChecksQueryValidation.parse(req.query);
|
||||||
|
|
||||||
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
|
|
||||||
const result = await this.checkService.getChecksByMonitor({
|
const result = await this.checkService.getChecksByMonitor({
|
||||||
monitorId: req?.params?.monitorId,
|
monitorId: validatedParams.monitorId,
|
||||||
query: req?.query,
|
teamId,
|
||||||
teamId: req?.user?.teamId,
|
sortOrder: validatedQuery.sortOrder,
|
||||||
|
dateRange: validatedQuery.dateRange,
|
||||||
|
filter: validatedQuery.filter,
|
||||||
|
page: validatedQuery.page,
|
||||||
|
rowsPerPage: validatedQuery.rowsPerPage,
|
||||||
|
status: validatedQuery.status,
|
||||||
});
|
});
|
||||||
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
@@ -46,10 +54,16 @@ class CheckController {
|
|||||||
|
|
||||||
getChecksByTeam = async (req: Request, res: Response, next: NextFunction) => {
|
getChecksByTeam = async (req: Request, res: Response, next: NextFunction) => {
|
||||||
try {
|
try {
|
||||||
getTeamChecksQueryValidation.parse(req.query);
|
const validatedQuery = getTeamChecksQueryValidation.parse(req.query);
|
||||||
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
|
|
||||||
const checkData = await this.checkService.getChecksByTeam({
|
const checkData = await this.checkService.getChecksByTeam({
|
||||||
teamId: req?.user?.teamId,
|
teamId,
|
||||||
query: req?.query,
|
sortOrder: validatedQuery.sortOrder,
|
||||||
|
dateRange: validatedQuery.dateRange,
|
||||||
|
page: validatedQuery.page,
|
||||||
|
rowsPerPage: validatedQuery.rowsPerPage,
|
||||||
|
filter: validatedQuery.filter,
|
||||||
});
|
});
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
@@ -63,8 +77,11 @@ class CheckController {
|
|||||||
|
|
||||||
getChecksSummaryByTeamId = async (req: Request, res: Response, next: NextFunction) => {
|
getChecksSummaryByTeamId = async (req: Request, res: Response, next: NextFunction) => {
|
||||||
try {
|
try {
|
||||||
const dateRange = req.query?.dateRange || "hour";
|
const validatedQuery = getChecksSummaryByTeamIdQueryValidation.parse(req.query);
|
||||||
const summary = await this.checkService.getChecksSummaryByTeamId({ teamId: req?.user?.teamId, dateRange });
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
|
const dateRange = validatedQuery.dateRange ?? "hour";
|
||||||
|
|
||||||
|
const summary = await this.checkService.getChecksSummaryByTeamId({ teamId, dateRange });
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
msg: "Checks summary retrieved successfully",
|
msg: "Checks summary retrieved successfully",
|
||||||
@@ -77,11 +94,12 @@ class CheckController {
|
|||||||
|
|
||||||
deleteChecks = async (req: Request, res: Response, next: NextFunction) => {
|
deleteChecks = async (req: Request, res: Response, next: NextFunction) => {
|
||||||
try {
|
try {
|
||||||
deleteChecksParamValidation.parse(req.params);
|
const validatedParams = deleteChecksParamValidation.parse(req.params);
|
||||||
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
|
|
||||||
const deletedCount = await this.checkService.deleteChecks({
|
const deletedCount = await this.checkService.deleteChecks({
|
||||||
monitorId: req.params.monitorId as string,
|
monitorId: validatedParams.monitorId,
|
||||||
teamId: req?.user?.teamId,
|
teamId,
|
||||||
});
|
});
|
||||||
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
@@ -96,9 +114,9 @@ class CheckController {
|
|||||||
|
|
||||||
deleteChecksByTeamId = async (req: Request, res: Response, next: NextFunction) => {
|
deleteChecksByTeamId = async (req: Request, res: Response, next: NextFunction) => {
|
||||||
try {
|
try {
|
||||||
deleteChecksByTeamIdParamValidation.parse(req.params);
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
|
|
||||||
const deletedCount = await this.checkService.deleteChecksByTeamId({ teamId: req?.user?.teamId });
|
const deletedCount = await this.checkService.deleteChecksByTeamId({ teamId });
|
||||||
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
@@ -109,24 +127,6 @@ class CheckController {
|
|||||||
next(error);
|
next(error);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
updateChecksTTL = async (req: Request, res: Response, next: NextFunction) => {
|
|
||||||
try {
|
|
||||||
updateChecksTTLBodyValidation.parse(req.body);
|
|
||||||
|
|
||||||
await this.checkService.updateChecksTTL({
|
|
||||||
teamId: req?.user?.teamId,
|
|
||||||
ttl: req?.body?.ttl,
|
|
||||||
});
|
|
||||||
|
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
msg: "Checks TTL updated successfully",
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default CheckController;
|
export default CheckController;
|
||||||
@@ -13,9 +13,10 @@ import type {
|
|||||||
GotTimings,
|
GotTimings,
|
||||||
MonitorType,
|
MonitorType,
|
||||||
} from "@/types/index.js";
|
} from "@/types/index.js";
|
||||||
import { CheckModel, MonitorModel, type CheckDocument } from "@/db/models/index.js";
|
import { CheckModel, type CheckDocument } from "@/db/models/index.js";
|
||||||
import mongoose from "mongoose";
|
import mongoose from "mongoose";
|
||||||
import { getDateForRange } from "@/utils/dataUtils.js";
|
import { getDateForRange } from "@/utils/dataUtils.js";
|
||||||
|
import { ILogger } from "@/utils/logger.js";
|
||||||
|
|
||||||
const SERVICE_NAME = "StatusService";
|
const SERVICE_NAME = "StatusService";
|
||||||
|
|
||||||
@@ -26,8 +27,8 @@ type HardwareUpChecks = { totalChecks: number };
|
|||||||
class MongoChecksRepository implements IChecksRepository {
|
class MongoChecksRepository implements IChecksRepository {
|
||||||
static SERVICE_NAME = SERVICE_NAME;
|
static SERVICE_NAME = SERVICE_NAME;
|
||||||
|
|
||||||
private logger: any;
|
private logger: ILogger;
|
||||||
constructor(logger: any) {
|
constructor(logger: ILogger) {
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -227,7 +228,7 @@ class MongoChecksRepository implements IChecksRepository {
|
|||||||
status: boolean | undefined
|
status: boolean | undefined
|
||||||
) => {
|
) => {
|
||||||
// Match
|
// Match
|
||||||
const matchStage: Record<string, any> = {
|
const matchStage: Record<string, unknown> = {
|
||||||
"metadata.monitorId": new mongoose.Types.ObjectId(monitorId),
|
"metadata.monitorId": new mongoose.Types.ObjectId(monitorId),
|
||||||
...(typeof status !== "undefined" && { status }),
|
...(typeof status !== "undefined" && { status }),
|
||||||
...(getDateForRange(dateRange) && {
|
...(getDateForRange(dateRange) && {
|
||||||
@@ -279,7 +280,7 @@ class MongoChecksRepository implements IChecksRepository {
|
|||||||
};
|
};
|
||||||
|
|
||||||
findByTeamId = async (sortOrder: string, dateRange: string, filter: string, page: number, rowsPerPage: number, teamId: string) => {
|
findByTeamId = async (sortOrder: string, dateRange: string, filter: string, page: number, rowsPerPage: number, teamId: string) => {
|
||||||
const matchStage: Record<string, any> = {
|
const matchStage: Record<string, unknown> = {
|
||||||
"metadata.teamId": new mongoose.Types.ObjectId(teamId),
|
"metadata.teamId": new mongoose.Types.ObjectId(teamId),
|
||||||
...(getDateForRange(dateRange) && {
|
...(getDateForRange(dateRange) && {
|
||||||
createdAt: {
|
createdAt: {
|
||||||
@@ -348,7 +349,7 @@ class MongoChecksRepository implements IChecksRepository {
|
|||||||
);
|
);
|
||||||
|
|
||||||
const mapped = results.reduce<LatestChecksMap>((acc, { monitorId, docs }) => {
|
const mapped = results.reduce<LatestChecksMap>((acc, { monitorId, docs }) => {
|
||||||
acc[monitorId] = docs.map((doc: any) => this.toEntity(doc as CheckDocument));
|
acc[monitorId] = docs.map((doc: CheckDocument) => this.toEntity(doc));
|
||||||
return acc;
|
return acc;
|
||||||
}, {});
|
}, {});
|
||||||
return mapped;
|
return mapped;
|
||||||
@@ -402,11 +403,31 @@ class MongoChecksRepository implements IChecksRepository {
|
|||||||
return result.deletedCount ?? 0;
|
return result.deletedCount ?? 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
deleteOlderThan = async (date: Date): Promise<number> => {
|
deleteOlderThan = async (cutoffDate: Date, batchDays: number = 30): Promise<number> => {
|
||||||
const result = await CheckModel.deleteMany({ createdAt: { $lt: date } });
|
// Find the oldest check that is older than the cutoff
|
||||||
return result.deletedCount ?? 0;
|
const oldest = await CheckModel.findOne({ createdAt: { $lt: cutoffDate } })
|
||||||
};
|
.sort({ createdAt: 1 })
|
||||||
|
.select({ createdAt: 1 });
|
||||||
|
if (!oldest?.createdAt) return 0;
|
||||||
|
|
||||||
|
let totalDeleted = 0;
|
||||||
|
let batchStart = new Date(oldest.createdAt);
|
||||||
|
const batchMs = batchDays * 24 * 60 * 60 * 1000;
|
||||||
|
|
||||||
|
// Delete in 30 day chunks until we reach the cutoff
|
||||||
|
while (batchStart < cutoffDate) {
|
||||||
|
// Advance startTime by batchMs to get to the end of last batch
|
||||||
|
const nextStart = batchStart.getTime() + batchMs;
|
||||||
|
const batchEnd = new Date(Math.min(nextStart, cutoffDate.getTime()));
|
||||||
|
const result = await CheckModel.deleteMany({
|
||||||
|
createdAt: { $gte: batchStart, $lt: batchEnd },
|
||||||
|
});
|
||||||
|
totalDeleted += result.deletedCount ?? 0;
|
||||||
|
batchStart = batchEnd;
|
||||||
|
}
|
||||||
|
|
||||||
|
return totalDeleted;
|
||||||
|
};
|
||||||
private findUptimeDateRangeChecks = async (
|
private findUptimeDateRangeChecks = async (
|
||||||
monitorType: Exclude<MonitorType, "hardware" | "pagespeed">,
|
monitorType: Exclude<MonitorType, "hardware" | "pagespeed">,
|
||||||
monitorObjectId: mongoose.Types.ObjectId,
|
monitorObjectId: mongoose.Types.ObjectId,
|
||||||
|
|||||||
@@ -15,7 +15,6 @@ class CheckRoutes {
|
|||||||
initRoutes() {
|
initRoutes() {
|
||||||
this.router.get("/team/summary", this.checkController.getChecksSummaryByTeamId);
|
this.router.get("/team/summary", this.checkController.getChecksSummaryByTeamId);
|
||||||
this.router.get("/team", this.checkController.getChecksByTeam);
|
this.router.get("/team", this.checkController.getChecksByTeam);
|
||||||
this.router.put("/team/ttl", isAllowed(["admin", "superadmin"]), this.checkController.updateChecksTTL);
|
|
||||||
this.router.delete("/team", isAllowed(["admin", "superadmin"]), this.checkController.deleteChecksByTeamId);
|
this.router.delete("/team", isAllowed(["admin", "superadmin"]), this.checkController.deleteChecksByTeamId);
|
||||||
this.router.get("/:monitorId", this.checkController.getChecksByMonitor);
|
this.router.get("/:monitorId", this.checkController.getChecksByMonitor);
|
||||||
this.router.delete("/:monitorId", this.checkController.deleteChecks);
|
this.router.delete("/:monitorId", this.checkController.deleteChecks);
|
||||||
|
|||||||
@@ -19,13 +19,28 @@ const SERVICE_NAME = "checkService";
|
|||||||
export interface ICheckService {
|
export interface ICheckService {
|
||||||
createChecks(checks: Check[]): Promise<Check[]>;
|
createChecks(checks: Check[]): Promise<Check[]>;
|
||||||
buildCheck(statusResponse: MonitorStatusResponse<MonitorPayloadMap[keyof MonitorPayloadMap]>): Check | undefined;
|
buildCheck(statusResponse: MonitorStatusResponse<MonitorPayloadMap[keyof MonitorPayloadMap]>): Check | undefined;
|
||||||
getChecksByMonitor(params: { monitorId: string; query: Record<string, string>; teamId: string }): Promise<ChecksQueryResult>;
|
getChecksByMonitor(params: {
|
||||||
getChecksByTeam(params: { teamId: string; query: Record<string, string> }): Promise<ChecksQueryResult>;
|
monitorId: string;
|
||||||
|
teamId: string;
|
||||||
|
sortOrder: string;
|
||||||
|
dateRange: string;
|
||||||
|
page: number;
|
||||||
|
rowsPerPage: number;
|
||||||
|
filter?: string;
|
||||||
|
status?: boolean;
|
||||||
|
}): Promise<ChecksQueryResult>;
|
||||||
|
getChecksByTeam(params: {
|
||||||
|
teamId: string;
|
||||||
|
sortOrder: string;
|
||||||
|
dateRange: string;
|
||||||
|
page: number;
|
||||||
|
rowsPerPage: number;
|
||||||
|
filter?: string;
|
||||||
|
}): Promise<ChecksQueryResult>;
|
||||||
getChecksSummaryByTeamId(params: { teamId: string; dateRange: string }): Promise<ChecksSummary>;
|
getChecksSummaryByTeamId(params: { teamId: string; dateRange: string }): Promise<ChecksSummary>;
|
||||||
deleteChecks(params: { monitorId: string; teamId: string }): Promise<number>;
|
deleteChecks(params: { monitorId: string; teamId: string }): Promise<number>;
|
||||||
deleteChecksByTeamId(params: { teamId: string }): Promise<number>;
|
deleteChecksByTeamId(params: { teamId: string }): Promise<number>;
|
||||||
deleteOlderThan(date: Date): Promise<number>;
|
deleteOlderThan(date: Date): Promise<number>;
|
||||||
updateChecksTTL(params: { teamId: string; ttl: string }): Promise<void>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class CheckService implements ICheckService {
|
export class CheckService implements ICheckService {
|
||||||
@@ -124,7 +139,25 @@ export class CheckService implements ICheckService {
|
|||||||
return check;
|
return check;
|
||||||
};
|
};
|
||||||
|
|
||||||
getChecksByMonitor = async ({ monitorId, query, teamId }: { monitorId: string; query: Record<string, string>; teamId: string }) => {
|
getChecksByMonitor = async ({
|
||||||
|
monitorId,
|
||||||
|
teamId,
|
||||||
|
sortOrder,
|
||||||
|
dateRange,
|
||||||
|
filter,
|
||||||
|
page,
|
||||||
|
rowsPerPage,
|
||||||
|
status,
|
||||||
|
}: {
|
||||||
|
monitorId: string;
|
||||||
|
teamId: string;
|
||||||
|
sortOrder: string;
|
||||||
|
dateRange: string;
|
||||||
|
page: number;
|
||||||
|
rowsPerPage: number;
|
||||||
|
status?: boolean;
|
||||||
|
filter?: string;
|
||||||
|
}) => {
|
||||||
if (!monitorId) {
|
if (!monitorId) {
|
||||||
throw new AppError({ message: "No monitor ID in request", service: SERVICE_NAME, method: "getChecksByMonitor", status: 400 });
|
throw new AppError({ message: "No monitor ID in request", service: SERVICE_NAME, method: "getChecksByMonitor", status: 400 });
|
||||||
}
|
}
|
||||||
@@ -135,69 +168,49 @@ export class CheckService implements ICheckService {
|
|||||||
// For verification, throws an error if monitor doesn't belong to team
|
// For verification, throws an error if monitor doesn't belong to team
|
||||||
await this.monitorsRepository.findById(monitorId, teamId);
|
await this.monitorsRepository.findById(monitorId, teamId);
|
||||||
|
|
||||||
const { sortOrder, dateRange, filter, page, rowsPerPage, status } = query;
|
|
||||||
if (!sortOrder || !dateRange || !page || !rowsPerPage) {
|
|
||||||
throw new AppError({
|
|
||||||
message: `Missing required query parameters sortOrder: ${sortOrder}, dateRange: ${dateRange}, filter: ${filter}, page: ${page}, rowsPerPage: ${rowsPerPage}`,
|
|
||||||
service: SERVICE_NAME,
|
|
||||||
method: "getChecksByMonitor",
|
|
||||||
status: 400,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
const parsedStatus = typeof status === "undefined" ? status : ParseBoolean(status);
|
const parsedStatus = typeof status === "undefined" ? status : ParseBoolean(status);
|
||||||
const parsedPage = page ? parseInt(page) : 0;
|
const parsedPage = page ? page : 0;
|
||||||
const parsedRowsPerPage = rowsPerPage ? parseInt(rowsPerPage) : 5;
|
const parsedRowsPerPage = rowsPerPage ? rowsPerPage : 5;
|
||||||
|
|
||||||
const result = await this.checksRepository.findByMonitorId(monitorId, sortOrder, dateRange, filter, parsedPage, parsedRowsPerPage, parsedStatus);
|
const result = await this.checksRepository.findByMonitorId(monitorId, sortOrder, dateRange, filter, parsedPage, parsedRowsPerPage, parsedStatus);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
getChecksByTeam = async ({ teamId, query }: { teamId: string; query: Record<string, string> }) => {
|
getChecksByTeam = async ({
|
||||||
const { sortOrder, dateRange, filter, page, rowsPerPage } = query;
|
teamId,
|
||||||
if (!sortOrder || !dateRange || !page || !rowsPerPage) {
|
sortOrder,
|
||||||
throw new AppError({ message: "Missing required query parameters", service: SERVICE_NAME, method: "getChecksByTeam", status: 400 });
|
dateRange,
|
||||||
}
|
filter,
|
||||||
|
page,
|
||||||
if (!teamId) {
|
rowsPerPage,
|
||||||
throw new AppError({ message: "No team ID in request", service: SERVICE_NAME, method: "getChecksByTeam", status: 400 });
|
}: {
|
||||||
}
|
teamId: string;
|
||||||
|
sortOrder: string;
|
||||||
const parsedPage = page ? parseInt(page) : 0;
|
dateRange: string;
|
||||||
const parsedRowsPerPage = rowsPerPage ? parseInt(rowsPerPage) : 5;
|
page: number;
|
||||||
|
rowsPerPage: number;
|
||||||
|
filter?: string;
|
||||||
|
}) => {
|
||||||
|
const parsedPage = page ? page : 0;
|
||||||
|
const parsedRowsPerPage = rowsPerPage ? rowsPerPage : 5;
|
||||||
|
|
||||||
const checkData = await this.checksRepository.findByTeamId(sortOrder, dateRange, filter, parsedPage, parsedRowsPerPage, teamId);
|
const checkData = await this.checksRepository.findByTeamId(sortOrder, dateRange, filter, parsedPage, parsedRowsPerPage, teamId);
|
||||||
return checkData;
|
return checkData;
|
||||||
};
|
};
|
||||||
|
|
||||||
getChecksSummaryByTeamId = async ({ teamId, dateRange }: { teamId: string; dateRange: string }) => {
|
getChecksSummaryByTeamId = async ({ teamId, dateRange }: { teamId: string; dateRange: string }) => {
|
||||||
if (!teamId) {
|
|
||||||
throw new AppError({ message: "No team ID in request", service: SERVICE_NAME, method: "getChecksSummaryByTeamId", status: 400 });
|
|
||||||
}
|
|
||||||
const summary = await this.checksRepository.findSummaryByTeamId(teamId, dateRange);
|
const summary = await this.checksRepository.findSummaryByTeamId(teamId, dateRange);
|
||||||
return summary;
|
return summary;
|
||||||
};
|
};
|
||||||
|
|
||||||
deleteChecks = async ({ monitorId, teamId }: { monitorId: string; teamId: string }) => {
|
deleteChecks = async ({ monitorId, teamId }: { monitorId: string; teamId: string }) => {
|
||||||
if (!monitorId) {
|
|
||||||
throw new AppError({ message: "No monitor ID in request", service: SERVICE_NAME, method: "deleteChecks", status: 400 });
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!teamId) {
|
|
||||||
throw new AppError({ message: "No team ID in request", service: SERVICE_NAME, method: "deleteChecks", status: 400 });
|
|
||||||
}
|
|
||||||
|
|
||||||
// For verification, throws an error if monitor doesn't belong to team
|
|
||||||
await this.monitorsRepository.findById(monitorId, teamId);
|
await this.monitorsRepository.findById(monitorId, teamId);
|
||||||
|
|
||||||
const deletedCount = await this.checksRepository.deleteByMonitorId(monitorId);
|
const deletedCount = await this.checksRepository.deleteByMonitorId(monitorId);
|
||||||
return deletedCount;
|
return deletedCount;
|
||||||
};
|
};
|
||||||
deleteChecksByTeamId = async ({ teamId }: { teamId: string }) => {
|
deleteChecksByTeamId = async ({ teamId }: { teamId: string }) => {
|
||||||
if (!teamId) {
|
|
||||||
throw new AppError({ message: "No team ID in request", service: SERVICE_NAME, method: "deleteChecksByTeamId", status: 400 });
|
|
||||||
}
|
|
||||||
|
|
||||||
const deletedCount = await this.checksRepository.deleteByTeamId(teamId);
|
const deletedCount = await this.checksRepository.deleteByTeamId(teamId);
|
||||||
return deletedCount;
|
return deletedCount;
|
||||||
};
|
};
|
||||||
@@ -210,8 +223,4 @@ export class CheckService implements ICheckService {
|
|||||||
const deletedCount = await this.checksRepository.deleteOlderThan(date);
|
const deletedCount = await this.checksRepository.deleteOlderThan(date);
|
||||||
return deletedCount;
|
return deletedCount;
|
||||||
};
|
};
|
||||||
|
|
||||||
updateChecksTTL = async ({ teamId, ttl }: { teamId: string; ttl: string }) => {
|
|
||||||
throw new AppError({ message: "Not implemented", service: SERVICE_NAME, method: "updateChecksTTL", status: 500, details: { teamId, ttl } });
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
@@ -11,7 +11,7 @@ import {
|
|||||||
IncidentService,
|
IncidentService,
|
||||||
type IGeoChecksService,
|
type IGeoChecksService,
|
||||||
} from "@/service/index.js";
|
} from "@/service/index.js";
|
||||||
import type { MaintenanceWindow, StatusChangeResult } from "@/types/index.js";
|
import { CHECK_TTL_SENTINEL, type MaintenanceWindow, type StatusChangeResult } from "@/types/index.js";
|
||||||
import {
|
import {
|
||||||
IMaintenanceWindowsRepository,
|
IMaintenanceWindowsRepository,
|
||||||
IMonitorsRepository,
|
IMonitorsRepository,
|
||||||
@@ -391,7 +391,7 @@ export class SuperSimpleQueueHelper implements ISuperSimpleQueueHelper {
|
|||||||
|
|
||||||
const checkTTL = settings.checkTTL; // Check TTL is in DAYS, not MS
|
const checkTTL = settings.checkTTL; // Check TTL is in DAYS, not MS
|
||||||
|
|
||||||
if (checkTTL === 366) {
|
if (checkTTL === CHECK_TTL_SENTINEL) {
|
||||||
this.logger.info({
|
this.logger.info({
|
||||||
message: `Check TTL is set to unlimited, skipping cleanup`,
|
message: `Check TTL is set to unlimited, skipping cleanup`,
|
||||||
service: SERVICE_NAME,
|
service: SERVICE_NAME,
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
import type { MonitorType } from "@/types/index.js";
|
import type { MonitorType } from "@/types/index.js";
|
||||||
import type { Response } from "got";
|
import type { Response } from "got";
|
||||||
|
|
||||||
|
export const CHECK_TTL_SENTINEL = 366;
|
||||||
|
|
||||||
export type GotTimings = Response["timings"];
|
export type GotTimings = Response["timings"];
|
||||||
|
|
||||||
export interface CheckMetadata {
|
export interface CheckMetadata {
|
||||||
|
|||||||
@@ -12,32 +12,34 @@ export const getChecksParamValidation = z.object({
|
|||||||
|
|
||||||
export const getChecksQueryValidation = z.object({
|
export const getChecksQueryValidation = z.object({
|
||||||
type: z.enum(["http", "ping", "pagespeed", "hardware", "docker", "port", "game", "grpc", "websocket"]).optional(),
|
type: z.enum(["http", "ping", "pagespeed", "hardware", "docker", "port", "game", "grpc", "websocket"]).optional(),
|
||||||
sortOrder: z.enum(["asc", "desc"]).optional(),
|
sortOrder: z.enum(["asc", "desc"]),
|
||||||
limit: z.coerce.number().optional(),
|
limit: z.coerce.number().optional(),
|
||||||
dateRange: z.enum(["recent", "hour", "day", "week", "month", "all"]).optional(),
|
dateRange: z.enum(["recent", "hour", "day", "week", "month", "all"]),
|
||||||
filter: z.enum(["all", "up", "down", "resolve"]).optional(),
|
filter: z.enum(["all", "up", "down", "resolve"]).optional(),
|
||||||
ack: booleanCoercion.optional(),
|
ack: booleanCoercion.optional(),
|
||||||
page: z.coerce.number().optional(),
|
page: z.coerce.number(),
|
||||||
rowsPerPage: z.coerce.number().optional(),
|
rowsPerPage: z.coerce.number(),
|
||||||
status: booleanCoercion.optional(),
|
status: booleanCoercion.optional(),
|
||||||
continent: z.union([z.enum(GeoContinents), z.array(z.enum(GeoContinents))]).optional(),
|
continent: z.union([z.enum(GeoContinents), z.array(z.enum(GeoContinents))]).optional(),
|
||||||
});
|
});
|
||||||
|
|
||||||
export const getTeamChecksQueryValidation = z.object({
|
export const getTeamChecksQueryValidation = z.object({
|
||||||
sortOrder: z.enum(["asc", "desc"]).optional(),
|
sortOrder: z.enum(["asc", "desc"]),
|
||||||
limit: z.coerce.number().optional(),
|
limit: z.coerce.number().optional(),
|
||||||
dateRange: z.enum(["recent", "hour", "day", "week", "month", "all"]).optional(),
|
dateRange: z.enum(["recent", "hour", "day", "week", "month", "all"]),
|
||||||
filter: z.enum(["all", "up", "down", "resolve"]).optional(),
|
filter: z.enum(["all", "up", "down", "resolve"]).optional(),
|
||||||
ack: booleanCoercion.optional(),
|
ack: booleanCoercion.optional(),
|
||||||
page: z.coerce.number().optional(),
|
page: z.coerce.number(),
|
||||||
rowsPerPage: z.coerce.number().optional(),
|
rowsPerPage: z.coerce.number(),
|
||||||
});
|
});
|
||||||
|
|
||||||
export const deleteChecksParamValidation = z.object({
|
export const deleteChecksParamValidation = z.object({
|
||||||
monitorId: z.string().min(1, "Monitor ID is required"),
|
monitorId: z.string().min(1, "Monitor ID is required"),
|
||||||
});
|
});
|
||||||
|
|
||||||
export const deleteChecksByTeamIdParamValidation = z.object({});
|
export const getChecksSummaryByTeamIdQueryValidation = z.object({
|
||||||
|
dateRange: z.enum(["recent", "hour", "day", "week", "month", "all"]).optional(),
|
||||||
|
});
|
||||||
|
|
||||||
export const updateChecksTTLBodyValidation = z.object({
|
export const updateChecksTTLBodyValidation = z.object({
|
||||||
ttl: z.number().min(1, "TTL is required"),
|
ttl: z.number().min(1, "TTL is required"),
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { CHECK_TTL_SENTINEL } from "@/types/check.js";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
|
||||||
//****************************************
|
//****************************************
|
||||||
@@ -6,9 +7,8 @@ import { z } from "zod";
|
|||||||
|
|
||||||
export const updateAppSettingsBodyValidation = z
|
export const updateAppSettingsBodyValidation = z
|
||||||
.object({
|
.object({
|
||||||
checkTTL: z.union([z.number().int().min(1).max(366), z.literal("")]).optional(),
|
checkTTL: z.union([z.number().int().min(1).max(CHECK_TTL_SENTINEL), z.literal("")]).optional(),
|
||||||
systemEmailPort: z.union([z.number(), z.literal("")]).optional(),
|
systemEmailPort: z.union([z.number(), z.literal("")]).optional(),
|
||||||
|
|
||||||
pagespeedApiKey: z.union([z.string(), z.literal("")]).optional(),
|
pagespeedApiKey: z.union([z.string(), z.literal("")]).optional(),
|
||||||
language: z.union([z.string(), z.literal("")]).optional(),
|
language: z.union([z.string(), z.literal("")]).optional(),
|
||||||
timezone: z.union([z.string(), z.literal("")]).optional(),
|
timezone: z.union([z.string(), z.literal("")]).optional(),
|
||||||
|
|||||||
Reference in new issue
Block a user