diff --git a/client/src/components/monitors/BarDockerCount.tsx b/client/src/components/monitors/BarDockerCount.tsx index f2c51308e..5a0f39ae4 100644 --- a/client/src/components/monitors/BarDockerCount.tsx +++ b/client/src/components/monitors/BarDockerCount.tsx @@ -17,7 +17,7 @@ import { BaseBox } from "@/components/design-elements"; import { useAppSelector } from "@/hooks/AppHooks"; import { formatDateWithTz, tooltipDateFormatLookup } from "@/utils/TimeUtils"; import { useTranslation } from "react-i18next"; -import type { GroupedCheck } from "@/types/check"; +import type { AggregateCheck } from "@/types/check"; export const BarDockerCount = ({ data, dataKey, @@ -25,7 +25,7 @@ export const BarDockerCount = ({ title, emptyText, }: { - data: GroupedCheck[]; + data: AggregateCheck[]; dataKey: string; range: string; title: string; @@ -89,7 +89,7 @@ export const BarDockerCount = ({ vertical={false} /> } /> { const format = tickDateFormatLookup(range); const theme = useTheme(); const uiTimezone = useAppSelector((state: any) => state.ui.timezone); - console.log(payload); return ( { const theme = useTheme(); diff --git a/client/src/components/monitors/HistogramDockerPercentage.tsx b/client/src/components/monitors/HistogramDockerPercentage.tsx index 61a3eccc6..167355b9c 100644 --- a/client/src/components/monitors/HistogramDockerPercentage.tsx +++ b/client/src/components/monitors/HistogramDockerPercentage.tsx @@ -87,7 +87,7 @@ export const HistogramDockerPercentage = ({ } diff --git a/client/src/components/monitors/HistogramInfrastructure.tsx b/client/src/components/monitors/HistogramInfrastructure.tsx index 0a57a660c..58f88fd32 100644 --- a/client/src/components/monitors/HistogramInfrastructure.tsx +++ b/client/src/components/monitors/HistogramInfrastructure.tsx @@ -11,7 +11,7 @@ import { Fragment, useId } from "react"; import { XTick } from "./ChartResponseTime"; import { useTheme } from "@mui/material/styles"; -import type { GroupedCheck } from "@/types/check"; +import type { AggregateCheck } from "@/types/check"; const AREA_COLORS = [ // Blues @@ -104,7 +104,7 @@ export const HistogramInfrastructure = ({ title: string; type: string; idx: number | null; - checks: GroupedCheck[]; + checks: AggregateCheck[]; xKey: string; yDomain?: number[]; dataKeys: string[]; @@ -118,7 +118,7 @@ export const HistogramInfrastructure = ({ const theme = useTheme(); const uniqueId = useId(); - let avgTemps: { bucketDate: Date; avg_temp: number | null }[] = []; + let avgTemps: { bucketDate: string; avg_temp: number | null }[] = []; let tempYDomain: number[] = []; if (type === "temp") { avgTemps = checks.map((check) => { diff --git a/client/src/components/monitors/HistogramPageSpeedScores.tsx b/client/src/components/monitors/HistogramPageSpeedScores.tsx index 2b4138cc8..0442bff07 100644 --- a/client/src/components/monitors/HistogramPageSpeedScores.tsx +++ b/client/src/components/monitors/HistogramPageSpeedScores.tsx @@ -12,7 +12,7 @@ import { import { HistogramPageSpeedScoresTooltip } from "./HistogramPageSpeedScoresTooltip"; import { useTheme } from "@mui/material/styles"; -import type { GroupedCheck } from "@/types/check"; +import type { AggregateCheck } from "@/types/check"; import type { Palette } from "@mui/material/styles"; type PaletteColorKey = Extract< keyof Palette, @@ -52,7 +52,7 @@ export const HistogramPageSpeedScores = ({ checks, range, }: { - checks: GroupedCheck[]; + checks: AggregateCheck[]; range: string; }) => { const theme = useTheme(); diff --git a/client/src/components/monitors/HistogramStatus.tsx b/client/src/components/monitors/HistogramStatus.tsx index b4ad582e5..99602e64c 100644 --- a/client/src/components/monitors/HistogramStatus.tsx +++ b/client/src/components/monitors/HistogramStatus.tsx @@ -11,7 +11,7 @@ import { } from "recharts"; import { TrendingUp, AlertTriangle } from "lucide-react"; -import type { GroupedCheck } from "@/types/check"; +import type { AggregateCheck } from "@/types/check"; import type { MonitorStatus } from "@/types/monitor"; import { useTranslation } from "react-i18next"; @@ -26,8 +26,8 @@ const XLabel = ({ p2, range, }: { - p1: GroupedCheck; - p2: GroupedCheck; + p1: AggregateCheck; + p2: AggregateCheck; range: string; }) => { const theme = useTheme(); @@ -65,7 +65,7 @@ export const HistogramStatus = ({ range, title, }: { - checks: GroupedCheck[]; + checks: AggregateCheck[]; status: MonitorStatus; range: string; title: string; @@ -84,7 +84,7 @@ export const HistogramStatus = ({ const CustomTooltip = ({ active, payload }: any) => { const uiTimezone = useAppSelector((state: any) => state.ui.timezone); if (!active || !payload?.length) return null; - const d = payload[0]?.payload as GroupedCheck & { + const d = payload[0]?.payload as AggregateCheck & { avgResponseTime?: number; }; const avg = d?.avgResponseTime ?? 0; @@ -133,7 +133,7 @@ export const HistogramStatus = ({ } const totalChecks = normalChecks.reduce((count, check) => { - return count + check.count; + return count + (check.count ?? 0); }, 0); return ( diff --git a/client/src/pages/infrastructure/InfraDetailsGauges.tsx b/client/src/pages/infrastructure/InfraDetailsGauges.tsx index 781fe2f15..fe91879d4 100644 --- a/client/src/pages/infrastructure/InfraDetailsGauges.tsx +++ b/client/src/pages/infrastructure/InfraDetailsGauges.tsx @@ -7,7 +7,7 @@ import { useTranslation } from "react-i18next"; import { getGbs, getFrequency } from "./InfraUtils"; import { useTheme } from "@mui/material/styles"; import useMediaQuery from "@mui/material/useMediaQuery"; -import type { GroupedCheck } from "@/types/check"; +import type { AggregateCheck } from "@/types/check"; const InfraDetailGauge = ({ title, @@ -42,7 +42,11 @@ const InfraDetailGauge = ({ ); }; -export const InfraDetailsGauges = ({ checks }: { checks: GroupedCheck[] }) => { +export const InfraDetailsGauges = ({ + checks, +}: { + checks: AggregateCheck[]; +}) => { const { t } = useTranslation(); const theme = useTheme(); const isSmall = useMediaQuery(theme.breakpoints.down("md")); diff --git a/client/src/pages/infrastructure/InfraDetailsGraphs.tsx b/client/src/pages/infrastructure/InfraDetailsGraphs.tsx index ea46aa42c..269cff0f9 100644 --- a/client/src/pages/infrastructure/InfraDetailsGraphs.tsx +++ b/client/src/pages/infrastructure/InfraDetailsGraphs.tsx @@ -2,12 +2,12 @@ import Grid from "@mui/material/Grid"; import { HistogramInfrastructure } from "@/components/monitors"; import { useTranslation } from "react-i18next"; -import type { GroupedCheck } from "@/types/check"; +import type { AggregateCheck } from "@/types/check"; import { useMemo } from "react"; import { useTheme } from "@mui/material/styles"; import useMediaQuery from "@mui/material/useMediaQuery"; -const getChartConfigs = (theme: any, checks: GroupedCheck[], t: any) => { +const getChartConfigs = (theme: any, checks: AggregateCheck[], t: any) => { return [ { title: t("common.charts.infrastructure.memory.title"), @@ -52,7 +52,7 @@ export const InfraDetailsGraphs = ({ checks, range, }: { - checks: GroupedCheck[]; + checks: AggregateCheck[]; range: string; }) => { const theme = useTheme(); diff --git a/client/src/pages/infrastructure/InfraDetailsNetGraphs.tsx b/client/src/pages/infrastructure/InfraDetailsNetGraphs.tsx index 9879489ce..655a8f5ff 100644 --- a/client/src/pages/infrastructure/InfraDetailsNetGraphs.tsx +++ b/client/src/pages/infrastructure/InfraDetailsNetGraphs.tsx @@ -4,17 +4,18 @@ import Grid from "@mui/material/Grid"; import { HistogramInfrastructure } from "@/components/monitors"; import { Select } from "@/components/inputs"; -import type { IInfraCheck } from "@/types/check"; +import type { AggregateCheck } from "@/types/check"; import { useMemo, useState, useEffect } from "react"; import { useTheme } from "@mui/material/styles"; import useMediaQuery from "@mui/material/useMediaQuery"; const getChartConfigs = ( theme: any, - checks: IInfraCheck[], + checks: AggregateCheck[], selectedInterface: string ) => { - const idx = checks[0]?.net.findIndex((i) => i.name === selectedInterface); + const idx = + checks?.[0]?.net?.findIndex((i) => i.name === selectedInterface) ?? -1; if (idx === -1) return []; return [ { @@ -56,7 +57,7 @@ export const InfraDetailsNetGraphs = ({ checks, range, }: { - checks: IInfraCheck[]; + checks: AggregateCheck[]; range: string; }) => { const theme = useTheme(); @@ -68,11 +69,15 @@ export const InfraDetailsNetGraphs = ({ ); useEffect(() => { - if (checks[0]) { + if (checks?.[0]?.net && checks[0].net.length > 0) { const firstNonLoopback = checks[0].net.find( (iface) => iface.name !== "lo" ); - setSelectedInterface(firstNonLoopback?.name || ""); + setSelectedInterface( + firstNonLoopback?.name || checks[0].net[0]?.name || "" + ); + } else { + setSelectedInterface(""); } }, [checks]); return ( @@ -84,7 +89,7 @@ export const InfraDetailsNetGraphs = ({ value={selectedInterface} onChange={(e) => setSelectedInterface(e.target.value)} > - {checks[0]?.net.map((iface) => { + {checks?.[0]?.net?.map((iface) => { return ( {iface.name} @@ -108,7 +113,7 @@ export const InfraDetailsNetGraphs = ({ idx={config.idx} key={`${config.type}-${config.idx ?? ""}`} checks={checks} - xKey="_id" + xKey="bucketDate" yDomain={config.yDomain} dataKeys={config.dataKeys} gradient={true} diff --git a/client/src/types/check.ts b/client/src/types/check.ts index 951b0b835..283bce0a3 100644 --- a/client/src/types/check.ts +++ b/client/src/types/check.ts @@ -146,16 +146,28 @@ export interface CheckWithMonitor { updatedAt: string; timings: CheckTimings; } -export interface GroupedCheck { - bucketDate: Date; +export interface AggregateCheck { + bucketDate: string; + // Common aggregates count?: number; + upChecks?: number; + downChecks?: number; avgResponseTime?: number; + avgResponseTimeUp?: number; + avgResponseTimeDown?: number; + // Docker aggregates totalContainers?: number; runningContainers?: number; + runningPercent?: number; healthyContainers?: number; + healthyPercent?: number; + runningContainersSnapshot?: IDockerContainerSummary[]; + totalExposedPorts?: number; + uniqueImages?: number; + // PageSpeed aggregates accessibility?: number | null; bestPractices?: number | null; seo?: number | null; diff --git a/client/src/types/monitor.ts b/client/src/types/monitor.ts index d6b429217..21ac36308 100644 --- a/client/src/types/monitor.ts +++ b/client/src/types/monitor.ts @@ -1,4 +1,4 @@ -import type { GroupedCheck, ICheck } from "@/types/check"; +import type { AggregateCheck, ICheck } from "@/types/check"; import type { IMonitorStats } from "./monitorStats"; export const MonitorTypes = [ @@ -61,7 +61,7 @@ export interface IMonitorWithStats { } export interface IMonitorWithMonitorStats { - checks: GroupedCheck[]; + checks: AggregateCheck[]; monitor: IMonitor; stats: IMonitorStats; } diff --git a/server/src/repositories/checks/MongoCheckRepository.ts b/server/src/repositories/checks/MongoCheckRepository.ts index 6e0d97fb6..13641b860 100644 --- a/server/src/repositories/checks/MongoCheckRepository.ts +++ b/server/src/repositories/checks/MongoCheckRepository.ts @@ -48,7 +48,12 @@ class MongoCheckRepository implements IChecksRepository { avgResponseTimeDown: doc.avgResponseTimeDown, totalContainers: doc.totalContainers, runningContainers: doc.runningContainers, + runningPercent: doc.runningPercent, healthyContainers: doc.healthyContainers, + healthyPercent: doc.healthyPercent, + runningContainersSnapshot: doc.runningContainersSnapshot, + totalExposedPorts: doc.totalExposedPorts, + uniqueImages: doc.uniqueImages, accessibility: doc.accessibility, bestPractices: doc.bestPractices, seo: doc.seo, @@ -190,12 +195,53 @@ class MongoCheckRepository implements IChecksRepository { }, }, }, + runningContainersSnapshot: { $last: "$runningContainersSnapshot" }, + totalExposedPorts: { $last: "$totalExposedPorts" }, + uniqueImages: { $last: "$uniqueImages" }, }; }; private getDockerProjection = (): object => { const projectStage: any = { status: 1, responseTime: 1, createdAt: 1 }; projectStage["dockerContainers"] = 1; + projectStage["runningContainersSnapshot"] = { + $filter: { + input: { $ifNull: ["$dockerContainers", []] }, + as: "c", + cond: { $eq: ["$$c.running", true] }, + }, + }; + projectStage["totalExposedPorts"] = { + $sum: { + $map: { + input: { $ifNull: ["$dockerContainers", []] }, + as: "c", + in: { $size: { $ifNull: ["$$c.exposed_ports", []] } }, + }, + }, + }; + projectStage["uniqueImages"] = { + $size: { + $setUnion: [ + { + $filter: { + input: { + $map: { + input: { $ifNull: ["$dockerContainers", []] }, + as: "c", + in: "$$c.base_image", + }, + }, + as: "img", + cond: { + $and: [{ $ne: ["$$img", null] }, { $ne: ["$$img", ""] }], + }, + }, + }, + [], + ], + }, + }; return projectStage; }; @@ -516,6 +562,7 @@ class MongoCheckRepository implements IChecksRepository { .allowDiskUse(true) .hint({ "metadata.monitorId": 1, createdAt: 1 }); + console.log(checks); return checks.map(this.toAggregateCheck); }; diff --git a/server/src/types/domain/check.ts b/server/src/types/domain/check.ts index 25d4d20b8..ef27ffb4d 100644 --- a/server/src/types/domain/check.ts +++ b/server/src/types/domain/check.ts @@ -136,8 +136,6 @@ export interface CheckEntity { expiry: Date; } -// AggregateCheck reflects the minimal shape needed from aggregation results -// for list/history views, with string ids and optional docker payload. export interface AggregateCheck { // Time bucket identifier from aggregation (_id) bucketDate: Date; @@ -153,8 +151,12 @@ export interface AggregateCheck { // Docker aggregates (counts per bucket) totalContainers?: number; runningContainers?: number; + runningPercent?: number; healthyContainers?: number; - + healthyPercent?: number; + runningContainersSnapshot?: IDockerContainerSummary[]; + totalExposedPorts?: number; + uniqueImages?: number; // PageSpeed aggregates accessibility?: number | null; bestPractices?: number | null; @@ -166,8 +168,7 @@ export interface AggregateCheck { lcp?: number | null; tbt?: number | null; - // Infrastructure aggregates (optional snapshot/averages) - cpu?: Partial; + // Infrastructure aggregates memory?: Partial; disk?: Partial[]; host?: Partial;