Files
HeXi/hexi/plugins/nonebot_plugin_picstatus/services/collectors/cpu.py
T
sansenhoshiandClaude 9371a28e35 refactor: restructure plugins per MTSS standard, consolidate assets to res/
Per docs/plugin-audit-report.md (plugins normalized to
Trigger(handlers) → Service(services) → Model(repository/models) + utils):

- Split monolithic __init__.py into handlers/services/utils across
  dailywife, deer_pipe, dice, galgame_card, helldivers_tools,
  huoziyinshua, learning_chat, makeaquote, mc_server_status,
  ncm_saying, picfinder_take, picstatus, random_jm_code, regif,
  steam_info, video_analysis, group_tools
- Move static assets under res/: deer_pipe font/img, makeaquote font,
  helldivers img/templates, huoziyinshua HuoZiYinShua
- Add config.py + register_config_items to ncm_saying, random_jm_code,
  group_tools; learning_chat unified config bridge
- Remove deprecated: voice_trans plugin, bf_bot/test.py, dead code in
  dailywife/deer_pipe, empty dirs, debug scripts under helldivers temp
- Disable brash_general_supercredits_tools (stub comment only)
- bot.py: optional stdout/stderr redirect to log file for Web log viewer,
  force ANSI colorize on non-TTY sinks
- Move runtime data (jm_code.json) out of plugin dir into hexi/data
- Docs: plugin-audit-report.md; README reflects removed plugins

Co-Authored-By: Claude <noreply@anthropic.com>
2026-09-03 00:44:38 +08:00

71 lines
1.8 KiB
Python

import asyncio
from dataclasses import dataclass
from typing import cast
import psutil
from cpuinfo import get_cpu_info
from nonebot import logger
from . import first_time_collector, normal_collector, periodic_collector
@dataclass
class CpuFreq:
current: float | None
min: float | None # noqa: A003
max: float | None # noqa: A003
@first_time_collector()
async def cpu_brand() -> str:
try:
brand = (
cast("str", get_cpu_info().get("brand_raw", ""))
.split("@", maxsplit=1)[0]
.strip()
)
if brand.lower().endswith(("cpu", "processor")):
brand = brand.rsplit(maxsplit=1)[0].strip()
except Exception:
logger.exception("Error when getting CPU brand")
return "未知型号"
else:
return brand
@first_time_collector()
async def cpu_count_logical() -> int | None:
return psutil.cpu_count()
@first_time_collector()
async def cpu_count() -> int | None:
return psutil.cpu_count(logical=False)
async def get_cpu_percent() -> float:
"""实时采样 0.5s,避免 psutil 首次调用返回 0.0"""
return await asyncio.to_thread(psutil.cpu_percent, 0.5)
async def get_cpu_percent_periodic() -> float:
"""周期采样:两次调用之间的间隔即为采样区间,直接取瞬时差值即可"""
return psutil.cpu_percent()
normal_collector("cpu_percent")(get_cpu_percent)
periodic_collector("cpu_percent_periodic")(get_cpu_percent_periodic)
async def get_cpu_freq() -> CpuFreq:
cpu_freq = psutil.cpu_freq()
return CpuFreq(
current=getattr(cpu_freq, "current", None),
min=getattr(cpu_freq, "min", None),
max=getattr(cpu_freq, "max", None),
)
normal_collector("cpu_freq")(get_cpu_freq)
periodic_collector("cpu_freq_periodic")(get_cpu_freq)