"""Системная статистика: диск хранилища, CPU, RAM, аптайм.""" from __future__ import annotations import os import shutil import time import psutil from ..config import Config _START = time.time() def storage_disk(path: str) -> dict: """Использование диска, на котором лежит path (для одного хранилища).""" try: usage = shutil.disk_usage(path) return { "total_gb": round(usage.total / 1024 ** 3, 1), "used_gb": round(usage.used / 1024 ** 3, 1), "free_gb": round(usage.free / 1024 ** 3, 1), "percent": round(usage.used / usage.total * 100, 1) if usage.total else 0, } except OSError: return {"total_gb": 0, "used_gb": 0, "free_gb": 0, "percent": 0} def is_mounted(path: str) -> bool: """Точка монтирования? (для NAS — признак, что том реально подключён).""" try: return os.path.ismount(path) except OSError: return False def system_stats(config: Config) -> dict: active = config.active_storage() path = active.path vm = psutil.virtual_memory() return { "uptime_s": int(time.time() - _START), "cpu_percent": psutil.cpu_percent(interval=None), "ram_percent": vm.percent, "ram_used_gb": round(vm.used / 1024 ** 3, 1), "ram_total_gb": round(vm.total / 1024 ** 3, 1), "storage_path": path, "disk": storage_disk(path), "quota_gb": active.quota_gb, "retention_days": config.storage.retention_days, "segment_seconds": config.storage.segment_seconds, }