Skip to content

Commit 3afc421

Browse files
committed
refactor(api): streamline InfoResolver and SubscriptionTrackerService
- Removed unnecessary imports and parameters in InfoResolver for cleaner code. - Updated CPU utilization method to directly use infoService for generating CPU load. - Enhanced SubscriptionTrackerService to improve subscriber count management and added early return for idempotency in unsubscribe method.
1 parent 476abd5 commit 3afc421

2 files changed

Lines changed: 21 additions & 17 deletions

File tree

api/src/unraid-api/graph/resolvers/info/info.resolver.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { OnModuleInit } from '@nestjs/common';
2-
import { Parent, Query, ResolveField, Resolver, Subscription } from '@nestjs/graphql';
2+
import { Query, ResolveField, Resolver, Subscription } from '@nestjs/graphql';
33

44
import { Resource } from '@unraid/shared/graphql.model.js';
55
import {
66
AuthActionVerb,
77
AuthPossession,
88
UsePermissions,
99
} from '@unraid/shared/use-permissions.directive.js';
10-
import { PubSub } from 'graphql-subscriptions';
1110
import { baseboard as getBaseboard, system as getSystem } from 'systeminformation';
1211

1312
import { createSubscription, pubsub, PUBSUB_CHANNEL } from '@app/core/pubsub.js';
@@ -37,8 +36,7 @@ export class InfoResolver implements OnModuleInit {
3736
constructor(
3837
private readonly infoService: InfoService,
3938
private readonly displayService: DisplayService,
40-
private readonly subscriptionTracker: SubscriptionTrackerService,
41-
private readonly cpuDataService: CpuDataService
39+
private readonly subscriptionTracker: SubscriptionTrackerService
4240
) {}
4341

4442
onModuleInit() {
@@ -148,12 +146,7 @@ export class InfoResolver implements OnModuleInit {
148146
possession: AuthPossession.ANY,
149147
})
150148
public async cpuUtilization(): Promise<CpuUtilization> {
151-
const { currentLoad: load, cpus } = await this.cpuDataService.getCpuLoad();
152-
return {
153-
id: 'info/cpu-load',
154-
load,
155-
cpus,
156-
};
149+
return this.infoService.generateCpuLoad();
157150
}
158151

159152
@Subscription(() => CpuUtilization, {
@@ -189,7 +182,7 @@ export class InfoCpuResolver {
189182
description: 'CPU utilization in percent',
190183
nullable: true,
191184
})
192-
public async utilization(@Parent() cpu: InfoCpu): Promise<number> {
185+
public async utilization(): Promise<number> {
193186
const { currentLoad } = await this.cpuDataService.getCpuLoad();
194187
return currentLoad;
195188
}

api/src/unraid-api/graph/services/subscription-tracker.service.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,39 @@ export class SubscriptionTrackerService {
1010
}
1111

1212
public subscribe(topic: string): void {
13-
const currentCount = this.subscriberCounts.get(topic) || 0;
13+
const currentCount = this.subscriberCounts.get(topic) ?? 0;
1414
this.subscriberCounts.set(topic, currentCount + 1);
1515

1616
if (currentCount === 0) {
1717
const handlers = this.topicHandlers.get(topic);
18-
if (handlers) {
18+
if (handlers?.onStart) {
1919
handlers.onStart();
2020
}
2121
}
2222
}
2323

2424
public unsubscribe(topic: string): void {
25-
const currentCount = this.subscriberCounts.get(topic) || 1;
26-
const newCount = currentCount - 1;
25+
const currentCount = this.subscriberCounts.get(topic) ?? 0;
26+
27+
// Early return for idempotency - if already at 0, do nothing
28+
if (currentCount === 0) {
29+
return;
30+
}
2731

28-
this.subscriberCounts.set(topic, newCount);
32+
const newCount = currentCount - 1;
2933

3034
if (newCount === 0) {
35+
// Delete the topic entry when reaching zero
36+
this.subscriberCounts.delete(topic);
37+
38+
// Call onStop handler if it exists
3139
const handlers = this.topicHandlers.get(topic);
32-
if (handlers) {
40+
if (handlers?.onStop) {
3341
handlers.onStop();
3442
}
43+
} else {
44+
// Only update the count if not zero
45+
this.subscriberCounts.set(topic, newCount);
3546
}
3647
}
3748
}

0 commit comments

Comments
 (0)