diff --git a/server/src/db/models/checks/Check.ts b/server/src/db/models/checks/Check.ts index c23fe888f..47c9f17a8 100644 --- a/server/src/db/models/checks/Check.ts +++ b/server/src/db/models/checks/Check.ts @@ -4,120 +4,14 @@ import { MonitorTypes, MonitorStatus, MonitorStatuses, + ISystemInfo, + ICaptureInfo, + ICheckLighthouseFields, + IDockerContainerHealth, + IDockerExposedPort, } from "@/types/domain/index.js"; import type { Response } from "got"; export type GotTimings = Response["timings"]; -export interface ITimingPhases { - wait: number; - dns: number; - tcp: number; - tls: number; - request: number; - firstByte: number; - download: number; - total: number; -} - -export interface ICpuInfo { - physical_core: number; - logical_core: number; - frequency: number; - current_frequency: number; - temperature: number[]; // per-core temps - free_percent: number; - usage_percent: number; -} - -export interface IMemoryInfo { - total_bytes: number; - available_bytes: number; - used_bytes: number; - usage_percent: number; -} - -export interface IHostInfo { - os?: string; - platform?: string; - kernel_version?: string; - pretty_name?: string; -} - -export interface IDiskInfo { - device: string; - total_bytes: number; - free_bytes: number; - used_bytes: number; - usage_percent: number; - total_inodes?: number; - free_inodes?: number; - used_inodes?: number; - inodes_usage_percent?: number; - read_bytes?: number; - write_bytes?: number; - read_time?: number; - write_time?: number; -} - -export interface INetInfo { - name: string; - bytes_sent: number; - bytes_recv: number; - packets_sent: number; - packets_recv: number; - err_in: number; - err_out: number; - drop_in: number; - drop_out: number; - fifo_in: number; - fifo_out: number; -} - -export interface ICaptureInfo { - version?: string; - mode?: string; -} - -export interface ISystemInfo { - cpu: ICpuInfo; - memory: IMemoryInfo; - disk: IDiskInfo[]; - host: IHostInfo; - net: INetInfo[]; -} - -export interface ILighthouseAudit { - id?: string; - title?: string; - score?: number | null; - displayValue?: string; - numericValue?: number; - numericUnit?: string; -} -export interface ILighthouseCategories { - accessibility?: { score?: number | null }; - "best-practices"?: { score?: number | null }; - seo?: { score?: number | null }; - performance?: { score?: number | null }; -} - -export interface ILighthouseResult { - categories?: ILighthouseCategories; - audits?: Record; -} - -export interface ICheckLighthouseFields { - accessibility: number; - bestPractices: number; - seo: number; - performance: number; - audits: { - cls: ILighthouseAudit; - si: ILighthouseAudit; - fcp: ILighthouseAudit; - lcp: ILighthouseAudit; - tbt: ILighthouseAudit; - }; -} export interface ICheck extends Document { _id: Types.ObjectId; @@ -143,18 +37,7 @@ export interface ICheck extends Document { expiry: Date; } -export interface IDockerContainerHealth { - healthy: boolean; - source?: string; - message?: string; -} - -export interface IDockerExposedPort { - port: string; - protocol: string; -} - -export interface IDockerContainerSummary { +interface IDockerContainerSummary { container_id: string; container_name: string; status: string; diff --git a/server/src/db/models/index.ts b/server/src/db/models/index.ts index b6c3c2ed5..e64d92fa7 100644 --- a/server/src/db/models/index.ts +++ b/server/src/db/models/index.ts @@ -1,12 +1,5 @@ export { Check } from "./checks/Check.js"; -export type { - ICheck, - ISystemInfo, - ICaptureInfo, - ILighthouseResult, - IDockerContainerSummary, -} from "./checks/Check.js"; - +export type { ICheck } from "./checks/Check.js"; export { Monitor } from "./monitors/Monitor.js"; export type { IMonitor } from "./monitors/Monitor.js"; diff --git a/server/src/db/models/monitors/Monitor.ts b/server/src/db/models/monitors/Monitor.ts index 47cde2a02..6f38235e9 100644 --- a/server/src/db/models/monitors/Monitor.ts +++ b/server/src/db/models/monitors/Monitor.ts @@ -1,5 +1,5 @@ import mongoose, { Schema, Document, Types } from "mongoose"; -import type { IDockerContainerSummary } from "@/db/models/checks/Check.js"; +import type { IDockerContainerSummary } from "@/types/domain/index.js"; import { Check, Incident, diff --git a/server/src/services/business/CheckService.ts b/server/src/services/business/CheckService.ts index 1b01d7f0c..d7cda9f2e 100644 --- a/server/src/services/business/CheckService.ts +++ b/server/src/services/business/CheckService.ts @@ -1,16 +1,19 @@ import { ISystemInfo } from "@/types/domain/index.js"; -import type { IDockerPayload } from "@/services/infrastructure/NetworkService.js"; +import type { + IDockerPayload, + IPageSpeedPayload, +} from "@/services/infrastructure/NetworkService.js"; import { MonitorStatus, MonitorType } from "@/types/domain/index.js"; import { StatusResponse } from "@/services/infrastructure/NetworkService.js"; -import type { - ICapturePayload, - ILighthousePayload, -} from "@/services/infrastructure/NetworkService.js"; +import type { ICapturePayload } from "@/services/infrastructure/NetworkService.js"; import ApiError from "@/utils/ApiError.js"; import { getChildLogger } from "@/logger/Logger.js"; import { getStartDate } from "@/utils/TimeUtils.js"; -import type { CheckEntity } from "@/types/domain/index.js"; +import type { + CheckEntity, + ICheckLighthouseFields, +} from "@/types/domain/index.js"; import type { IChecksRepository, IMonitorRepository, @@ -100,9 +103,7 @@ class CheckService implements ICheckService { return true; }; - private isPagespeedPayload = ( - payload: any - ): payload is ILighthousePayload => { + private isPagespeedPayload = (payload: any): payload is IPageSpeedPayload => { if (!payload || typeof payload !== "object") return false; if ( @@ -165,7 +166,7 @@ class CheckService implements ICheckService { }; private buildPagespeedCheck = ( - statusResponse: StatusResponse + statusResponse: StatusResponse ) => { if (!this.isPagespeedPayload(statusResponse.payload)) { throw new Error("Invalid payload for pagespeed monitor"); @@ -226,7 +227,7 @@ class CheckService implements ICheckService { case "pagespeed": return this.buildPagespeedCheck( - statusResponse as StatusResponse + statusResponse as StatusResponse ); case "http": case "https": diff --git a/server/src/services/infrastructure/NetworkService.ts b/server/src/services/infrastructure/NetworkService.ts index f4df4a38d..df7308c73 100644 --- a/server/src/services/infrastructure/NetworkService.ts +++ b/server/src/services/infrastructure/NetworkService.ts @@ -6,15 +6,14 @@ import type { Response } from "got"; import type { ISystemInfo, ICaptureInfo, - ILighthouseResult, -} from "@/db/models/index.js"; + IDockerContainerSummary, +} from "@/types/domain/index.js"; import type { TLSSocket } from "node:tls"; import { MonitorType, MonitorStatus } from "@/types/domain/index.js"; 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"; import type { Monitor } from "@/types/domain/monitor.js"; const SERVICE_NAME = "NetworkService"; @@ -33,15 +32,25 @@ export interface ICapturePayload { capture: ICaptureInfo; } -export interface ILighthousePayload { - lighthouseResult: ILighthouseResult; -} - export interface IDockerPayload { data: IDockerContainerSummary[]; capture: ICaptureInfo; } +interface ILighthouseCategories { + accessibility?: { score?: number | null }; + "best-practices"?: { score?: number | null }; + seo?: { score?: number | null }; + performance?: { score?: number | null }; +} + +export interface IPageSpeedPayload { + lighthouseResult: { + categories: ILighthouseCategories; + audits: Record; + }; +} + export interface StatusResponse { monitorId: string; teamId: string; @@ -235,7 +244,7 @@ class NetworkService implements INetworkService { throw new Error("No URL provided"); } - let statusResponse: StatusResponse; + let statusResponse: StatusResponse; try { const response: Response = await this.client(url); @@ -244,13 +253,13 @@ class NetworkService implements INetworkService { response, null, null - ) as StatusResponse; + ) as StatusResponse; } catch (error) { statusResponse = this.buildStatusResponse(monitor, null, null, error); } const pagespeedUrl = `https://pagespeedonline.googleapis.com/pagespeedonline/v5/runPagespeed?url=${url}&category=seo&category=accessibility&category=best-practices&category=performance&key=${apiKey}`; - const pagespeedResponse = await this.client( + const pagespeedResponse = await this.client( pagespeedUrl, { responseType: "json", diff --git a/server/src/services/infrastructure/StatusService.ts b/server/src/services/infrastructure/StatusService.ts index 98cc73f1b..475140325 100644 --- a/server/src/services/infrastructure/StatusService.ts +++ b/server/src/services/infrastructure/StatusService.ts @@ -1,4 +1,3 @@ -import { ICheck } from "@/db/models/index.js"; import { StatusResponse } from "./NetworkService.js"; import { IMonitorRepository,