11from __future__ import annotations
22
33import time
4+ from dataclasses import dataclass
45from typing import TYPE_CHECKING
56
67import psutil
1617router = Router (name = "raito.system.stats" )
1718
1819
19- def get_process_stats () -> dict :
20+ @dataclass
21+ class MemoryInformation :
22+ rss_mb : float
23+ vms_mb : float
24+
25+
26+ @dataclass
27+ class ProcessStats :
28+ uptime_sec : int
29+ memory : MemoryInformation
30+ cpu_percent : float
31+
32+
33+ def get_process_stats () -> ProcessStats :
2034 proc = psutil .Process ()
2135
2236 with proc .oneshot ():
2337 mem_info = proc .memory_info ()
2438 cpu_percent = proc .cpu_percent (interval = 0.1 )
2539 create_time = proc .create_time ()
2640
27- return {
28- " uptime_sec" : time .time () - create_time ,
29- " memory" : {
30- " rss_mb" : mem_info .rss / 1024 ** 2 ,
31- " vms_mb" : mem_info .vms / 1024 ** 2 ,
32- } ,
33- " cpu_percent" : cpu_percent ,
34- }
41+ return ProcessStats (
42+ uptime_sec = int ( time .time () - create_time ) ,
43+ memory = MemoryInformation (
44+ rss_mb = mem_info .rss / 1024 ** 2 ,
45+ vms_mb = mem_info .vms / 1024 ** 2 ,
46+ ) ,
47+ cpu_percent = cpu_percent ,
48+ )
3549
3650
3751def strf_seconds (seconds : int ) -> str :
@@ -54,16 +68,13 @@ def strf_seconds(seconds: int) -> str:
5468async def stats (message : Message ) -> None :
5569 stats = get_process_stats ()
5670
57- text = (
58- html .bold ("Process stats" )
59- + "\n "
60- + "CPU: "
61- + "{:.2f}" .format (stats ["cpu_percent" ])
62- + "%\n "
63- + "RAM: "
64- + "{:.2f}" .format (stats ["memory" ]["rss_mb" ])
65- + "Mb\n "
66- + "Uptime: "
67- + strf_seconds (int (stats ["uptime_sec" ]))
71+ text = "\n " .join (
72+ [
73+ html .bold ("Process stats" ),
74+ "" ,
75+ f"• CPU: { stats .cpu_percent :.2f} %" ,
76+ f"• RAM: { stats .memory .rss_mb :.2f} mb" ,
77+ f"• Uptime: { strf_seconds (stats .uptime_sec )} " ,
78+ ]
6879 )
6980 await message .answer (text = text , parse_mode = "HTML" )
0 commit comments