Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add system resources #781

Merged
merged 2 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
},
};
}
}
Loading