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>
62 lines
1.7 KiB
Python
62 lines
1.7 KiB
Python
import asyncio
|
|
from dataclasses import dataclass
|
|
from typing import cast
|
|
|
|
import psutil
|
|
|
|
from ...config import config
|
|
from ...util import match_list_regexp
|
|
from . import normal_collector, periodic_collector
|
|
|
|
|
|
@dataclass
|
|
class ProcessStatus:
|
|
name: str
|
|
cpu: float
|
|
mem: int
|
|
|
|
|
|
async def get_process_status() -> list[ProcessStatus]:
|
|
if not config.ps_proc_len:
|
|
return []
|
|
|
|
async def parse_one(proc: psutil.Process) -> ProcessStatus | None:
|
|
name = proc.name()
|
|
if match_list_regexp(config.ps_ignore_procs, name):
|
|
# logger.info(f"进程 {name} 匹配 {regex.re.pattern},忽略")
|
|
return None
|
|
|
|
# proc.cpu_percent()
|
|
# await asyncio.sleep(1)
|
|
cpu_count = psutil.cpu_count()
|
|
with proc.oneshot():
|
|
cpu = proc.cpu_percent()
|
|
cpu = (
|
|
(cpu / cpu_count) if config.ps_proc_cpu_max_100p and cpu_count else cpu
|
|
)
|
|
mem: int = proc.memory_info().rss
|
|
|
|
return ProcessStatus(name=name, cpu=cpu, mem=mem)
|
|
|
|
def sorter(x: ProcessStatus):
|
|
sort_by = config.ps_proc_sort_by
|
|
if sort_by == "mem":
|
|
return x.mem
|
|
# if sort_by == "cpu":
|
|
return x.cpu
|
|
|
|
proc_list = cast(
|
|
"list[ProcessStatus | None | Exception]",
|
|
await asyncio.gather(
|
|
*(parse_one(proc) for proc in psutil.process_iter()),
|
|
return_exceptions=True,
|
|
),
|
|
)
|
|
proc_list = [x for x in proc_list if x and (not isinstance(x, Exception))]
|
|
proc_list.sort(key=sorter, reverse=True)
|
|
return proc_list[: config.ps_proc_len]
|
|
|
|
|
|
normal_collector("process_status")(get_process_status)
|
|
periodic_collector("process_status_periodic")(get_process_status)
|