Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit e201b86

Browse files
authored
Merge pull request #781 from janhq/feat/add-system-resources
feat: add system resources
2 parents 7e4e1b8 + af0e3bd commit e201b86

File tree

5 files changed

+89
-0
lines changed

5 files changed

+89
-0
lines changed

cortex-js/src/app.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { ConfigsModule } from './usecases/configs/configs.module';
3131
import { EnginesModule } from './usecases/engines/engines.module';
3232
import { ConfigsController } from './infrastructure/controllers/configs.controller';
3333
import { EnginesController } from './infrastructure/controllers/engines.controller';
34+
import { ResourceManagerModule } from './infrastructure/services/resources-manager/resources-manager.module';
3435

3536
@Module({
3637
imports: [
@@ -58,6 +59,7 @@ import { EnginesController } from './infrastructure/controllers/engines.controll
5859
ExtensionsModule,
5960
ConfigsModule,
6061
EnginesModule,
62+
ResourceManagerModule,
6163
],
6264
controllers: [
6365
AssistantsController,
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export interface ResourceEvent {
2+
data: ResourceStatus;
3+
}
4+
5+
export interface ResourceStatus {
6+
mem: UsedMemInfo;
7+
cpu: {
8+
usage: number;
9+
};
10+
}
11+
12+
export interface UsedMemInfo {
13+
total: number;
14+
used: number;
15+
}

cortex-js/src/infrastructure/controllers/events.controller.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,20 @@ import { EventEmitter2 } from '@nestjs/event-emitter';
1616
import { ApiOperation, ApiTags } from '@nestjs/swagger';
1717
import {
1818
Observable,
19+
catchError,
1920
combineLatest,
21+
from,
2022
fromEvent,
23+
interval,
2124
map,
2225
merge,
2326
of,
2427
startWith,
28+
switchMap,
2529
throttleTime,
2630
} from 'rxjs';
31+
import { ResourcesManagerService } from '../services/resources-manager/resources-manager.service';
32+
import { ResourceEvent } from '@/domain/models/resource.interface';
2733

2834
@ApiTags('Events')
2935
@Controller('events')
@@ -32,6 +38,7 @@ export class EventsController {
3238
private readonly downloadManagerService: DownloadManagerService,
3339
private readonly modelsUsecases: ModelsUsecases,
3440
private readonly eventEmitter: EventEmitter2,
41+
private readonly resourcesManagerService: ResourcesManagerService,
3542
) {}
3643

3744
@ApiOperation({
@@ -83,4 +90,33 @@ export class EventsController {
8390
map(([status, event]) => ({ data: { status, event } })),
8491
);
8592
}
93+
94+
@ApiOperation({
95+
summary: 'Get resources status',
96+
description: 'Retrieves the resources status of the system.',
97+
})
98+
@Sse('resources')
99+
resourcesEvent(): Observable<ResourceEvent> {
100+
const initialData$ = from(
101+
this.resourcesManagerService.getResourceStatuses(),
102+
).pipe(
103+
map((data) => ({ data: data })),
104+
catchError((error) => {
105+
console.error('Error fetching initial resource statuses', error);
106+
return of(); // Ensure the stream is kept alive even if initial fetch fails
107+
}),
108+
);
109+
110+
const getResourceStatuses$ = interval(2000).pipe(
111+
switchMap(() => this.resourcesManagerService.getResourceStatuses()),
112+
map((data) => ({ data: data })),
113+
catchError((error) => {
114+
console.error('Error fetching resource statuses', error);
115+
return of(); // Keep the stream alive on error
116+
}),
117+
);
118+
119+
// Merge the initial data with the interval updates
120+
return merge(initialData$, getResourceStatuses$);
121+
}
86122
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Module } from '@nestjs/common';
2+
import { ResourcesManagerService } from './resources-manager.service';
3+
4+
@Module({
5+
providers: [ResourcesManagerService],
6+
exports: [ResourcesManagerService],
7+
})
8+
export class ResourceManagerModule {}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import {
2+
ResourceStatus,
3+
UsedMemInfo,
4+
} from '@/domain/models/resource.interface';
5+
import { Injectable } from '@nestjs/common';
6+
import systemInformation, { Systeminformation } from 'systeminformation';
7+
8+
@Injectable()
9+
export class ResourcesManagerService {
10+
async getResourceStatuses(): Promise<ResourceStatus> {
11+
const promises = [systemInformation.currentLoad(), systemInformation.mem()];
12+
const results = await Promise.all(promises);
13+
14+
const cpuUsage = results[0] as Systeminformation.CurrentLoadData;
15+
const memory = results[1] as Systeminformation.MemData;
16+
const memInfo: UsedMemInfo = {
17+
total: memory.total,
18+
used: memory.used,
19+
};
20+
21+
return {
22+
mem: memInfo,
23+
cpu: {
24+
usage: cpuUsage.avgLoad,
25+
},
26+
};
27+
}
28+
}

0 commit comments

Comments
 (0)