diff --git a/app/api/v1/endpoints/system.py b/app/api/v1/endpoints/system.py index 82ba5ff9..78865cf0 100644 --- a/app/api/v1/endpoints/system.py +++ b/app/api/v1/endpoints/system.py @@ -1,47 +1,27 @@ from fastapi import APIRouter -from gpustat import GPUStatCollection from app.api.v1.schemas.system import ( - GpuInfoPayload, - GpuInfosResponse, - LibraryInfo, + GpusInfoResponse, ServerInfoPayload, ServerInfoResponse, ) +from app.services.system import system_service router = APIRouter() @router.get("/server-info", response_model=ServerInfoResponse) def get_server_info() -> ServerInfoResponse: + installed_libraries = system_service.get_installed_libraries() - server_info = ServerInfoPayload(installed_libraries=[LibraryInfo(name="netspresso", version="1.14.0")]) + server_info = ServerInfoPayload(installed_libraries=installed_libraries) return ServerInfoResponse(data=server_info) -@router.get("/gpu-info", response_model=GpuInfosResponse) -def get_gpu_infos() -> GpuInfosResponse: - - stats = GPUStatCollection.new_query() - - gpu_infos = [ - GpuInfoPayload( - index=gpu.index, - uuid=gpu.uuid, - name=gpu.name, - temperature_gpu=gpu.temperature, - fan_speed=gpu.fan_speed, - utilization_gpu=gpu.utilization, - utilization_enc=gpu.utilization_enc, - utilization_dec=gpu.utilization_dec, - power_draw=gpu.power_draw, - enforced_power_limit=gpu.power_limit, - memory_used=gpu.memory_used, - memory_total=gpu.memory_total, - processes=list(gpu.processes), - ) - for gpu in stats.gpus - ] - - return GpuInfosResponse(data=gpu_infos) +@router.get("/gpus-info", response_model=GpusInfoResponse) +def get_gpus_info() -> GpusInfoResponse: + + gpus_info = system_service.get_gpus_info() + + return GpusInfoResponse(data=gpus_info) diff --git a/app/api/v1/schemas/system.py b/app/api/v1/schemas/system.py index 2a19f4b1..ec8ece03 100644 --- a/app/api/v1/schemas/system.py +++ b/app/api/v1/schemas/system.py @@ -34,5 +34,5 @@ class ServerInfoResponse(ResponseItem): data: ServerInfoPayload -class GpuInfosResponse(ResponseItem): +class GpusInfoResponse(ResponseItem): data: List[GpuInfoPayload] diff --git a/app/services/__init__.py b/app/services/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/app/services/system.py b/app/services/system.py new file mode 100644 index 00000000..e92b27b0 --- /dev/null +++ b/app/services/system.py @@ -0,0 +1,45 @@ +from typing import List + +from gpustat import GPUStatCollection + +from app.api.v1.schemas.system import GpuInfoPayload, LibraryInfo +from netspresso.clients.utils.system import get_package_version + + +class SystemService: + def get_installed_libraries(self) -> List[LibraryInfo]: + LIBRARY_KEYS = ["netspresso"] + + installed_libraries = [ + LibraryInfo(name=library_name, version=get_package_version(package_name=library_name)) + for library_name in LIBRARY_KEYS + ] + + return installed_libraries + + def get_gpus_info(self) -> List[GpuInfoPayload]: + stats = GPUStatCollection.new_query() + + gpus_info = [ + GpuInfoPayload( + index=gpu.index, + uuid=gpu.uuid, + name=gpu.name, + temperature_gpu=gpu.temperature, + fan_speed=gpu.fan_speed, + utilization_gpu=gpu.utilization, + utilization_enc=gpu.utilization_enc, + utilization_dec=gpu.utilization_dec, + power_draw=gpu.power_draw, + enforced_power_limit=gpu.power_limit, + memory_used=gpu.memory_used, + memory_total=gpu.memory_total, + processes=list(gpu.processes), + ) + for gpu in stats.gpus + ] + + return gpus_info + + +system_service = SystemService()