refactor(api): update CPU and memory metrics naming conventions

- Renamed CPU and memory metric fields for consistency, changing `load` to `percentTotal`, `loadUser` to `percentUser`, and similar adjustments for other fields.
- Updated integration tests to reflect the new naming conventions, ensuring accurate property checks for CPU and memory utilization.
- Enhanced the `CpuService` and `MemoryService` to return the updated metric names, improving clarity in the API response.
This commit is contained in:
Eli Bosley
2025-08-19 14:07:20 -04:00
parent 25ff13b0bb
commit 9df941317a
6 changed files with 47 additions and 32 deletions

View File

@@ -6,33 +6,33 @@ import { GraphQLJSON } from 'graphql-scalars';
@ObjectType({ description: 'CPU load for a single core' })
export class CpuLoad {
@Field(() => Float, { description: 'The total CPU load on a single core, in percent.' })
load!: number;
percentTotal!: number;
@Field(() => Float, { description: 'The percentage of time the CPU spent in user space.' })
loadUser!: number;
percentUser!: number;
@Field(() => Float, { description: 'The percentage of time the CPU spent in kernel space.' })
loadSystem!: number;
percentSystem!: number;
@Field(() => Float, {
description:
'The percentage of time the CPU spent on low-priority (niced) user space processes.',
})
loadNice!: number;
percentNice!: number;
@Field(() => Float, { description: 'The percentage of time the CPU was idle.' })
loadIdle!: number;
percentIdle!: number;
@Field(() => Float, {
description: 'The percentage of time the CPU spent servicing hardware interrupts.',
})
loadIrq!: number;
percentIrq!: number;
}
@ObjectType({ implements: () => Node })
export class CpuUtilization extends Node {
@Field(() => Float, { description: 'Total CPU load in percent' })
load!: number;
percentTotal!: number;
@Field(() => [CpuLoad], { description: 'CPU load for each core' })
cpus!: CpuLoad[];

View File

@@ -35,12 +35,19 @@ export class CpuService {
}
async generateCpuLoad(): Promise<CpuUtilization> {
const { currentLoad: load, cpus } = await currentLoad();
const loadData = await currentLoad();
return {
id: 'info/cpu-load',
load,
cpus,
percentTotal: loadData.currentLoad,
cpus: loadData.cpus.map((cpu) => ({
percentTotal: cpu.load,
percentUser: cpu.loadUser,
percentSystem: cpu.loadSystem,
percentNice: cpu.loadNice,
percentIdle: cpu.loadIdle,
percentIrq: cpu.loadIrq,
})),
};
}
}

View File

@@ -34,8 +34,16 @@ describe('InfoResolver Integration Tests', () => {
OsService,
VersionsService,
DisplayService,
SubscriptionTrackerService,
SubscriptionHelperService,
{
provide: SubscriptionTrackerService,
useValue: {
trackActiveSubscriptions: vi.fn(),
},
},
{
provide: SubscriptionHelperService,
useValue: {},
},
{
provide: ConfigService,
useValue: {

View File

@@ -60,7 +60,7 @@ export class MemoryUtilization extends Node {
buffcache!: number;
@Field(() => Float, { description: 'Memory usage percentage' })
usedPercent!: number;
percentUsed!: number;
@Field(() => GraphQLBigInt, { description: 'Total swap memory in bytes' })
swapTotal!: number;
@@ -72,7 +72,7 @@ export class MemoryUtilization extends Node {
swapFree!: number;
@Field(() => Float, { description: 'Swap usage percentage' })
swapUsedPercent!: number;
percentSwapUsed!: number;
}
@ObjectType({ implements: () => Node })

View File

@@ -40,12 +40,12 @@ export class MemoryService {
available: Math.floor(memInfo.available),
active: Math.floor(memInfo.active),
buffcache: Math.floor(memInfo.buffcache),
usedPercent:
percentUsed:
memInfo.total > 0 ? ((memInfo.total - memInfo.available) / memInfo.total) * 100 : 0,
swapTotal: Math.floor(memInfo.swaptotal),
swapUsed: Math.floor(memInfo.swapused),
swapFree: Math.floor(memInfo.swapfree),
swapUsedPercent: memInfo.swaptotal > 0 ? (memInfo.swapused / memInfo.swaptotal) * 100 : 0,
percentSwapUsed: memInfo.swaptotal > 0 ? (memInfo.swapused / memInfo.swaptotal) * 100 : 0,
};
}
}

View File

@@ -54,18 +54,18 @@ describe('MetricsResolver Integration Tests', () => {
const result = await metricsResolver.cpu();
expect(result).toHaveProperty('id', 'info/cpu-load');
expect(result).toHaveProperty('load');
expect(result).toHaveProperty('percentTotal');
expect(result).toHaveProperty('cpus');
expect(result.cpus).toBeInstanceOf(Array);
expect(result.load).toBeGreaterThanOrEqual(0);
expect(result.load).toBeLessThanOrEqual(100);
expect(result.percentTotal).toBeGreaterThanOrEqual(0);
expect(result.percentTotal).toBeLessThanOrEqual(100);
if (result.cpus.length > 0) {
const firstCpu = result.cpus[0];
expect(firstCpu).toHaveProperty('load');
expect(firstCpu).toHaveProperty('loadUser');
expect(firstCpu).toHaveProperty('loadSystem');
expect(firstCpu).toHaveProperty('loadIdle');
expect(firstCpu).toHaveProperty('percentTotal');
expect(firstCpu).toHaveProperty('percentUser');
expect(firstCpu).toHaveProperty('percentSystem');
expect(firstCpu).toHaveProperty('percentIdle');
}
});
@@ -77,15 +77,15 @@ describe('MetricsResolver Integration Tests', () => {
expect(result).toHaveProperty('used');
expect(result).toHaveProperty('free');
expect(result).toHaveProperty('available');
expect(result).toHaveProperty('usedPercent');
expect(result).toHaveProperty('percentUsed');
expect(result).toHaveProperty('swapTotal');
expect(result).toHaveProperty('swapUsed');
expect(result).toHaveProperty('swapFree');
expect(result).toHaveProperty('swapUsedPercent');
expect(result).toHaveProperty('percentSwapUsed');
expect(result.total).toBeGreaterThan(0);
expect(result.usedPercent).toBeGreaterThanOrEqual(0);
expect(result.usedPercent).toBeLessThanOrEqual(100);
expect(result.percentUsed).toBeGreaterThanOrEqual(0);
expect(result.percentUsed).toBeLessThanOrEqual(100);
});
});
@@ -100,7 +100,7 @@ describe('MetricsResolver Integration Tests', () => {
await new Promise((resolve) => setTimeout(resolve, 50)); // Simulate slow operation
return {
id: 'info/cpu-load',
load: 50,
percentTotal: 50,
cpus: [],
};
});
@@ -131,11 +131,11 @@ describe('MetricsResolver Integration Tests', () => {
available: 8000000000,
active: 4000000000,
buffcache: 2000000000,
usedPercent: 50,
percentUsed: 50,
swapTotal: 0,
swapUsed: 0,
swapFree: 0,
swapUsedPercent: 0,
percentSwapUsed: 0,
} as any;
});
@@ -164,7 +164,7 @@ describe('MetricsResolver Integration Tests', () => {
expect.objectContaining({
systemMetricsCpu: expect.objectContaining({
id: 'info/cpu-load',
load: expect.any(Number),
percentTotal: expect.any(Number),
cpus: expect.any(Array),
}),
})
@@ -191,7 +191,7 @@ describe('MetricsResolver Integration Tests', () => {
id: 'memory-utilization',
used: expect.any(Number),
free: expect.any(Number),
usedPercent: expect.any(Number),
percentUsed: expect.any(Number),
}),
})
);