Files
Toju/electron/app-metrics.ts
2026-06-12 01:22:01 +02:00

38 lines
1.1 KiB
TypeScript

import { app } from 'electron';
export interface AppMetricsProcessSnapshot {
pid: number;
type: string;
workingSetKb: number | null;
peakWorkingSetKb: number | null;
privateBytesKb: number | null;
creationTime: number | null;
cpuPercent: number | null;
}
export interface AppMetricsSnapshot {
collectedAt: number;
processes: AppMetricsProcessSnapshot[];
}
export function collectAppMetricsSnapshot(): AppMetricsSnapshot {
return {
collectedAt: Date.now(),
processes: app.getAppMetrics().map((metric) => ({
pid: metric.pid,
type: metric.type,
workingSetKb: metric.memory?.workingSetSize ?? null,
peakWorkingSetKb: readOptionalKilobytes(metric.memory?.peakWorkingSetSize),
privateBytesKb: readOptionalKilobytes(metric.memory?.privateBytes),
creationTime: metric.creationTime ?? null,
cpuPercent: typeof metric.cpu?.percentCPUUsage === 'number'
? Math.round(metric.cpu.percentCPUUsage * 10) / 10
: null
}))
};
}
function readOptionalKilobytes(value: number | undefined): number | null {
return typeof value === 'number' && value >= 0 ? value : null;
}