backend implementation
This commit is contained in:
1 parent
a250bf38aa
commit
378a40ce84
16 files changed
+777
-728
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import { ICheck, Check, Monitor, ISystemInfo } from "@/db/models/index.js";
|
||||
import type { IDockerPayload } from "@/services/infrastructure/NetworkService.js";
|
||||
import { MonitorStatus } from "@/db/models/monitors/Monitor.js";
|
||||
import { MonitorType } from "@/db/models/monitors/Monitor.js";
|
||||
import { StatusResponse } from "../infrastructure/NetworkService.js";
|
||||
@@ -98,6 +99,25 @@ class CheckService implements ICheckService {
|
||||
return true;
|
||||
};
|
||||
|
||||
private isDockerPayload = (payload: any): payload is IDockerPayload => {
|
||||
if (!payload || typeof payload !== "object") return false;
|
||||
if (!Array.isArray((payload as any).data)) return false;
|
||||
// Spot-check a couple of properties on the first element if present
|
||||
const first = (payload as any).data[0];
|
||||
if (first) {
|
||||
if (typeof first !== "object") return false;
|
||||
if (
|
||||
typeof first.container_id !== "string" ||
|
||||
typeof first.container_name !== "string"
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (!("capture" in payload) || typeof (payload as any).capture !== "object")
|
||||
return false;
|
||||
return true;
|
||||
};
|
||||
|
||||
private buildBaseCheck = (statusResponse: StatusResponse) => {
|
||||
const monitorId = new mongoose.Types.ObjectId(statusResponse.monitorId);
|
||||
const teamId = new mongoose.Types.ObjectId(statusResponse.teamId);
|
||||
@@ -159,6 +179,22 @@ class CheckService implements ICheckService {
|
||||
return check;
|
||||
};
|
||||
|
||||
private buildDockerCheck = (
|
||||
statusResponse: StatusResponse<IDockerPayload>
|
||||
) => {
|
||||
const code = statusResponse?.code;
|
||||
if (code && (code < 200 || code >= 300)) {
|
||||
throw new Error(statusResponse?.message || "Bad monitor response");
|
||||
}
|
||||
if (!this.isDockerPayload(statusResponse.payload)) {
|
||||
throw new Error("Invalid payload for docker monitor");
|
||||
}
|
||||
const check = this.buildBaseCheck(statusResponse);
|
||||
check.dockerContainers = statusResponse.payload.data || [];
|
||||
check.capture = statusResponse.payload.capture;
|
||||
return check;
|
||||
};
|
||||
|
||||
buildCheck = async (
|
||||
statusResponse: StatusResponse,
|
||||
type: MonitorType
|
||||
@@ -168,6 +204,10 @@ class CheckService implements ICheckService {
|
||||
return this.buildInfrastructureCheck(
|
||||
statusResponse as StatusResponse<ICapturePayload>
|
||||
);
|
||||
case "docker":
|
||||
return this.buildDockerCheck(
|
||||
statusResponse as StatusResponse<IDockerPayload>
|
||||
);
|
||||
|
||||
case "pagespeed":
|
||||
return this.buildPagespeedCheck(
|
||||
|
||||
@@ -15,6 +15,8 @@ import ApiError from "@/utils/ApiError.js";
|
||||
import { config } from "@/config/index.js";
|
||||
import CacheableLookup from "cacheable-lookup";
|
||||
import { getChildLogger } from "@/logger/Logger.js";
|
||||
import type { IDockerContainerSummary } from "@/db/models/index.js";
|
||||
|
||||
const SERVICE_NAME = "NetworkService";
|
||||
const logger = getChildLogger(SERVICE_NAME);
|
||||
export interface INetworkService {
|
||||
@@ -35,6 +37,11 @@ export interface ILighthousePayload {
|
||||
lighthouseResult: ILighthouseResult;
|
||||
}
|
||||
|
||||
export interface IDockerPayload {
|
||||
data: IDockerContainerSummary[];
|
||||
capture: ICaptureInfo;
|
||||
}
|
||||
|
||||
export interface StatusResponse<TPayload = unknown> {
|
||||
monitorId: string;
|
||||
teamId: string;
|
||||
@@ -359,6 +366,11 @@ class NetworkService implements INetworkService {
|
||||
return await this.requestHttp(monitor); // uses GOT
|
||||
case "infrastructure":
|
||||
return await this.requestInfrastructure(monitor); // uses GOT
|
||||
case "docker":
|
||||
// For now, docker monitors reuse the infrastructure capture protocol
|
||||
// (same capture agent and auth flow). This lets us ship server support
|
||||
// quickly; specialized parsing will be added in CheckService.
|
||||
return await this.requestInfrastructure(monitor);
|
||||
case "pagespeed":
|
||||
return await this.requestPagespeed(monitor); // uses GOT
|
||||
case "ping":
|
||||
|
||||
Reference in new issue
Block a user