From 5db29c7e2c28607efb8bd774c8bbe06df278f47a Mon Sep 17 00:00:00 2001 From: snowy Date: Wed, 24 Apr 2024 00:44:45 +0800 Subject: [PATCH] =?UTF-8?q?:memo:=20=E6=B7=BB=E5=8A=A0=E5=AD=97=E4=BD=93?= =?UTF-8?q?=E5=8E=9F=E6=9C=89=E7=89=88=E6=9D=83=E5=A3=B0=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- liteyuki/plugins/liteyuki_status/api.py | 23 +++++++++-- liteyuki/resources/templates/js/status.js | 49 +++++++++++++++++++++++ requirements.txt | 3 +- 3 files changed, 70 insertions(+), 5 deletions(-) diff --git a/liteyuki/plugins/liteyuki_status/api.py b/liteyuki/plugins/liteyuki_status/api.py index 5362184c..b1bff3ff 100644 --- a/liteyuki/plugins/liteyuki_status/api.py +++ b/liteyuki/plugins/liteyuki_status/api.py @@ -146,6 +146,20 @@ async def get_bots_data(self_id: str = "0") -> dict: async def get_hardware_data() -> dict: mem = psutil.virtual_memory() + all_processes = psutil.Process().children(recursive=True) + all_processes.append(psutil.Process()) + + mem_used_bot = 0 + process_mem = {} + for process in all_processes: + try: + ps_name = process.name().replace(".exe", "") + if ps_name not in process_mem: + process_mem[ps_name] = 0 + process_mem[ps_name] += process.memory_info().rss + mem_used_bot += process.memory_info().rss + except Exception: + pass swap = psutil.swap_memory() cpu_brand_raw = cpuinfo.get_cpu_info().get("brand_raw", "Unknown") if "AMD" in cpu_brand_raw: @@ -157,13 +171,16 @@ async def get_hardware_data() -> dict: result = { "cpu" : { "percent": psutil.cpu_percent(), - "name" : f"{brand} {cpuinfo.get_cpu_info().get('arch', 'Unknown')}" + "name" : f"{brand} {cpuinfo.get_cpu_info().get('arch', 'Unknown')}", + "cores" : psutil.cpu_count(logical=False), + "threads": psutil.cpu_count(logical=True), + "freq" : psutil.cpu_freq().current # MHz }, - "mem" : { + "memory" : { "percent": mem.percent, "total" : mem.total, "used" : mem.used, - "free" : mem.free + "free" : mem.free, }, "swap": { "percent": swap.percent, diff --git a/liteyuki/resources/templates/js/status.js b/liteyuki/resources/templates/js/status.js index 77f5dc3d..45825cc6 100644 --- a/liteyuki/resources/templates/js/status.js +++ b/liteyuki/resources/templates/js/status.js @@ -59,6 +59,32 @@ function createPieChartOption(title, data) { } } + +function convertSize(size, precision = 2, addUnit = true, suffix = " XiB"): string { + let isNegative = size < 0; + size = Math.abs(size); + let units = ["", "K", "M", "G", "T", "P", "E", "Z"]; + let unit = ""; + + for (let i = 0; i < units.length; i++) { + if (size < 1024) { + unit = units[i]; + break; + } + size /= 1024; + } + + if (isNegative) { + size = -size; + } + + if (addUnit) { + return size.toFixed(precision) + suffix.replace('X', unit); + } else { + return size; + } +} + /** * 创建磁盘用量柱状图 * @param title @@ -141,6 +167,29 @@ function main() { document.body.insertBefore(liteyukiInfoDiv, document.getElementById('hardware-info')) // 插入对象 // 添加硬件信息 + const cpuData = hardwareData['cpu'] + const memData = hardwareData['memory'] + const swapData = hardwareData['swap'] + + const cpuTagArray = [ + cpuData['name'], + `${cpuData['cores']}C ${cpuData['threads']}T`, + `${(cpuData['freq'] / 1000).toFixed(2)}GHz` + ] + + const memTagArray = [ + `Bot ${convertSize(memData['bot'])}`, + `${localData['used']} ${convertSize(memData['used'])}`, + `${localData['free']} ${convertSize(memData['free'])}`, + `${localData['total']} ${convertSize(memData['total'])}` + ] + + const swapTagArray = [ + `${localData['used']} ${convertSize(swapData['used'])}`, + `${localData['free']} ${convertSize(swapData['free'])}`, + `${localData['total']} ${convertSize(swapData['total'])}` + ] + } main() \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index d593ac3a..d0841b6b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,14 +4,13 @@ colored==2.2.4 dash==2.16.1 GitPython==3.1.42 httpx==0.27.0 -jieba==0.42.1 nb-cli==1.4.1 nonebot2[fastapi,httpx,websockets]==2.2.1 nonebot-plugin-htmlrender==0.3.1 nonebot-adapter-onebot==2.4.3 nonebot-plugin-alconna==0.43.0 nonebot_plugin_apscheduler==0.4.0 -packaging==21.4 +packaging==23.1 playwright==1.17.2 psutil==5.9.8 py-cpuinfo==9.0.0