tighten server types

This commit is contained in:
Alex Holliday committed 2026-05-26 14:53:33 -07:00
1 parent 290adf024c
commit e834402672
3 files changed
+191 -135

No files matched your search

+3 -2
View File
@@ -1,3 +1,4 @@
import { Mongoose } from "mongoose";
import MongoDB from "../db/MongoDB.js"; import MongoDB from "../db/MongoDB.js";
import { IDb } from "@/db/IDb.js"; import { IDb } from "@/db/IDb.js";
import { import {
@@ -125,7 +126,7 @@ import { ILogger } from "@/utils/logger.js";
export type InitializedServices = { export type InitializedServices = {
settingsService: ISettingsService; settingsService: ISettingsService;
db: IDb; db: IDb<Mongoose>;
networkService: INetworkService; networkService: INetworkService;
emailService: IEmailService; emailService: IEmailService;
bufferService: IBufferService; bufferService: IBufferService;
@@ -173,7 +174,7 @@ export const initializeServices = async ({
}): Promise<InitializedServices> => { }): Promise<InitializedServices> => {
// Create DB // Create DB
let db: IDb | null = null; let db: IDb<Mongoose> | null = null;
db = new MongoDB(logger, envSettings); db = new MongoDB(logger, envSettings);
await db.connect(); await db.connect();
@@ -6,16 +6,39 @@ import { AppError } from "@/utils/AppError.js";
const SERVICE_NAME = "diagnosticService"; const SERVICE_NAME = "diagnosticService";
export interface CollectionDiagnostics {
name: string;
documentCount: number;
storageSize: number;
totalIndexSize: number;
totalSize: number;
bucketCount?: number;
}
export interface MongoDiagnostics {
readyState: number;
host: string;
port: number;
dbName: string;
totalSize: number;
collections: CollectionDiagnostics[];
}
export interface Diagnostics {
osStats: { totalMemoryBytes: number };
cpuUsage: { usagePercentage: number };
v8HeapStats: {
totalHeapSizeBytes: number;
usedHeapSizeBytes: number;
heapSizeLimitBytes: number;
};
eventLoopDelayMs: number;
uptimeMs: number;
mongoStats: MongoDiagnostics;
}
export interface IDiagnosticService { export interface IDiagnosticService {
getCPUUsage(): Promise<{ userUsageMs: number; systemUsageMs: number; usagePercentage: number }>; getSystemStats(): Promise<Diagnostics>;
getSystemStats(): Promise<{
osStats: { freeMemoryBytes: number; totalMemoryBytes: number };
memoryUsage: Record<keyof NodeJS.MemoryUsage, number>;
cpuUsage: { userUsageMs: number; systemUsageMs: number; usagePercentage: number };
v8HeapStats: { totalHeapSizeBytes: number; usedHeapSizeBytes: number; heapSizeLimitBytes: number };
eventLoopDelayMs: number;
uptimeMs: number;
}>;
} }
export class DiagnosticService implements IDiagnosticService { export class DiagnosticService implements IDiagnosticService {
@@ -37,117 +60,86 @@ export class DiagnosticService implements IDiagnosticService {
return DiagnosticService.SERVICE_NAME; return DiagnosticService.SERVICE_NAME;
} }
getCPUUsage = async () => { private getCPUUsagePercentage = async (): Promise<number> => {
const startUsage = process.cpuUsage(); const startUsage = process.cpuUsage();
const timingPeriod = 1000; // measured in ms const timingPeriod = 1000; // measured in ms
await new Promise((resolve) => setTimeout(resolve, timingPeriod)); await new Promise((resolve) => setTimeout(resolve, timingPeriod));
const endUsage = process.cpuUsage(startUsage); const endUsage = process.cpuUsage(startUsage);
const cpuUsage = { return ((endUsage.user + endUsage.system) / 1000 / timingPeriod) * 100;
userUsageMs: endUsage.user / 1000,
systemUsageMs: endUsage.system / 1000,
usagePercentage: ((endUsage.user + endUsage.system) / 1000 / timingPeriod) * 100,
};
return cpuUsage;
}; };
private getMongoDBStats = async () => { private getMongoDBStats = async (): Promise<MongoDiagnostics> => {
const mongo = await this.db.getConnection(); const mongo = await this.db.getConnection();
const db = mongo.connection.db; const db = mongo.connection.db;
if (!db) { if (!db) {
throw new AppError({ message: "Database connection is not available", service: SERVICE_NAME, method: "getMongoDBStats" }); throw new AppError({ message: "Database connection is not available", service: SERVICE_NAME, method: "getMongoDBStats" });
} }
const readyState = mongo.connection.readyState; const dbStats = await db.stats();
const host = mongo.connection.host; const rawCollections = await db.collections();
const port = mongo.connection.port;
const dbName = mongo.connection.name;
const serverStatus = await db.admin().serverStatus(); const collections: CollectionDiagnostics[] = await Promise.all(
const serverInfo = await db.admin().serverInfo(); rawCollections
.filter((collection) => !collection.collectionName.startsWith("system"))
const stats = await db.stats(); .map(async (collection) => {
const collections = await db.collections(); const stats = await db.command({ collStats: collection.collectionName });
const documentCount = await db.collection(collection.collectionName).countDocuments({});
const collectionStats = await Promise.all( const entry: CollectionDiagnostics = {
(collections || []).map(async (collection) => { name: collection.collectionName,
const stats = await db.command({ collStats: collection.collectionName }); documentCount,
const documentCount = await db.collection(collection.collectionName).countDocuments({}); storageSize: stats.storageSize,
return { name: collection.collectionName, documentCount, ...stats }; totalIndexSize: stats.totalIndexSize,
}) totalSize: stats.totalSize,
};
if (stats.timeseries?.bucketCount !== undefined) {
entry.bucketCount = stats.timeseries.bucketCount;
}
return entry;
})
); );
return { return {
readyState, readyState: mongo.connection.readyState,
host, host: mongo.connection.host,
port, port: mongo.connection.port,
dbName, dbName: mongo.connection.name,
serverStatus, totalSize: dbStats.totalSize,
serverInfo, collections,
stats,
collectionStats,
}; };
}; };
getSystemStats = async () => { getSystemStats = async (): Promise<Diagnostics> => {
// Memory Usage const osStats = { totalMemoryBytes: os.totalmem() };
const totalMemory = os.totalmem();
const freeMemory = os.freemem();
const osStats = { const cpuUsage = { usagePercentage: await this.getCPUUsagePercentage() };
freeMemoryBytes: freeMemory, // bytes
totalMemoryBytes: totalMemory, // bytes
};
const used = process.memoryUsage();
// In MB
const memoryUsage: Record<keyof NodeJS.MemoryUsage, number> = {
rss: Math.round((used.rss / 1024 / 1024) * 100) / 100,
heapTotal: Math.round((used.heapTotal / 1024 / 1024) * 100) / 100,
heapUsed: Math.round((used.heapUsed / 1024 / 1024) * 100) / 100,
external: Math.round((used.external / 1024 / 1024) * 100) / 100,
arrayBuffers: Math.round((used.arrayBuffers / 1024 / 1024) * 100) / 100,
};
// CPU Usage
const cpuMetrics = await this.getCPUUsage();
// V8 Heap Statistics
const heapStats = v8.getHeapStatistics(); const heapStats = v8.getHeapStatistics();
const v8Metrics = { const v8HeapStats = {
totalHeapSizeBytes: heapStats.total_heap_size, // bytes totalHeapSizeBytes: heapStats.total_heap_size,
usedHeapSizeBytes: heapStats.used_heap_size, // bytes usedHeapSizeBytes: heapStats.used_heap_size,
heapSizeLimitBytes: heapStats.heap_size_limit, // bytes heapSizeLimitBytes: heapStats.heap_size_limit,
}; };
// Event Loop Delay let eventLoopDelayMs = 0;
let eventLoopDelay = 0;
performance.mark("start"); performance.mark("start");
await new Promise((resolve) => setTimeout(resolve, 0)); await new Promise((resolve) => setTimeout(resolve, 0));
performance.mark("end"); performance.mark("end");
performance.measure("eventLoopDelay", "start", "end"); performance.measure("eventLoopDelay", "start", "end");
const entries = performance.getEntriesByName("eventLoopDelay"); const entries = performance.getEntriesByName("eventLoopDelay");
if (entries.length > 0 && entries[0] !== undefined) { if (entries.length > 0 && entries[0] !== undefined) {
eventLoopDelay = entries[0].duration; eventLoopDelayMs = entries[0].duration;
} }
// Uptime const uptimeMs = process.uptime() * 1000;
const uptimeMs = process.uptime() * 1000; // ms
// Mongo
const mongoStats = await this.getMongoDBStats(); const mongoStats = await this.getMongoDBStats();
// Combine Metrics return {
const diagnostics = {
osStats, osStats,
memoryUsage, cpuUsage,
cpuUsage: cpuMetrics, v8HeapStats,
v8HeapStats: v8Metrics, eventLoopDelayMs,
eventLoopDelayMs: eventLoopDelay,
uptimeMs, uptimeMs,
mongoStats, mongoStats,
}; };
return diagnostics;
}; };
} }
@@ -1,5 +1,49 @@
import { describe, expect, it, jest, beforeEach, afterEach } from "@jest/globals"; import { describe, expect, it, jest, beforeEach, afterEach } from "@jest/globals";
import { DiagnosticService } from "../../../src/service/business/diagnosticService.ts"; import { DiagnosticService } from "../../../src/service/business/diagnosticService.ts";
import type { IDb } from "../../../src/db/IDb.ts";
import type { Mongoose } from "mongoose";
// ── Test helpers ─────────────────────────────────────────────────────────────
const makeDb = (overrides?: {
collections?: Array<{ collectionName: string }>;
collStats?: Record<string, unknown>;
dbStats?: Record<string, unknown>;
countDocuments?: number;
noDb?: boolean;
}): IDb<Mongoose> => {
const collections = overrides?.collections ?? [];
const collStats = overrides?.collStats ?? {
storageSize: 100,
totalIndexSize: 50,
totalSize: 150,
};
const dbStats = overrides?.dbStats ?? { totalSize: 1000 };
const countDocuments = overrides?.countDocuments ?? 42;
const dbApi = overrides?.noDb
? null
: {
stats: jest.fn<() => Promise<Record<string, unknown>>>().mockResolvedValue(dbStats),
collections: jest.fn<() => Promise<Array<{ collectionName: string }>>>().mockResolvedValue(collections),
command: jest.fn<() => Promise<Record<string, unknown>>>().mockResolvedValue(collStats),
collection: jest.fn().mockReturnValue({
countDocuments: jest.fn<() => Promise<number>>().mockResolvedValue(countDocuments),
}),
};
return {
getConnection: jest.fn<() => Promise<Mongoose>>().mockResolvedValue({
connection: {
db: dbApi,
readyState: 1,
host: "localhost",
port: 27017,
name: "uptime_db",
},
} as unknown as Mongoose),
} as unknown as IDb<Mongoose>;
};
// ── Tests ──────────────────────────────────────────────────────────────────── // ── Tests ────────────────────────────────────────────────────────────────────
@@ -18,7 +62,7 @@ describe("DiagnosticService", () => {
}); });
it("returns diagnosticService from instance getter", () => { it("returns diagnosticService from instance getter", () => {
const service = new DiagnosticService(); const service = new DiagnosticService(makeDb());
expect(service.serviceName).toBe("diagnosticService"); expect(service.serviceName).toBe("diagnosticService");
}); });
}); });
@@ -37,12 +81,11 @@ describe("DiagnosticService", () => {
}) as any; }) as any;
const clearMarksSpy = jest.spyOn(performance, "clearMarks"); const clearMarksSpy = jest.spyOn(performance, "clearMarks");
const service = new DiagnosticService(); const service = new DiagnosticService(makeDb());
expect(mockObserve).toHaveBeenCalledWith({ entryTypes: ["measure"] }); expect(mockObserve).toHaveBeenCalledWith({ entryTypes: ["measure"] });
expect(capturedCallback).not.toBeNull(); expect(capturedCallback).not.toBeNull();
// Invoke the callback manually
const mockEntryList = { getEntries: jest.fn().mockReturnValue([]) } as unknown as PerformanceObserverEntryList; const mockEntryList = { getEntries: jest.fn().mockReturnValue([]) } as unknown as PerformanceObserverEntryList;
capturedCallback!(mockEntryList); capturedCallback!(mockEntryList);
@@ -55,68 +98,88 @@ describe("DiagnosticService", () => {
}); });
}); });
// ── getCPUUsage ─────────────────────────────────────────────────────────
describe("getCPUUsage", () => {
it("returns cpu usage metrics after timing period", async () => {
const service = new DiagnosticService();
const promise = service.getCPUUsage();
jest.advanceTimersByTime(1000);
const result = await promise;
expect(result).toHaveProperty("userUsageMs");
expect(result).toHaveProperty("systemUsageMs");
expect(result).toHaveProperty("usagePercentage");
expect(typeof result.userUsageMs).toBe("number");
expect(typeof result.systemUsageMs).toBe("number");
expect(typeof result.usagePercentage).toBe("number");
});
});
// ── getSystemStats ────────────────────────────────────────────────────── // ── getSystemStats ──────────────────────────────────────────────────────
describe("getSystemStats", () => { describe("getSystemStats", () => {
it("returns all diagnostic sections", async () => { it("returns only the slimmed diagnostic shape (no serverStatus/serverInfo/memoryUsage)", async () => {
const service = new DiagnosticService(); const service = new DiagnosticService(makeDb());
const promise = service.getSystemStats(); const promise = service.getSystemStats();
// getCPUUsage has 1s timeout, then event loop delay has 0ms timeout // getCPUUsagePercentage waits 1s, then event loop delay waits 0ms
jest.advanceTimersByTime(1000); jest.advanceTimersByTime(1000);
// Need to flush the microtask queue and advance again for the nested setTimeout(0)
await jest.advanceTimersByTimeAsync(10); await jest.advanceTimersByTimeAsync(10);
const result = await promise; const result = await promise;
expect(result).toHaveProperty("osStats"); expect(Object.keys(result).sort()).toEqual(["cpuUsage", "eventLoopDelayMs", "mongoStats", "osStats", "uptimeMs", "v8HeapStats"].sort());
expect(result.osStats).toHaveProperty("freeMemoryBytes");
expect(result.osStats).toHaveProperty("totalMemoryBytes");
expect(result).toHaveProperty("memoryUsage");
expect(result.memoryUsage).toHaveProperty("rss");
expect(result.memoryUsage).toHaveProperty("heapTotal");
expect(result.memoryUsage).toHaveProperty("heapUsed");
expect(result.memoryUsage).toHaveProperty("external");
expect(result.memoryUsage).toHaveProperty("arrayBuffers");
expect(result).toHaveProperty("cpuUsage");
expect(result.cpuUsage).toHaveProperty("userUsageMs");
expect(result).toHaveProperty("v8HeapStats");
expect(result.v8HeapStats).toHaveProperty("totalHeapSizeBytes");
expect(result.v8HeapStats).toHaveProperty("usedHeapSizeBytes");
expect(result.v8HeapStats).toHaveProperty("heapSizeLimitBytes");
expect(result.osStats).toEqual({ totalMemoryBytes: expect.any(Number) });
expect(result.cpuUsage).toEqual({ usagePercentage: expect.any(Number) });
expect(result.v8HeapStats).toEqual({
totalHeapSizeBytes: expect.any(Number),
usedHeapSizeBytes: expect.any(Number),
heapSizeLimitBytes: expect.any(Number),
});
expect(typeof result.eventLoopDelayMs).toBe("number"); expect(typeof result.eventLoopDelayMs).toBe("number");
expect(result.eventLoopDelayMs).toBeGreaterThanOrEqual(0);
expect(typeof result.uptimeMs).toBe("number");
expect(result.uptimeMs).toBeGreaterThan(0); expect(result.uptimeMs).toBeGreaterThan(0);
}); });
it("returns slimmed mongoStats with `collections` (not `collectionStats`) and no serverStatus/serverInfo", async () => {
const db = makeDb({
collections: [{ collectionName: "monitors" }, { collectionName: "checks" }],
collStats: { storageSize: 200, totalIndexSize: 80, totalSize: 280 },
dbStats: { totalSize: 9999 },
countDocuments: 7,
});
const service = new DiagnosticService(db);
const promise = service.getSystemStats();
jest.advanceTimersByTime(1000);
await jest.advanceTimersByTimeAsync(10);
const result = await promise;
expect(Object.keys(result.mongoStats).sort()).toEqual(["collections", "dbName", "host", "port", "readyState", "totalSize"].sort());
expect(result.mongoStats.totalSize).toBe(9999);
expect(result.mongoStats.collections).toHaveLength(2);
expect(result.mongoStats.collections[0]).toEqual({
name: "monitors",
documentCount: 7,
storageSize: 200,
totalIndexSize: 80,
totalSize: 280,
});
});
it("includes bucketCount on timeseries collections, omits it elsewhere", async () => {
const db = makeDb({
collections: [{ collectionName: "checks" }],
collStats: { storageSize: 1, totalIndexSize: 2, totalSize: 3, timeseries: { bucketCount: 12 } },
});
const service = new DiagnosticService(db);
const promise = service.getSystemStats();
jest.advanceTimersByTime(1000);
await jest.advanceTimersByTimeAsync(10);
const result = await promise;
expect(result.mongoStats.collections[0]?.bucketCount).toBe(12);
const db2 = makeDb({ collections: [{ collectionName: "monitors" }] });
const service2 = new DiagnosticService(db2);
const promise2 = service2.getSystemStats();
jest.advanceTimersByTime(1000);
await jest.advanceTimersByTimeAsync(10);
const result2 = await promise2;
expect(result2.mongoStats.collections[0]).not.toHaveProperty("bucketCount");
});
it("throws AppError when the mongo connection has no db handle", async () => {
jest.useRealTimers();
const service = new DiagnosticService(makeDb({ noDb: true }));
await expect(service.getSystemStats()).rejects.toThrow("Database connection is not available");
jest.useFakeTimers();
});
it("reads eventLoopDelay from real performance entries", async () => { it("reads eventLoopDelay from real performance entries", async () => {
jest.useRealTimers(); jest.useRealTimers();
const service = new DiagnosticService(); const service = new DiagnosticService(makeDb());
// Call getSystemStats with real timers — slow but guarantees real perf entries
const result = await service.getSystemStats(); const result = await service.getSystemStats();
expect(typeof result.eventLoopDelayMs).toBe("number"); expect(typeof result.eventLoopDelayMs).toBe("number");
expect(result.eventLoopDelayMs).toBeGreaterThanOrEqual(0); expect(result.eventLoopDelayMs).toBeGreaterThanOrEqual(0);
jest.useFakeTimers(); jest.useFakeTimers();
@@ -126,7 +189,7 @@ describe("DiagnosticService", () => {
{ label: "empty entries array", entries: [] }, { label: "empty entries array", entries: [] },
{ label: "undefined entry in array", entries: [undefined] }, { label: "undefined entry in array", entries: [undefined] },
])("defaults eventLoopDelayMs to 0 when $label", async ({ entries }) => { ])("defaults eventLoopDelayMs to 0 when $label", async ({ entries }) => {
const service = new DiagnosticService(); const service = new DiagnosticService(makeDb());
const originalGetEntries = performance.getEntriesByName; const originalGetEntries = performance.getEntriesByName;
const originalMeasure = performance.measure; const originalMeasure = performance.measure;
const originalMark = performance.mark; const originalMark = performance.mark;