""" 本机硬件与系统信息(供「系统」页轮询)。 """ from __future__ import annotations import platform import time from typing import Any import psutil from web2_host_caps import gpu_status_payload, machine_summary_payload def _cpu_freq() -> dict[str, float | None] | None: try: f = psutil.cpu_freq() if not f: return None return { "current_mhz": round(f.current, 1) if f.current else None, "max_mhz": round(f.max, 1) if f.max else None, } except Exception: return None def snapshot() -> dict[str, Any]: try: psutil.cpu_percent(interval=None) cpu = psutil.cpu_percent(interval=0.15) except Exception: cpu = None try: vm = psutil.virtual_memory() mem = { "percent": vm.percent, "used": vm.used, "total": vm.total, "available": vm.available, } except Exception: mem = {} try: sm = psutil.swap_memory() swap = {"percent": sm.percent, "used": sm.used, "total": sm.total} except Exception: swap = {} disks: list[dict[str, Any]] = [] try: for part in psutil.disk_partitions(all=False): try: usage = psutil.disk_usage(part.mountpoint) disks.append( { "device": part.device, "mountpoint": part.mountpoint, "fstype": part.fstype, "percent": usage.percent, "free": usage.free, "total": usage.total, } ) except (PermissionError, OSError): continue except Exception: pass boot: int | None = None try: boot = int(psutil.boot_time()) except Exception: pass machine = machine_summary_payload() processor = (platform.processor() or "").strip() or None return { "ts": time.time(), "system": { **machine, "os_version": platform.version(), "processor": processor, "boot_time_unix": boot, }, "hardware": { "cpu_logical": psutil.cpu_count(logical=True), "cpu_physical": psutil.cpu_count(logical=False), "cpu_freq": _cpu_freq(), "memory_total": mem.get("total"), "gpu": gpu_status_payload(), }, "cpu_percent": cpu, "memory": mem, "swap": swap, "disks": disks, }