|
| 1 | +import os from 'os' |
| 2 | + |
| 3 | +export interface AdminMetricsSnapshot { |
| 4 | + timestamp: number |
| 5 | + eventsPerSecond: number |
| 6 | + acceptedEvents: number |
| 7 | + rejectedEvents: number |
| 8 | + activeConnections: number |
| 9 | + cpuLoadPercent: number |
| 10 | + memoryUsedMb: number |
| 11 | +} |
| 12 | + |
| 13 | +export class AdminMetricsCollector { |
| 14 | + private acceptedEvents = 0 |
| 15 | + private rejectedEvents = 0 |
| 16 | + private readonly activeConnections = new Set<string>() |
| 17 | + private readonly eventTimestamps: number[] = [] |
| 18 | + private previousCpuUsage = process.cpuUsage() |
| 19 | + private previousCpuTimeMs = Date.now() |
| 20 | + |
| 21 | + public recordAcceptedEvent(timestamp: number = Date.now()): void { |
| 22 | + this.acceptedEvents += 1 |
| 23 | + this.eventTimestamps.push(timestamp) |
| 24 | + this.pruneEventWindow(timestamp) |
| 25 | + } |
| 26 | + |
| 27 | + public recordRejectedEvent(timestamp: number = Date.now()): void { |
| 28 | + this.rejectedEvents += 1 |
| 29 | + this.eventTimestamps.push(timestamp) |
| 30 | + this.pruneEventWindow(timestamp) |
| 31 | + } |
| 32 | + |
| 33 | + public openConnection(connectionId: string): void { |
| 34 | + this.activeConnections.add(connectionId) |
| 35 | + } |
| 36 | + |
| 37 | + public closeConnection(connectionId: string): void { |
| 38 | + this.activeConnections.delete(connectionId) |
| 39 | + } |
| 40 | + |
| 41 | + public getSnapshot(timestamp: number = Date.now()): AdminMetricsSnapshot { |
| 42 | + this.pruneEventWindow(timestamp) |
| 43 | + |
| 44 | + return { |
| 45 | + timestamp, |
| 46 | + eventsPerSecond: this.eventTimestamps.length, |
| 47 | + acceptedEvents: this.acceptedEvents, |
| 48 | + rejectedEvents: this.rejectedEvents, |
| 49 | + activeConnections: this.activeConnections.size, |
| 50 | + cpuLoadPercent: this.getCpuLoadPercent(timestamp), |
| 51 | + memoryUsedMb: Math.round((process.memoryUsage().heapUsed / 1024 / 1024) * 100) / 100, |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private pruneEventWindow(timestamp: number): void { |
| 56 | + const minTimestamp = timestamp - 1000 |
| 57 | + |
| 58 | + while (this.eventTimestamps.length > 0 && this.eventTimestamps[0] <= minTimestamp) { |
| 59 | + this.eventTimestamps.shift() |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private getCpuLoadPercent(timestamp: number): number { |
| 64 | + const elapsedMs = timestamp - this.previousCpuTimeMs |
| 65 | + if (elapsedMs <= 0) { |
| 66 | + return 0 |
| 67 | + } |
| 68 | + |
| 69 | + const cpuDiff = process.cpuUsage(this.previousCpuUsage) |
| 70 | + this.previousCpuUsage = process.cpuUsage() |
| 71 | + this.previousCpuTimeMs = timestamp |
| 72 | + |
| 73 | + const cpuTotalMs = (cpuDiff.user + cpuDiff.system) / 1000 |
| 74 | + const cores = Math.max(os.cpus().length, 1) |
| 75 | + const loadPercent = (cpuTotalMs / (elapsedMs * cores)) * 100 |
| 76 | + |
| 77 | + return Math.round(Math.max(0, Math.min(loadPercent, 100)) * 100) / 100 |
| 78 | + } |
| 79 | +} |
0 commit comments