diff --git a/packages/api/src/router/widgets/beszel.ts b/packages/api/src/router/widgets/beszel.ts index 2a2d08332..be83505a2 100644 --- a/packages/api/src/router/widgets/beszel.ts +++ b/packages/api/src/router/widgets/beszel.ts @@ -12,7 +12,7 @@ import { createManyIntegrationMiddleware } from "../../middlewares/integration"; import { createTRPCRouter, publicProcedure } from "../../trpc"; export const beszelRouter = createTRPCRouter({ - getSystems: publicProcedure.concat(createManyIntegrationMiddleware("query", "beszel")).query(async ({ ctx }) => { + getSystems: publicProcedure.concat(createManyIntegrationMiddleware("query", "beszel", "mock")).query(async ({ ctx }) => { const results = await Promise.all( ctx.integrations.map(async (integration) => { const innerHandler = beszelSystemsRequestHandler.handler(integration, {}); @@ -30,7 +30,7 @@ export const beszelRouter = createTRPCRouter({ }), subscribeSystems: publicProcedure - .concat(createManyIntegrationMiddleware("query", "beszel")) + .concat(createManyIntegrationMiddleware("query", "beszel", "mock")) .subscription(({ ctx }) => { return observable<{ integrationId: string; systems: BeszelSystemRow[]; timestamp: Date }>((emit) => { const unsubscribes = ctx.integrations.map((integration) => { @@ -50,7 +50,7 @@ export const beszelRouter = createTRPCRouter({ }), getAlerts: publicProcedure - .concat(createManyIntegrationMiddleware("query", "beszel")) + .concat(createManyIntegrationMiddleware("query", "beszel", "mock")) .input( z.object({ includeHistory: z.boolean().default(true), @@ -86,7 +86,7 @@ export const beszelRouter = createTRPCRouter({ }), subscribeAlerts: publicProcedure - .concat(createManyIntegrationMiddleware("query", "beszel")) + .concat(createManyIntegrationMiddleware("query", "beszel", "mock")) .input( z.object({ includeHistory: z.boolean().default(true), @@ -111,7 +111,7 @@ export const beszelRouter = createTRPCRouter({ }), getSystemStats: publicProcedure - .concat(createManyIntegrationMiddleware("query", "beszel")) + .concat(createManyIntegrationMiddleware("query", "beszel", "mock")) .input( z.object({ systemId: z.string(), diff --git a/packages/db/migrations/seed.ts b/packages/db/migrations/seed.ts index a2f19e59f..bac7ca56d 100644 --- a/packages/db/migrations/seed.ts +++ b/packages/db/migrations/seed.ts @@ -410,7 +410,12 @@ const buildDemoWidgets = (appIds: string[]): DemoWidget[] => [ options: { appId, openInNewTab: true, showTitle: true, pingEnabled: false }, }), ), - // Row 9: notebook + dockerContainers + weather = 12 + // Row 9: beszelSystemGrid + beszelAlerts = 12 + { kind: "beszelSystemGrid", width: 8, height: 3, needsIntegration: true }, + { kind: "beszelAlerts", width: 4, height: 3, needsIntegration: true }, + // Row 10: beszelSystemTable = 12 + { kind: "beszelSystemTable", width: 12, height: 3, needsIntegration: true }, + // Row 11: notebook + dockerContainers + weather = 12 { kind: "notebook", width: 4, height: 4, needsIntegration: false }, { kind: "dockerContainers", width: 6, height: 2, needsIntegration: false }, { kind: "weather", width: 2, height: 1, needsIntegration: false }, diff --git a/packages/integrations/src/mock/data/beszel.ts b/packages/integrations/src/mock/data/beszel.ts new file mode 100644 index 000000000..d14117518 --- /dev/null +++ b/packages/integrations/src/mock/data/beszel.ts @@ -0,0 +1,159 @@ +import type { + BeszelAlert, + BeszelAlertHistory, + BeszelContainerStats, + BeszelContainerStatsRecord, + BeszelSystem, + BeszelSystemDetails, + BeszelSystemStats, + BeszelSystemStatsRecord, +} from "../../beszel/beszel-types"; + +const mockSystems: { name: string; host: string; os: string; cpu: string; cores: number; mem: number }[] = [ + { name: "web-server", host: "192.168.1.10", os: "Ubuntu 24.04", cpu: "AMD Ryzen 9 7950X", cores: 16, mem: 64 * 1024 * 1024 * 1024 }, + { name: "nas-01", host: "192.168.1.20", os: "Debian 12", cpu: "Intel Xeon E-2388G", cores: 8, mem: 32 * 1024 * 1024 * 1024 }, + { name: "pi-cluster-1", host: "192.168.1.30", os: "Raspberry Pi OS", cpu: "ARM Cortex-A76", cores: 4, mem: 8 * 1024 * 1024 * 1024 }, + { name: "media-server", host: "192.168.1.40", os: "Proxmox VE 8.2", cpu: "Intel i7-13700K", cores: 16, mem: 128 * 1024 * 1024 * 1024 }, + { name: "backup-node", host: "192.168.1.50", os: "TrueNAS SCALE", cpu: "Intel Xeon E5-2680", cores: 12, mem: 48 * 1024 * 1024 * 1024 }, +]; + +const containerNames = ["nginx", "postgres", "redis", "grafana", "prometheus", "traefik", "minio", "gitea"]; + +const rand = (min: number, max: number) => min + Math.random() * (max - min); +const randInt = (min: number, max: number) => Math.floor(rand(min, max)); + +const BYTES_GB = 1024 * 1024 * 1024; +const BYTES_MB = 1024 * 1024; + +function generateSystemStats(baseLoad: number): BeszelSystemStats { + const cpuJitter = rand(-5, 5); + const memTotal = rand(8, 128) * BYTES_GB; + const memUsed = memTotal * rand(0.3, 0.85); + const diskTotal = rand(200, 2000) * BYTES_GB; + const diskUsed = diskTotal * rand(0.2, 0.7); + + return { + cpu: Math.max(0, Math.min(100, baseLoad + cpuJitter)), + la: [rand(0.1, 4), rand(0.1, 3), rand(0.1, 2)], + m: memTotal, + mu: memUsed, + mp: (memUsed / memTotal) * 100, + mb: rand(0.5, 4) * BYTES_GB, + s: 4 * BYTES_GB, + su: rand(0, 2) * BYTES_GB, + d: diskTotal, + du: diskUsed, + dp: (diskUsed / diskTotal) * 100, + dr: rand(0, 50) * BYTES_MB, + dw: rand(0, 30) * BYTES_MB, + ns: rand(100, 5000) * 1024, + nr: rand(100, 8000) * 1024, + b: [rand(50, 3000) * 1024, rand(100, 5000) * 1024], + }; +} + +function generateContainerStats(names: string[]): BeszelContainerStats[] { + return names.map((n) => ({ + n, + c: rand(0, 25), + m: rand(50, 500) * BYTES_MB, + b: [rand(0, 500) * 1024, rand(0, 800) * 1024], + })); +} + +function generateTimeSeries(count: number, intervalMinutes: number, generator: () => T): (T & { id: string; system: string; type: string; created: string; updated: string })[] { + const now = Date.now(); + return Array.from({ length: count }, (_, i) => ({ + id: `mock-stat-${i}`, + system: "mock-sys-1", + type: `${intervalMinutes}m`, + created: new Date(now - (count - i) * intervalMinutes * 60_000).toISOString(), + updated: new Date(now - (count - i) * intervalMinutes * 60_000).toISOString(), + ...generator(), + })); +} + +export class BeszelMockService { + public async getSystemsAsync(): Promise { + return mockSystems.map((sys, i) => ({ + id: `mock-sys-${i + 1}`, + name: sys.name, + host: sys.host, + port: "45876", + status: i === 2 ? "paused" as const : "up" as const, + info: { + h: sys.name, + cpu: rand(5, 85), + c: sys.cores, + ct: sys.cores * 2, + m: sys.cpu, + la: [rand(0.1, 4), rand(0.1, 3), rand(0.1, 2)] as [number, number, number], + o: sys.os, + u: randInt(3600, 2_000_000), + mp: rand(20, 85), + dp: rand(15, 70), + bb: rand(100, 5000) * 1024, + v: "0.8.2", + g: i === 3 ? rand(10, 60) : 0, + dt: rand(30, 65), + sv: [randInt(5, 30), randInt(30, 50)] as [number, number], + }, + users: [], + created: new Date().toISOString(), + updated: new Date().toISOString(), + })); + } + + public async getSystemDetailsAsync(systemId: string): Promise { + const idx = Number(systemId.replace("mock-sys-", "")) - 1; + const sys = mockSystems[idx] ?? mockSystems[0]!; + return { + id: systemId, + system: systemId, + hostname: sys.name, + kernel: "6.8.0-40-generic", + cores: sys.cores, + threads: sys.cores * 2, + cpu: sys.cpu, + os: 1, + os_name: sys.os, + arch: "amd64", + memory: sys.mem, + podman: false, + updated: new Date().toISOString(), + }; + } + + public async getSystemStatsAsync(_systemId: string, _type: string, perPage: number): Promise { + const baseLoad = rand(10, 60); + return generateTimeSeries(perPage, 1, () => ({ stats: generateSystemStats(baseLoad) })); + } + + public async getContainerStatsAsync(_systemId: string, _type: string, perPage: number): Promise { + const names = containerNames.slice(0, randInt(3, containerNames.length)); + return generateTimeSeries(perPage, 1, () => ({ stats: generateContainerStats(names) })); + } + + public async getAlertsAsync(): Promise { + return [ + { id: "mock-alert-1", user: "mock-user", system: "mock-sys-1", name: "CPU", triggered: true, value: 90, min: 5 }, + { id: "mock-alert-2", user: "mock-user", system: "mock-sys-2", name: "Memory", triggered: false, value: 80, min: 10 }, + { id: "mock-alert-3", user: "mock-user", system: "mock-sys-1", name: "Disk", triggered: false, value: 85, min: 15 }, + { id: "mock-alert-4", user: "mock-user", system: "mock-sys-4", name: "Temperature", triggered: true, value: 75, min: 3 }, + ]; + } + + public async getAlertHistoryAsync(_systemId?: string, perPage = 10): Promise { + const now = Date.now(); + return Array.from({ length: Math.min(perPage, 5) }, (_, i) => ({ + id: `mock-history-${i}`, + alert: `mock-alert-${(i % 4) + 1}`, + user: "mock-user", + system: `mock-sys-${(i % 5) + 1}`, + name: ["CPU", "Memory", "Disk", "Temperature", "Bandwidth"][i % 5]!, + val: rand(60, 95), + created: new Date(now - (i + 1) * 3_600_000).toISOString(), + resolved: i % 2 === 0 ? new Date(now - i * 1_800_000).toISOString() : null, + })); + } +} diff --git a/packages/integrations/src/mock/mock-integration.ts b/packages/integrations/src/mock/mock-integration.ts index b435dbd32..0804267b1 100644 --- a/packages/integrations/src/mock/mock-integration.ts +++ b/packages/integrations/src/mock/mock-integration.ts @@ -29,6 +29,7 @@ import { NetworkControllerSummaryMockService } from "./data/network-controller-s import { NotificationsMockService } from "./data/notifications"; import { SmartHomeMockService } from "./data/smart-home"; import { SystemHealthMonitoringMockService } from "./data/system-health-monitoring"; +import { BeszelMockService } from "./data/beszel"; import { UpsSummaryMockService } from "./data/ups-summary"; export class MockIntegration @@ -61,6 +62,7 @@ export class MockIntegration private static readonly networkController = new NetworkControllerSummaryMockService(); private static readonly notifications = new NotificationsMockService(); private static readonly smartHome = new SmartHomeMockService(); + private static readonly beszel = new BeszelMockService(); private static readonly upsSummary = new UpsSummaryMockService(); protected async testingAsync(_: IntegrationTestingInput): Promise { @@ -130,4 +132,12 @@ export class MockIntegration // UpsSummaryIntegration getUpsSummariesAsync = MockIntegration.upsSummary.getUpsSummariesAsync.bind(MockIntegration.upsSummary); + + // BeszelIntegration + getSystemsAsync = MockIntegration.beszel.getSystemsAsync.bind(MockIntegration.beszel); + getSystemDetailsAsync = MockIntegration.beszel.getSystemDetailsAsync.bind(MockIntegration.beszel); + getSystemStatsAsync = MockIntegration.beszel.getSystemStatsAsync.bind(MockIntegration.beszel); + getContainerStatsAsync = MockIntegration.beszel.getContainerStatsAsync.bind(MockIntegration.beszel); + getAlertsAsync = MockIntegration.beszel.getAlertsAsync.bind(MockIntegration.beszel); + getAlertHistoryAsync = MockIntegration.beszel.getAlertHistoryAsync.bind(MockIntegration.beszel); } diff --git a/packages/request-handler/src/beszel.ts b/packages/request-handler/src/beszel.ts index 20ec9f4bd..34a21d2d4 100644 --- a/packages/request-handler/src/beszel.ts +++ b/packages/request-handler/src/beszel.ts @@ -52,7 +52,7 @@ function mapToSystemRow(system: BeszelSystem, details: BeszelSystemDetails | nul export const beszelSystemsRequestHandler = createCachedIntegrationRequestHandler< BeszelSystemRow[], - "beszel", + "beszel" | "mock", Record >({ async requestAsync(integration) { @@ -77,7 +77,7 @@ export interface BeszelAlertsData { export const beszelAlertsRequestHandler = createCachedIntegrationRequestHandler< BeszelAlertsData, - "beszel", + "beszel" | "mock", { includeHistory: boolean; maxHistoryItems: number } >({ async requestAsync(integration, input) { @@ -105,7 +105,7 @@ export interface BeszelStatsData { export const beszelStatsRequestHandler = createCachedIntegrationRequestHandler< BeszelStatsData, - "beszel", + "beszel" | "mock", { systemId: string; timePeriod: string; includeDocker: boolean } >({ async requestAsync(integration, input) { diff --git a/packages/widgets/src/beszel-alerts/index.ts b/packages/widgets/src/beszel-alerts/index.ts index 9052d83ba..828c88b27 100644 --- a/packages/widgets/src/beszel-alerts/index.ts +++ b/packages/widgets/src/beszel-alerts/index.ts @@ -6,7 +6,7 @@ import { optionsBuilder } from "../options"; export const { definition, componentLoader } = createWidgetDefinition("beszelAlerts", { icon: IconBell, - supportedIntegrations: ["beszel"], + supportedIntegrations: ["beszel", "mock"], integrationsRequired: true, createOptions() { return optionsBuilder.from((factory) => ({ diff --git a/packages/widgets/src/beszel-system-grid/index.ts b/packages/widgets/src/beszel-system-grid/index.ts index 9de8c53db..fba380645 100644 --- a/packages/widgets/src/beszel-system-grid/index.ts +++ b/packages/widgets/src/beszel-system-grid/index.ts @@ -6,7 +6,7 @@ import { optionsBuilder } from "../options"; export const { definition, componentLoader } = createWidgetDefinition("beszelSystemGrid", { icon: IconLayoutGrid, - supportedIntegrations: ["beszel"], + supportedIntegrations: ["beszel", "mock"], integrationsRequired: true, createOptions() { return optionsBuilder.from((factory) => ({ diff --git a/packages/widgets/src/beszel-system-stats/index.ts b/packages/widgets/src/beszel-system-stats/index.ts index 0b0ecd027..1afff7465 100644 --- a/packages/widgets/src/beszel-system-stats/index.ts +++ b/packages/widgets/src/beszel-system-stats/index.ts @@ -13,7 +13,7 @@ const timePeriodOptions = [ export const { definition, componentLoader } = createWidgetDefinition("beszelSystemStats", { icon: IconChartAreaLine, - supportedIntegrations: ["beszel"], + supportedIntegrations: ["beszel", "mock"], integrationsRequired: true, createOptions() { return optionsBuilder.from((factory) => ({ diff --git a/packages/widgets/src/beszel-system-table/index.ts b/packages/widgets/src/beszel-system-table/index.ts index eb8e9a2a8..4e8f985c4 100644 --- a/packages/widgets/src/beszel-system-table/index.ts +++ b/packages/widgets/src/beszel-system-table/index.ts @@ -25,7 +25,7 @@ const sortDirectionOptions = [ export const { definition, componentLoader } = createWidgetDefinition("beszelSystemTable", { icon: IconTable, - supportedIntegrations: ["beszel"], + supportedIntegrations: ["beszel", "mock"], integrationsRequired: true, createOptions() { return optionsBuilder.from((factory) => ({