pingProvider, httpProvider
This commit is contained in:
1 parent
3c99809e8e
commit
82175f166f
11 files changed
+345
-57
No files matched your search
@@ -75,6 +75,9 @@ import {
|
|||||||
} from "@/repositories/index.js";
|
} from "@/repositories/index.js";
|
||||||
import { ILogger } from "@/utils/logger.js";
|
import { ILogger } from "@/utils/logger.js";
|
||||||
import { EnvConfig } from "@/service/system/settingsService.js";
|
import { EnvConfig } from "@/service/system/settingsService.js";
|
||||||
|
import { PingProvider } from "@/service/infrastructure/network/PingProvider.js";
|
||||||
|
import { HttpProvider } from "@/service/infrastructure/network/HttpProvider.js";
|
||||||
|
import { AdvancedMatcher } from "@/service/infrastructure/network/AdvancedMatcher.js";
|
||||||
|
|
||||||
export type InitializedServices = {
|
export type InitializedServices = {
|
||||||
settingsService: any;
|
settingsService: any;
|
||||||
@@ -144,6 +147,11 @@ export const initializeServices = async ({
|
|||||||
const teamsRepository = new MongoTeamsRepository();
|
const teamsRepository = new MongoTeamsRepository();
|
||||||
const maintenanceWindowsRepository = new MongoMaintenanceWindowsRepository();
|
const maintenanceWindowsRepository = new MongoMaintenanceWindowsRepository();
|
||||||
|
|
||||||
|
// Providers
|
||||||
|
|
||||||
|
const pingProvider = new PingProvider(ping);
|
||||||
|
const httpProvider = new HttpProvider(got, new AdvancedMatcher(jmespath));
|
||||||
|
|
||||||
const networkService = new NetworkService(
|
const networkService = new NetworkService(
|
||||||
axios,
|
axios,
|
||||||
got,
|
got,
|
||||||
@@ -158,7 +166,9 @@ export const initializeServices = async ({
|
|||||||
net,
|
net,
|
||||||
settingsService,
|
settingsService,
|
||||||
grpc,
|
grpc,
|
||||||
protoLoader
|
protoLoader,
|
||||||
|
pingProvider,
|
||||||
|
httpProvider
|
||||||
);
|
);
|
||||||
const emailService = new EmailService(settingsService, fs, path, compile, mjml2html, nodemailer, logger);
|
const emailService = new EmailService(settingsService, fs, path, compile, mjml2html, nodemailer, logger);
|
||||||
|
|
||||||
|
|||||||
@@ -20,12 +20,19 @@ export interface IChecksRepository {
|
|||||||
monitorId: string,
|
monitorId: string,
|
||||||
sortOrder: string,
|
sortOrder: string,
|
||||||
dateRange: string,
|
dateRange: string,
|
||||||
filter: string,
|
filter: string | undefined,
|
||||||
page: number,
|
page: number,
|
||||||
rowsPerPage: number,
|
rowsPerPage: number,
|
||||||
status: boolean | undefined
|
status: boolean | undefined
|
||||||
): Promise<ChecksQueryResult>;
|
): Promise<ChecksQueryResult>;
|
||||||
findByTeamId(sortOrder: string, dateRange: string, filter: string, page: number, rowsPerPage: number, teamId: string): Promise<ChecksQueryResult>;
|
findByTeamId(
|
||||||
|
sortOrder: string,
|
||||||
|
dateRange: string,
|
||||||
|
filter: string | undefined,
|
||||||
|
page: number,
|
||||||
|
rowsPerPage: number,
|
||||||
|
teamId: string
|
||||||
|
): Promise<ChecksQueryResult>;
|
||||||
findLatestByMonitorIds(monitorIds: string[], options?: { limitPerMonitor?: number }): Promise<LatestChecksMap>;
|
findLatestByMonitorIds(monitorIds: string[], options?: { limitPerMonitor?: number }): Promise<LatestChecksMap>;
|
||||||
findByDateRangeAndMonitorId(
|
findByDateRangeAndMonitorId(
|
||||||
monitorId: string,
|
monitorId: string,
|
||||||
|
|||||||
@@ -231,7 +231,7 @@ class MongoChecksRepository implements IChecksRepository {
|
|||||||
monitorId: string,
|
monitorId: string,
|
||||||
sortOrder: string,
|
sortOrder: string,
|
||||||
dateRange: string,
|
dateRange: string,
|
||||||
filter: string,
|
filter: string | undefined,
|
||||||
page: number,
|
page: number,
|
||||||
rowsPerPage: number,
|
rowsPerPage: number,
|
||||||
status: boolean | undefined
|
status: boolean | undefined
|
||||||
|
|||||||
@@ -124,8 +124,13 @@ class CheckService implements ICheckService {
|
|||||||
await this.monitorsRepository.findById(monitorId, teamId);
|
await this.monitorsRepository.findById(monitorId, teamId);
|
||||||
|
|
||||||
const { sortOrder, dateRange, filter, page, rowsPerPage, status } = query;
|
const { sortOrder, dateRange, filter, page, rowsPerPage, status } = query;
|
||||||
if (!sortOrder || !dateRange || !filter || !page || !rowsPerPage) {
|
if (!sortOrder || !dateRange || !page || !rowsPerPage) {
|
||||||
throw new AppError({ message: "Missing required query parameters", service: SERVICE_NAME, method: "getChecksByMonitor", status: 400 });
|
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 ? parseInt(page) : 0;
|
||||||
@@ -138,7 +143,7 @@ class CheckService implements ICheckService {
|
|||||||
|
|
||||||
getChecksByTeam = async ({ teamId, query }: { teamId: string; query: Record<string, string> }) => {
|
getChecksByTeam = async ({ teamId, query }: { teamId: string; query: Record<string, string> }) => {
|
||||||
const { sortOrder, dateRange, filter, page, rowsPerPage } = query;
|
const { sortOrder, dateRange, filter, page, rowsPerPage } = query;
|
||||||
if (!sortOrder || !dateRange || !filter || !page || !rowsPerPage) {
|
if (!sortOrder || !dateRange || !page || !rowsPerPage) {
|
||||||
throw new AppError({ message: "Missing required query parameters", service: SERVICE_NAME, method: "getChecksByTeam", status: 400 });
|
throw new AppError({ message: "Missing required query parameters", service: SERVICE_NAME, method: "getChecksByTeam", status: 400 });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,45 @@
|
|||||||
|
import { Monitor } from "@/types/monitor.js";
|
||||||
|
import jmespath from "jmespath";
|
||||||
|
type JmesPath = typeof jmespath;
|
||||||
|
|
||||||
|
export class AdvancedMatcher {
|
||||||
|
constructor(private jmespath: JmesPath) {}
|
||||||
|
|
||||||
|
private compare(actual: unknown, expected: string, method?: string): boolean {
|
||||||
|
if (method === "equal") return String(actual) === expected;
|
||||||
|
if (method === "include") return String(actual).includes(expected);
|
||||||
|
if (method === "regex") return new RegExp(expected).test(String(actual));
|
||||||
|
return String(actual) === expected; // Default
|
||||||
|
}
|
||||||
|
|
||||||
|
validate<T>(payload: T, monitor: Monitor): { ok: boolean; message: string; extracted?: any } {
|
||||||
|
const { useAdvancedMatching, jsonPath, matchMethod, expectedValue } = monitor;
|
||||||
|
if (!useAdvancedMatching) return { ok: true, message: "Success" };
|
||||||
|
|
||||||
|
let dataToValidate = payload;
|
||||||
|
|
||||||
|
if (jsonPath) {
|
||||||
|
try {
|
||||||
|
dataToValidate = this.jmespath.search(payload, jsonPath);
|
||||||
|
} catch {
|
||||||
|
return { ok: false, message: "Error evaluating JSON path" };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (expectedValue) {
|
||||||
|
const ok = this.compare(dataToValidate, expectedValue, matchMethod);
|
||||||
|
return {
|
||||||
|
ok,
|
||||||
|
message: ok ? "Success" : "Expected value did not match",
|
||||||
|
extracted: dataToValidate,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const isFalsey = dataToValidate === false || dataToValidate === "false" || dataToValidate === undefined || dataToValidate === null;
|
||||||
|
return {
|
||||||
|
ok: !isFalsey,
|
||||||
|
message: !isFalsey ? "Success" : "Extracted value is falsey",
|
||||||
|
extracted: dataToValidate,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,94 @@
|
|||||||
|
import { type Got, HTTPError, RequestError } from "got";
|
||||||
|
import { AdvancedMatcher } from "@/service/infrastructure/network/AdvancedMatcher.js";
|
||||||
|
import { IStatusProvider } from "@/service/infrastructure/network/IStatusProvider.js";
|
||||||
|
import { HttpStatusPayload } from "@/types/network.js";
|
||||||
|
import { MonitorStatusResponse } from "@/types/network.js";
|
||||||
|
import { Agent as HttpsAgent } from "https";
|
||||||
|
import { Monitor } from "@/types/monitor.js";
|
||||||
|
import { NETWORK_ERROR } from "@/service/infrastructure/network/utils.js";
|
||||||
|
|
||||||
|
export class HttpProvider implements IStatusProvider<HttpStatusPayload> {
|
||||||
|
readonly type = "http";
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
private got: Got,
|
||||||
|
private advancedMatcher: AdvancedMatcher
|
||||||
|
) {}
|
||||||
|
|
||||||
|
supports(type: string) {
|
||||||
|
return type === "http";
|
||||||
|
}
|
||||||
|
|
||||||
|
private handleHttpError<T>(error: unknown, monitor: Monitor): MonitorStatusResponse<T> {
|
||||||
|
if (error instanceof HTTPError || error instanceof RequestError) {
|
||||||
|
return {
|
||||||
|
monitorId: monitor.id,
|
||||||
|
teamId: monitor.teamId,
|
||||||
|
type: monitor.type,
|
||||||
|
status: false,
|
||||||
|
code: error.response?.statusCode ?? NETWORK_ERROR,
|
||||||
|
message: error.message,
|
||||||
|
responseTime: error.timings?.phases?.total ?? 0,
|
||||||
|
timings: error.timings,
|
||||||
|
payload: null as T,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
monitorId: monitor.id,
|
||||||
|
teamId: monitor.teamId,
|
||||||
|
type: monitor.type,
|
||||||
|
status: false,
|
||||||
|
code: NETWORK_ERROR,
|
||||||
|
message: error instanceof Error ? error.message : String(error),
|
||||||
|
responseTime: 0,
|
||||||
|
payload: null as T,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async handle<T>(monitor: Monitor): Promise<MonitorStatusResponse<T>> {
|
||||||
|
const { url, secret, ignoreTlsErrors } = monitor;
|
||||||
|
const options: Record<string, unknown> = {
|
||||||
|
headers: monitor.secret ? { Authorization: `Bearer ${secret}` } : undefined,
|
||||||
|
};
|
||||||
|
|
||||||
|
options.agent = {
|
||||||
|
https: new HttpsAgent({ rejectUnauthorized: !ignoreTlsErrors }),
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await this.got<string>(url, options);
|
||||||
|
let payload: T;
|
||||||
|
const isJson = response.headers["content-type"]?.includes("application/json");
|
||||||
|
|
||||||
|
if (isJson) {
|
||||||
|
try {
|
||||||
|
payload = JSON.parse(response.body) as T;
|
||||||
|
} catch {
|
||||||
|
payload = response.body as unknown as T;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
payload = response.body as unknown as T;
|
||||||
|
}
|
||||||
|
|
||||||
|
const matchResult = this.advancedMatcher.validate<T>(payload, monitor);
|
||||||
|
return {
|
||||||
|
monitorId: monitor.id,
|
||||||
|
teamId: monitor.teamId,
|
||||||
|
type: monitor.type,
|
||||||
|
status: response.ok && matchResult.ok,
|
||||||
|
code: response.statusCode,
|
||||||
|
message: matchResult.ok ? (response.statusMessage ?? "OK") : matchResult.message,
|
||||||
|
responseTime: response.timings.phases.total ?? 0,
|
||||||
|
timings: response.timings,
|
||||||
|
payload,
|
||||||
|
extracted: matchResult.extracted,
|
||||||
|
jsonPath: monitor.jsonPath,
|
||||||
|
matchMethod: monitor.matchMethod,
|
||||||
|
expectedValue: monitor.expectedValue,
|
||||||
|
};
|
||||||
|
} catch (error: unknown) {
|
||||||
|
return this.handleHttpError(error, monitor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
import { Monitor, MonitorType } from "@/types/monitor.js";
|
||||||
|
import { MonitorStatusResponse } from "@/types/network.js";
|
||||||
|
|
||||||
|
export interface IStatusProvider<T> {
|
||||||
|
type: string;
|
||||||
|
supports: (type: MonitorType) => boolean;
|
||||||
|
handle(monitor: Monitor): Promise<MonitorStatusResponse<T>>;
|
||||||
|
}
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
import { PingStatusPayload } from "@/types/network.js";
|
||||||
|
import { IStatusProvider } from "./IStatusProvider.js";
|
||||||
|
import { MonitorType, Monitor } from "@/types/monitor.js";
|
||||||
|
import { MonitorStatusResponse } from "@/types/network.js";
|
||||||
|
import { AppError } from "@/utils/AppError.js";
|
||||||
|
import ping from "ping";
|
||||||
|
import { buildStatusResponse, timeRequest } from "@/service/infrastructure/network/utils.js";
|
||||||
|
const SERVICE_NAME = "PingProvider";
|
||||||
|
|
||||||
|
type Ping = typeof ping;
|
||||||
|
|
||||||
|
export class PingProvider implements IStatusProvider<PingStatusPayload> {
|
||||||
|
readonly type = "ping";
|
||||||
|
|
||||||
|
constructor(private ping: Ping) {}
|
||||||
|
|
||||||
|
supports(type: MonitorType): boolean {
|
||||||
|
return type === "ping";
|
||||||
|
}
|
||||||
|
|
||||||
|
private sanitizeHost(url: string): string {
|
||||||
|
return url
|
||||||
|
.replace(/^https?:\/\//, "")
|
||||||
|
.replace(/\/.*$/, "")
|
||||||
|
.replace(/:.*/, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
async handle(monitor: Monitor): Promise<MonitorStatusResponse<PingStatusPayload>> {
|
||||||
|
try {
|
||||||
|
if (!monitor.url) {
|
||||||
|
throw new Error("URL is required for ping monitor");
|
||||||
|
}
|
||||||
|
|
||||||
|
const sanitizedHost = this.sanitizeHost(monitor.url);
|
||||||
|
const { response, error } = await timeRequest<PingStatusPayload>(() => this.ping.promise.probe(sanitizedHost));
|
||||||
|
const safeTime = typeof response?.time === "number" ? response.time : parseFloat(String(response?.time)) || 0;
|
||||||
|
if (error) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!response) {
|
||||||
|
throw new Error(`No response from ping for host: ${sanitizedHost}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return buildStatusResponse<PingStatusPayload>({
|
||||||
|
monitor,
|
||||||
|
payload: response,
|
||||||
|
overrides: {
|
||||||
|
status: response.alive ?? false,
|
||||||
|
code: 200,
|
||||||
|
message: "Success",
|
||||||
|
responseTime: safeTime,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} catch (err: unknown) {
|
||||||
|
const message = err instanceof Error ? err.message : String(err);
|
||||||
|
throw new AppError({
|
||||||
|
message,
|
||||||
|
service: SERVICE_NAME,
|
||||||
|
method: "handle",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,85 @@
|
|||||||
|
import { HTTPError, RequestError } from "got";
|
||||||
|
import type { Response } from "got";
|
||||||
|
import type { Monitor } from "@/types/monitor.js";
|
||||||
|
import { MonitorStatusResponse, MonitorStatusResponseOverrides } from "@/types/network.js";
|
||||||
|
|
||||||
|
export const timeRequest = async <T>(operation: () => Promise<T>): Promise<{ response: T | null; responseTime: number; error: unknown }> => {
|
||||||
|
const start = process.hrtime.bigint();
|
||||||
|
try {
|
||||||
|
const response = await operation();
|
||||||
|
const elapsedMs = Math.round(Number(process.hrtime.bigint() - start) / 1_000_000);
|
||||||
|
return { response, responseTime: elapsedMs, error: null };
|
||||||
|
} catch (error) {
|
||||||
|
const elapsedMs = Math.round(Number(process.hrtime.bigint() - start) / 1_000_000);
|
||||||
|
return { response: null, responseTime: elapsedMs, error };
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const NETWORK_ERROR = 5000;
|
||||||
|
export const PING_ERROR = 5001;
|
||||||
|
|
||||||
|
interface BuildStatusResponseArgs<T> {
|
||||||
|
monitor: Monitor;
|
||||||
|
response?: Response<T> | null;
|
||||||
|
error?: Error | RequestError | HTTPError | null;
|
||||||
|
payload?: T | null;
|
||||||
|
jsonPath?: string;
|
||||||
|
matchMethod?: MonitorStatusResponse["matchMethod"];
|
||||||
|
expectedValue?: string;
|
||||||
|
extracted?: unknown;
|
||||||
|
overrides?: MonitorStatusResponseOverrides<T>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const buildStatusResponse = <T>({
|
||||||
|
monitor,
|
||||||
|
response,
|
||||||
|
error,
|
||||||
|
payload,
|
||||||
|
jsonPath,
|
||||||
|
matchMethod,
|
||||||
|
expectedValue,
|
||||||
|
extracted,
|
||||||
|
overrides,
|
||||||
|
}: BuildStatusResponseArgs<T>): MonitorStatusResponse<T> => {
|
||||||
|
if (error) {
|
||||||
|
const statusResponse: MonitorStatusResponse<T> = {
|
||||||
|
monitorId: monitor.id,
|
||||||
|
teamId: monitor.teamId,
|
||||||
|
type: monitor.type,
|
||||||
|
status: false,
|
||||||
|
code: NETWORK_ERROR,
|
||||||
|
message: error.message ?? "Network error",
|
||||||
|
responseTime: 0,
|
||||||
|
timings: undefined,
|
||||||
|
jsonPath,
|
||||||
|
matchMethod,
|
||||||
|
expectedValue,
|
||||||
|
extracted,
|
||||||
|
payload,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (error instanceof HTTPError || error instanceof RequestError) {
|
||||||
|
statusResponse.code = error?.response?.statusCode ?? NETWORK_ERROR;
|
||||||
|
statusResponse.message = error.message;
|
||||||
|
statusResponse.responseTime = error.timings?.phases?.total ?? 0;
|
||||||
|
statusResponse.timings = error.timings;
|
||||||
|
}
|
||||||
|
return { ...statusResponse, ...(overrides ?? {}) };
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
monitorId: monitor.id,
|
||||||
|
teamId: monitor.teamId,
|
||||||
|
type: monitor.type,
|
||||||
|
status: response?.ok ?? false,
|
||||||
|
code: response?.statusCode ?? NETWORK_ERROR,
|
||||||
|
message: response?.statusMessage ?? "",
|
||||||
|
responseTime: response?.timings?.phases?.total ?? 0,
|
||||||
|
timings: response?.timings,
|
||||||
|
payload: payload ?? response?.body,
|
||||||
|
jsonPath,
|
||||||
|
matchMethod,
|
||||||
|
expectedValue,
|
||||||
|
extracted,
|
||||||
|
};
|
||||||
|
};
|
||||||
@@ -16,14 +16,14 @@ import type { AxiosStatic } from "axios";
|
|||||||
import { AppError } from "@/utils/AppError.js";
|
import { AppError } from "@/utils/AppError.js";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import { fileURLToPath } from "url";
|
import { fileURLToPath } from "url";
|
||||||
|
import { MonitorStatusResponseOverrides } from "@/types/index.js";
|
||||||
|
|
||||||
import CacheableLookup from "cacheable-lookup";
|
import CacheableLookup from "cacheable-lookup";
|
||||||
import { ISettingsService } from "../system/settingsService.js";
|
import { ISettingsService } from "../system/settingsService.js";
|
||||||
import { ILogger } from "@/utils/logger.js";
|
import { ILogger } from "@/utils/logger.js";
|
||||||
|
import { IStatusProvider } from "./network/IStatusProvider.js";
|
||||||
const SERVICE_NAME = "NetworkService";
|
const SERVICE_NAME = "NetworkService";
|
||||||
|
|
||||||
type MonitorStatusResponseOverrides<T> = Partial<Omit<MonitorStatusResponse<T>, "monitorId" | "teamId" | "type">>;
|
|
||||||
|
|
||||||
interface BuildStatusResponseArgs<T> {
|
interface BuildStatusResponseArgs<T> {
|
||||||
monitor: Monitor;
|
monitor: Monitor;
|
||||||
response?: Response<T> | null;
|
response?: Response<T> | null;
|
||||||
@@ -94,6 +94,10 @@ class NetworkService implements INetworkService {
|
|||||||
private grpc: typeof import("@grpc/grpc-js");
|
private grpc: typeof import("@grpc/grpc-js");
|
||||||
private protoLoader: typeof import("@grpc/proto-loader");
|
private protoLoader: typeof import("@grpc/proto-loader");
|
||||||
|
|
||||||
|
// New providers
|
||||||
|
private pingProvider;
|
||||||
|
private httpProvider;
|
||||||
|
|
||||||
private buildStatusResponse = <T>({
|
private buildStatusResponse = <T>({
|
||||||
monitor,
|
monitor,
|
||||||
response,
|
response,
|
||||||
@@ -160,7 +164,11 @@ class NetworkService implements INetworkService {
|
|||||||
net: typeof import("net"),
|
net: typeof import("net"),
|
||||||
settingsService: ISettingsService,
|
settingsService: ISettingsService,
|
||||||
grpc: typeof import("@grpc/grpc-js"),
|
grpc: typeof import("@grpc/grpc-js"),
|
||||||
protoLoader: typeof import("@grpc/proto-loader")
|
protoLoader: typeof import("@grpc/proto-loader"),
|
||||||
|
|
||||||
|
// New providers
|
||||||
|
pingProvider: IStatusProvider<PingStatusPayload>,
|
||||||
|
httpProvider: IStatusProvider<HttpStatusPayload>
|
||||||
) {
|
) {
|
||||||
this.TYPE_PING = "ping";
|
this.TYPE_PING = "ping";
|
||||||
this.TYPE_HTTP = "http";
|
this.TYPE_HTTP = "http";
|
||||||
@@ -185,6 +193,10 @@ class NetworkService implements INetworkService {
|
|||||||
this.grpc = grpc;
|
this.grpc = grpc;
|
||||||
this.protoLoader = protoLoader;
|
this.protoLoader = protoLoader;
|
||||||
|
|
||||||
|
// New providers
|
||||||
|
this.pingProvider = pingProvider;
|
||||||
|
this.httpProvider = httpProvider;
|
||||||
|
|
||||||
const cacheable = new CacheableLookup();
|
const cacheable = new CacheableLookup();
|
||||||
|
|
||||||
this.got = got.extend({
|
this.got = got.extend({
|
||||||
@@ -231,9 +243,9 @@ class NetworkService implements INetworkService {
|
|||||||
const type = monitor?.type || "unknown";
|
const type = monitor?.type || "unknown";
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case this.TYPE_PING:
|
case this.TYPE_PING:
|
||||||
return await this.requestPing(monitor);
|
return await this.pingProvider.handle(monitor);
|
||||||
case this.TYPE_HTTP:
|
case this.TYPE_HTTP:
|
||||||
return await this.requestHttp<HttpStatusPayload>(monitor);
|
return await this.httpProvider.handle(monitor);
|
||||||
case this.TYPE_PAGESPEED:
|
case this.TYPE_PAGESPEED:
|
||||||
return await this.requestPageSpeed(monitor);
|
return await this.requestPageSpeed(monitor);
|
||||||
case this.TYPE_HARDWARE:
|
case this.TYPE_HARDWARE:
|
||||||
@@ -251,50 +263,6 @@ class NetworkService implements INetworkService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async requestPing(monitor: Monitor): Promise<MonitorStatusResponse<PingStatusPayload>> {
|
|
||||||
try {
|
|
||||||
if (!monitor?.url) {
|
|
||||||
throw new Error("Monitor URL is required");
|
|
||||||
}
|
|
||||||
|
|
||||||
const rawUrl = monitor.url;
|
|
||||||
const sanitizedHost = rawUrl
|
|
||||||
.replace(/^https?:\/\//, "")
|
|
||||||
.replace(/\/.*$/, "")
|
|
||||||
.replace(/:.*/, "");
|
|
||||||
const { response, error } = await this.timeRequest(() => this.ping.promise.probe(sanitizedHost));
|
|
||||||
|
|
||||||
if (!response) {
|
|
||||||
if (error) {
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
throw new Error("Ping failed - no result returned");
|
|
||||||
}
|
|
||||||
|
|
||||||
const pingResponse = this.buildStatusResponse<PingStatusPayload>({
|
|
||||||
monitor,
|
|
||||||
payload: response as PingStatusPayload,
|
|
||||||
overrides: {
|
|
||||||
status: (response as { alive?: boolean })?.alive ?? false,
|
|
||||||
code: 200,
|
|
||||||
message: "Success",
|
|
||||||
responseTime: (response as { time?: number })?.time ?? 0,
|
|
||||||
payload: response as PingStatusPayload,
|
|
||||||
} as MonitorStatusResponseOverrides<PingStatusPayload>,
|
|
||||||
});
|
|
||||||
|
|
||||||
return pingResponse;
|
|
||||||
} catch (err: unknown) {
|
|
||||||
const originalMessage = err instanceof Error ? err.message : String(err);
|
|
||||||
throw new AppError({
|
|
||||||
message: originalMessage || "Error performing ping",
|
|
||||||
service: this.SERVICE_NAME,
|
|
||||||
method: "requestPing",
|
|
||||||
details: { url: monitor.url },
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private async requestHttp<T = unknown>(monitor: Monitor): Promise<MonitorStatusResponse<T>> {
|
private async requestHttp<T = unknown>(monitor: Monitor): Promise<MonitorStatusResponse<T>> {
|
||||||
const { url, secret, ignoreTlsErrors, useAdvancedMatching, jsonPath, matchMethod, expectedValue } = monitor;
|
const { url, secret, ignoreTlsErrors, useAdvancedMatching, jsonPath, matchMethod, expectedValue } = monitor;
|
||||||
const httpResponse = this.buildStatusResponse<T>({
|
const httpResponse = this.buildStatusResponse<T>({
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ export interface PingStatusPayload {
|
|||||||
host: string;
|
host: string;
|
||||||
numeric_host?: string;
|
numeric_host?: string;
|
||||||
alive: boolean;
|
alive: boolean;
|
||||||
time: number;
|
time: number | unknown;
|
||||||
times?: number[];
|
times?: number[];
|
||||||
output?: string;
|
output?: string;
|
||||||
min?: string;
|
min?: string;
|
||||||
@@ -134,3 +134,5 @@ export type StatusChangeResult = {
|
|||||||
temp: boolean;
|
temp: boolean;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type MonitorStatusResponseOverrides<T> = Partial<Omit<MonitorStatusResponse<T>, "monitorId" | "teamId" | "type">>;
|
||||||
Reference in new issue
Block a user