refactor(monitor): address PR feedback for customUpCodes
Add HttpStatusCode types and simplify provider logic
This commit is contained in:
1 parent
87a6640c1d
commit
372fc9869b
5 files changed
+23
-12
No files matched your search
@@ -1,5 +1,5 @@
|
|||||||
import type { GeoContinent, GeoCheckResult, GeoCheckTimings, GeoCheckLocation } from "@/types/geoCheck.js";
|
import type { GeoContinent, GeoCheckResult, GeoCheckTimings, GeoCheckLocation } from "@/types/geoCheck.js";
|
||||||
import { supportsGeoCheck } from "@/types/monitor.js";
|
import { supportsGeoCheck, type HttpStatusCode } from "@/types/monitor.js";
|
||||||
import { MonitorType } from "@/types/index.js";
|
import { MonitorType } from "@/types/index.js";
|
||||||
import type { ILogger } from "@/utils/logger.js";
|
import type { ILogger } from "@/utils/logger.js";
|
||||||
import got from "got";
|
import got from "got";
|
||||||
@@ -62,7 +62,7 @@ interface GlobalPingProbeResult {
|
|||||||
export interface IGlobalPingService {
|
export interface IGlobalPingService {
|
||||||
readonly serviceName: string;
|
readonly serviceName: string;
|
||||||
createMeasurement(monitorType: MonitorType, url: string, locations: GeoContinent[]): Promise<string | null>;
|
createMeasurement(monitorType: MonitorType, url: string, locations: GeoContinent[]): Promise<string | null>;
|
||||||
pollForResults(measurementId: string, timeoutMs?: number, customUpCodes?: number[]): Promise<GeoCheckResult[]>;
|
pollForResults(measurementId: string, timeoutMs?: number, customUpCodes?: HttpStatusCode[]): Promise<GeoCheckResult[]>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class GlobalPingService implements IGlobalPingService {
|
export class GlobalPingService implements IGlobalPingService {
|
||||||
@@ -119,7 +119,11 @@ export class GlobalPingService implements IGlobalPingService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async pollForResults(measurementId: string, timeoutMs: number = MAX_POLL_TIMEOUT_MS, customUpCodes: number[] = []): Promise<GeoCheckResult[]> {
|
async pollForResults(
|
||||||
|
measurementId: string,
|
||||||
|
timeoutMs: number = MAX_POLL_TIMEOUT_MS,
|
||||||
|
customUpCodes: HttpStatusCode[] = []
|
||||||
|
): Promise<GeoCheckResult[]> {
|
||||||
const startTime = Date.now();
|
const startTime = Date.now();
|
||||||
|
|
||||||
while (Date.now() - startTime < timeoutMs) {
|
while (Date.now() - startTime < timeoutMs) {
|
||||||
@@ -174,7 +178,7 @@ export class GlobalPingService implements IGlobalPingService {
|
|||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
private transformResults(probeResults: GlobalPingProbeResult[], customUpCodes: number[] = []): GeoCheckResult[] {
|
private transformResults(probeResults: GlobalPingProbeResult[], customUpCodes: HttpStatusCode[] = []): GeoCheckResult[] {
|
||||||
const successfulResults: GeoCheckResult[] = [];
|
const successfulResults: GeoCheckResult[] = [];
|
||||||
|
|
||||||
for (const probeResult of probeResults) {
|
for (const probeResult of probeResults) {
|
||||||
|
|||||||
@@ -31,14 +31,15 @@ export class HttpProvider implements IStatusProvider<HttpStatusPayload> {
|
|||||||
|
|
||||||
private handleHttpError<T>(error: unknown, monitor: Monitor): MonitorStatusResponse<T> {
|
private handleHttpError<T>(error: unknown, monitor: Monitor): MonitorStatusResponse<T> {
|
||||||
if (error instanceof HTTPError || error instanceof RequestError) {
|
if (error instanceof HTTPError || error instanceof RequestError) {
|
||||||
const isCustomUp = error instanceof HTTPError && (monitor.customUpCodes ?? []).includes(error.response?.statusCode);
|
const statusCode = error.response?.statusCode;
|
||||||
|
const statusUp = statusCode !== undefined && monitor.customUpCodes.includes(statusCode);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
monitorId: monitor.id,
|
monitorId: monitor.id,
|
||||||
teamId: monitor.teamId,
|
teamId: monitor.teamId,
|
||||||
type: monitor.type,
|
type: monitor.type,
|
||||||
status: isCustomUp,
|
status: statusUp,
|
||||||
code: error.response?.statusCode ?? NETWORK_ERROR,
|
code: statusCode ?? NETWORK_ERROR,
|
||||||
message: error.message,
|
message: error.message,
|
||||||
responseTime: error.timings?.phases?.total ?? 0,
|
responseTime: error.timings?.phases?.total ?? 0,
|
||||||
timings: error.timings,
|
timings: error.timings,
|
||||||
@@ -104,11 +105,12 @@ export class HttpProvider implements IStatusProvider<HttpStatusPayload> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const matchResult = this.advancedMatcher.validate<T>(payload, monitor);
|
const matchResult = this.advancedMatcher.validate<T>(payload, monitor);
|
||||||
|
const statusUp = monitor.customUpCodes.includes(response.statusCode) || response.ok;
|
||||||
return {
|
return {
|
||||||
monitorId: monitor.id,
|
monitorId: monitor.id,
|
||||||
teamId: monitor.teamId,
|
teamId: monitor.teamId,
|
||||||
type: monitor.type,
|
type: monitor.type,
|
||||||
status: ((monitor.customUpCodes ?? []).includes(response.statusCode) || response.ok) && matchResult.ok,
|
status: statusUp && matchResult.ok,
|
||||||
code: response.statusCode,
|
code: response.statusCode,
|
||||||
message: matchResult.ok ? (response.statusMessage ?? "OK") : matchResult.message,
|
message: matchResult.ok ? (response.statusMessage ?? "OK") : matchResult.message,
|
||||||
responseTime: response.timings.phases.total ?? 0,
|
responseTime: response.timings.phases.total ?? 0,
|
||||||
|
|||||||
@@ -2,6 +2,11 @@ import type { CheckSnapshot } from "@/types/check.js";
|
|||||||
export type { CheckSnapshot } from "@/types/check.js";
|
export type { CheckSnapshot } from "@/types/check.js";
|
||||||
import type { GeoContinent, GroupedGeoCheck } from "@/types/geoCheck.js";
|
import type { GeoContinent, GroupedGeoCheck } from "@/types/geoCheck.js";
|
||||||
export type { GeoContinent } from "@/types/geoCheck.js";
|
export type { GeoContinent } from "@/types/geoCheck.js";
|
||||||
|
import http from "node:http";
|
||||||
|
|
||||||
|
export const HttpStatusCodes = Object.keys(http.STATUS_CODES).map(Number);
|
||||||
|
export const HttpStatusCodeSet = new Set(HttpStatusCodes);
|
||||||
|
export type HttpStatusCode = number;
|
||||||
|
|
||||||
export const MonitorTypes = ["http", "ping", "pagespeed", "hardware", "docker", "port", "game", "grpc", "websocket", "dns", "unknown"] as const;
|
export const MonitorTypes = ["http", "ping", "pagespeed", "hardware", "docker", "port", "game", "grpc", "websocket", "dns", "unknown"] as const;
|
||||||
export type MonitorType = (typeof MonitorTypes)[number];
|
export type MonitorType = (typeof MonitorTypes)[number];
|
||||||
@@ -60,7 +65,7 @@ export interface Monitor {
|
|||||||
uptimePercentage?: number;
|
uptimePercentage?: number;
|
||||||
notifications: string[];
|
notifications: string[];
|
||||||
tags: string[];
|
tags: string[];
|
||||||
customUpCodes: number[];
|
customUpCodes: HttpStatusCode[];
|
||||||
secret?: string;
|
secret?: string;
|
||||||
cpuAlertThreshold: number;
|
cpuAlertThreshold: number;
|
||||||
cpuAlertCounter: number;
|
cpuAlertCounter: number;
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { booleanCoercion, dnsHostnameRegex, dnsServerValidation } from "./shared.js";
|
import { booleanCoercion, dnsHostnameRegex, dnsServerValidation } from "./shared.js";
|
||||||
import { GeoContinents } from "@/types/geoCheck.js";
|
import { GeoContinents } from "@/types/geoCheck.js";
|
||||||
import { DnsRecordTypes, MonitorMatchMethods, MonitorStatuses, MonitorTypes, PageSpeedStrategies } from "@/types/monitor.js";
|
import { DnsRecordTypes, HttpStatusCodeSet, MonitorMatchMethods, MonitorStatuses, MonitorTypes, PageSpeedStrategies } from "@/types/monitor.js";
|
||||||
import http from "node:http";
|
|
||||||
|
|
||||||
const httpStatusCode = z.number().refine((code) => code.toString() in http.STATUS_CODES, { message: "Must be a valid HTTP status code" });
|
const httpStatusCode = z.number().refine((code) => HttpStatusCodeSet.has(code), { message: "Must be a valid HTTP status code" });
|
||||||
|
|
||||||
export const getMonitorByIdParamValidation = z.object({
|
export const getMonitorByIdParamValidation = z.object({
|
||||||
monitorId: z.string().min(1, "Monitor ID is required"),
|
monitorId: z.string().min(1, "Monitor ID is required"),
|
||||||
|
|||||||
@@ -56,6 +56,7 @@ const makeMonitor = (overrides?: Partial<Monitor>): Monitor =>
|
|||||||
useAdvancedMatching: false,
|
useAdvancedMatching: false,
|
||||||
matchMethod: undefined,
|
matchMethod: undefined,
|
||||||
expectedValue: undefined,
|
expectedValue: undefined,
|
||||||
|
customUpCodes: [],
|
||||||
...overrides,
|
...overrides,
|
||||||
}) as Monitor;
|
}) as Monitor;
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user