diff --git a/.gitignore b/.gitignore index dbf5bf77..ccc80c43 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,5 @@ .cache/ data/ -config.yml +_config.yml config.example.yml \ No newline at end of file diff --git a/main.py b/main.py index 3e8373b2..583ca4a2 100644 --- a/main.py +++ b/main.py @@ -3,7 +3,7 @@ import nonebot from nonebot.adapters.onebot import v11, v12 from src.utils.config import load_from_yaml -nonebot.init(**load_from_yaml("config.yml").get("nonebot", {})) +nonebot.init(**load_from_yaml("config.yml")) adapters = [v11.Adapter, v12.Adapter] driver = nonebot.get_driver() diff --git a/src/plugins/liteyuki_plugin_main/__init__.py b/src/plugins/liteyuki_plugin_main/__init__.py index a3704386..ec55400c 100644 --- a/src/plugins/liteyuki_plugin_main/__init__.py +++ b/src/plugins/liteyuki_plugin_main/__init__.py @@ -16,4 +16,4 @@ from src.utils.config import config sys_lang = get_system_lang() nonebot.logger.info(sys_lang.get("main.current_language", LANG=sys_lang.get("language.name"))) -nonebot.logger.info(sys_lang.get("main.enable_webdash", URL=f"http://{config['nonebot']['host']}:{config['nonebot']['port']}")) +nonebot.logger.info(sys_lang.get("main.enable_webdash", URL=f"http://127.0.0.1:{config.get('port', 8080)}")) diff --git a/src/plugins/liteyuki_plugin_main/webdash.py b/src/plugins/liteyuki_plugin_main/webdash.py index 3e65e73f..6833e563 100644 --- a/src/plugins/liteyuki_plugin_main/webdash.py +++ b/src/plugins/liteyuki_plugin_main/webdash.py @@ -4,6 +4,7 @@ from dash import Dash, Input, Output, dcc, html from starlette.middleware.wsgi import WSGIMiddleware from src.utils.language import Language +from src.utils.tools import convert_size app = nonebot.get_app() @@ -22,19 +23,20 @@ def get_system_info(): async def system_info(): return get_system_info() + lang = Language() dash_app = Dash(__name__) dash_app.layout = dash_app.layout = html.Div(children=[ - html.H1(children=lang.get("main.monitor.title"), style={ - 'textAlign': 'center' - }), + html.H1(children=lang.get("main.monitor.title"), style={ + 'textAlign': 'center' + }), - dcc.Graph(id='live-update-graph'), - dcc.Interval( - id='interval-component', - interval=1 * 1000, # in milliseconds - n_intervals=0 - ) + dcc.Graph(id='live-update-graph'), + dcc.Interval( + id='interval-component', + interval=1 * 1000, # in milliseconds + n_intervals=0 + ) ]) @@ -51,20 +53,23 @@ def update_graph_live(n): dcc.Graph(id='live-update-graph'), dcc.Interval( id='interval-component', - interval=1 * 1000, # in milliseconds + interval=2 * 1000, # in milliseconds n_intervals=0 ) ]) + mem = psutil.virtual_memory() + cpu_f = psutil.cpu_freq() figure = { 'data' : [ { - 'x' : [lang.get('main.monitor.cpu')], + 'x' : [f"{cpu_f.current / 1000:.2f}GHz {psutil.cpu_count(logical=False)}c{psutil.cpu_count()}t"], 'y' : [system_inf['cpu_percent']], 'type': 'bar', 'name': f"{lang.get('main.monitor.cpu')} {lang.get('main.monitor.usage')}" + }, { - 'x' : [lang.get('main.monitor.memory')], + 'x' : [f"{convert_size(mem.used, add_unit=False)}/{convert_size(mem.total)}({mem.used / mem.total * 100:.2f}%)"], 'y' : [system_inf['memory_percent']], 'type': 'bar', 'name': f"{lang.get('main.monitor.memory')} {lang.get('main.monitor.usage')}" @@ -72,6 +77,12 @@ def update_graph_live(n): ], 'layout': { 'title': lang.get('main.monitor.description'), + # 'xaxis': { + # 'range': [0, 10] + # }, # 设置x轴的范围 + 'yaxis': { + 'range': [0, 100] + }, # 设置y轴的范围 } } return figure diff --git a/src/utils/config.py b/src/utils/config.py index 978c2227..381731cc 100644 --- a/src/utils/config.py +++ b/src/utils/config.py @@ -1,10 +1,32 @@ -from yaml import load, FullLoader +import os + +import nonebot +import yaml +from pydantic import BaseModel config = None +class BasicConfig(BaseModel): + host: str = "127.0.0.1" + port: int = 20216 + superusers: list[str] = [] + command_start: list[str] = ["/", ""] + nickname: set[str] = {"Liteyuki"} + + def load_from_yaml(file: str) -> dict: + nonebot.logger.debug("Loading config from %s" % file) global config + if not os.path.exists(file): + nonebot.logger.warning(f'Config file {file} not found, created default config, please modify it and restart') + with open(file, 'w', encoding='utf-8') as f: + yaml.dump(BasicConfig().dict(), f, default_flow_style=False) + with open(file, 'r', encoding='utf-8') as f: - config = load(f, Loader=FullLoader) - return config + conf = yaml.load(f, Loader=yaml.FullLoader) + config = conf + if conf is None: + nonebot.logger.warning(f'Config file {file} is empty, use default config. please modify it and restart') + conf = BasicConfig().dict() + return conf diff --git a/src/utils/tools.py b/src/utils/tools.py new file mode 100644 index 00000000..190b63ac --- /dev/null +++ b/src/utils/tools.py @@ -0,0 +1,31 @@ +def convert_size(size: int, precision: int = 2, add_unit: bool = True, suffix: str = "iB") -> str: + """把字节数转换为人类可读的字符串,计算正负 + + Args: + + add_unit: 是否添加单位,False后则suffix无效 + suffix: iB或B + precision: 浮点数的小数点位数 + size (int): 字节数 + + Returns: + + str: The human-readable string, e.g. "1.23 GB". + """ + is_negative = False + if size < 0: + is_negative = True + size = -size + + for unit in ["", "K", "M", "G", "T", "P", "E", "Z", "Y"]: + if size < 1024: + if add_unit: + result = f"{size:.{precision}f} {unit}" + suffix + return f"-{result}" if is_negative else result + else: + return f"{size:.{precision}f}" + size /= 1024 + if add_unit: + return f"{size:.{precision}f} Y" + suffix + else: + return f"{size:.{precision}f}"