Skip to content

Commit

Permalink
Merge pull request #781 from janhq/feat/add-system-resources
Browse files Browse the repository at this point in the history
feat: add system resources
  • Loading branch information
namchuai authored Jun 26, 2024
2 parents 7e4e1b8 + af0e3bd commit e201b86
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cortex-js/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { ConfigsModule } from './usecases/configs/configs.module';
import { EnginesModule } from './usecases/engines/engines.module';
import { ConfigsController } from './infrastructure/controllers/configs.controller';
import { EnginesController } from './infrastructure/controllers/engines.controller';
import { ResourceManagerModule } from './infrastructure/services/resources-manager/resources-manager.module';

@Module({
imports: [
Expand Down Expand Up @@ -58,6 +59,7 @@ import { EnginesController } from './infrastructure/controllers/engines.controll
ExtensionsModule,
ConfigsModule,
EnginesModule,
ResourceManagerModule,
],
controllers: [
AssistantsController,
Expand Down
15 changes: 15 additions & 0 deletions cortex-js/src/domain/models/resource.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export interface ResourceEvent {
data: ResourceStatus;
}

export interface ResourceStatus {
mem: UsedMemInfo;
cpu: {
usage: number;
};
}

export interface UsedMemInfo {
total: number;
used: number;
}
36 changes: 36 additions & 0 deletions cortex-js/src/infrastructure/controllers/events.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,20 @@ import { EventEmitter2 } from '@nestjs/event-emitter';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import {
Observable,
catchError,
combineLatest,
from,
fromEvent,
interval,
map,
merge,
of,
startWith,
switchMap,
throttleTime,
} from 'rxjs';
import { ResourcesManagerService } from '../services/resources-manager/resources-manager.service';
import { ResourceEvent } from '@/domain/models/resource.interface';

@ApiTags('Events')
@Controller('events')
Expand All @@ -32,6 +38,7 @@ export class EventsController {
private readonly downloadManagerService: DownloadManagerService,
private readonly modelsUsecases: ModelsUsecases,
private readonly eventEmitter: EventEmitter2,
private readonly resourcesManagerService: ResourcesManagerService,
) {}

@ApiOperation({
Expand Down Expand Up @@ -83,4 +90,33 @@ export class EventsController {
map(([status, event]) => ({ data: { status, event } })),
);
}

@ApiOperation({
summary: 'Get resources status',
description: 'Retrieves the resources status of the system.',
})
@Sse('resources')
resourcesEvent(): Observable<ResourceEvent> {
const initialData$ = from(
this.resourcesManagerService.getResourceStatuses(),
).pipe(
map((data) => ({ data: data })),
catchError((error) => {
console.error('Error fetching initial resource statuses', error);
return of(); // Ensure the stream is kept alive even if initial fetch fails
}),
);

const getResourceStatuses$ = interval(2000).pipe(
switchMap(() => this.resourcesManagerService.getResourceStatuses()),
map((data) => ({ data: data })),
catchError((error) => {
console.error('Error fetching resource statuses', error);
return of(); // Keep the stream alive on error
}),
);

// Merge the initial data with the interval updates
return merge(initialData$, getResourceStatuses$);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Module } from '@nestjs/common';
import { ResourcesManagerService } from './resources-manager.service';

@Module({
providers: [ResourcesManagerService],
exports: [ResourcesManagerService],
})
export class ResourceManagerModule {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {
ResourceStatus,
UsedMemInfo,
} from '@/domain/models/resource.interface';
import { Injectable } from '@nestjs/common';
import systemInformation, { Systeminformation } from 'systeminformation';

@Injectable()
export class ResourcesManagerService {
async getResourceStatuses(): Promise<ResourceStatus> {
const promises = [systemInformation.currentLoad(), systemInformation.mem()];
const results = await Promise.all(promises);

const cpuUsage = results[0] as Systeminformation.CurrentLoadData;
const memory = results[1] as Systeminformation.MemData;
const memInfo: UsedMemInfo = {
total: memory.total,
used: memory.used,
};

return {
mem: memInfo,
cpu: {
usage: cpuUsage.avgLoad,
},
};
}
}

0 comments on commit e201b86

Please sign in to comment.