From f1abfd4f9d0dbf791d9bd3e0abae2b462a309948 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Thu, 22 Feb 2024 19:08:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=80=E6=91=86(0.4.7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ica-rs/Cargo.toml | 2 +- ica-rs/plugins/bmcl.py | 49 +++++++++++++------------- ica-rs/src/py/mod.rs | 79 +++++++++++++++++++++--------------------- news.md | 4 +++ 4 files changed, 68 insertions(+), 66 deletions(-) diff --git a/ica-rs/Cargo.toml b/ica-rs/Cargo.toml index 9a7e897..32ff852 100644 --- a/ica-rs/Cargo.toml +++ b/ica-rs/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ica-rs" -version = "0.4.6" +version = "0.4.7" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/ica-rs/plugins/bmcl.py b/ica-rs/plugins/bmcl.py index 121c954..71c38c6 100644 --- a/ica-rs/plugins/bmcl.py +++ b/ica-rs/plugins/bmcl.py @@ -10,7 +10,7 @@ else: NewMessage = TypeVar("NewMessage") IcaClient = TypeVar("IcaClient") -_version_ = "2.1.0-rs" +_version_ = "2.1.1-rs" def format_data_size(data_bytes: float) -> str: data_lens = ["B", "KB", "MB", "GB", "TB"] @@ -88,15 +88,15 @@ def bmcl_dashboard(msg: NewMessage, client: IcaClient) -> None: def parse_rank(data: dict) -> dict: rank_data = {"hits": 0, "bytes": 0} - if "metric" in rank_data: + if "metric" in data: rank_data["hits"] = data["metric"]["hits"] rank_data["bytes"] = data["metric"]["bytes"] return { "name": data["name"], "start": data["isEnabled"], - "full": data["fullSize"], - "version": data["version"] if "version" in data else "未知版本", - "owner": data["user"]["name"], + "full": "全量" if "fullSize" in data else "分片", + # "version": data["version"] if "version" in data else "未知版本", + "owner": data["sponsor"]["name"] if "sponsor" in data else "未知用户", "rank": rank_data } @@ -110,22 +110,24 @@ def bmcl_rank(msg: NewMessage, client: IcaClient, name: Optional[str]) -> None: ranks = [parse_rank(data) for data in rank_data] if name is None: # 全部排名 - # 显示前10名 - if len(ranks) < 10: + # 显示前3名 + limit = 3 + if len(ranks) < limit: show_ranks = ranks else: - show_ranks = ranks[:10] + show_ranks = ranks[:limit] rank_msg = ( - f"名称: {r["name"]}-{"全" if r["full"] else "分"} 版本: {r["version"]}\n" - f"拥有者: {r["owner"]} 状态: {r["start"]}\n" - f"h/d {format_hit_count(r["rank"]["hits"])}|{format_data_size(r["rank"]["bytes"])}\n" + f"名称: {r['name']}\n" + # f"-{rank['full']} \n" + # f"版本: {r['version']}\n" + f"拥有者: {r['owner']} 状态: {r['start']}|" + f"h/d {format_hit_count(r['rank']['hits'])}|{format_data_size(r['rank']['bytes'])}" for r in show_ranks ) + rank_msg = "\n".join(rank_msg) report_msg = ( - f"OpenBMCLAPI 面板v{_version_}-排名\n" - f"{'\n'.join(rank_msg)}" + f"OpenBMCLAPI 面板v{_version_}-排名\n{rank_msg}\n" f"请求时间: {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(req_time))}\n" - "数据源: https://bd.bangbang93.com/openbmclapi/metric/rank" ) reply = msg.reply_with(report_msg) client.info(report_msg) @@ -133,8 +135,8 @@ def bmcl_rank(msg: NewMessage, client: IcaClient, name: Optional[str]) -> None: return else: # 搜索是否有这个名字的节点 - names = [r["name"] for r in ranks] - finds = [re.search(name, n) for n in names] + names = [r["name"].lower() for r in ranks] + finds = [re.search(name.lower(), n) for n in names] if not finds: reply = msg.reply_with(f"未找到名为{name}的节点") client.send_message(reply) @@ -149,16 +151,13 @@ def bmcl_rank(msg: NewMessage, client: IcaClient, name: Optional[str]) -> None: if find: rank = ranks[i] rank_msg = ( - f"名称: {rank["name"]}-{"全" if rank["full"] else "分"} 版本: {rank["version"]}\n" - f"拥有者: {rank["owner"]} 状态: {rank["start"]}\n" - f"h/d {format_hit_count(rank["rank"]["hits"])}|{format_data_size(rank["rank"]["bytes"])}\n" - ) - report_msg = ( - f"OpenBMCLAPI 面板v{_version_}-排名\n" - f"{rank_msg}" - f"请求时间: {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(req_time))}\n" - "数据源: https://bd.bangbang93.com/openbmclapi/metric/rank" + f"名称: {rank['name']}\n" + # f"-{rank['full']} \n" + # f"版本: {rank['version']}\n" + f"拥有者: {rank['owner']} 状态: {rank['start']}|" + f"h/d {format_hit_count(rank['rank']['hits'])}|{format_data_size(rank['rank']['bytes'])}" ) + report_msg = f"OpenBMCLAPI 面板v{_version_}-排名\n{rank_msg}\n" reply = msg.reply_with(report_msg) client.info(report_msg) client.send_message(reply) diff --git a/ica-rs/src/py/mod.rs b/ica-rs/src/py/mod.rs index 0d400fc..5c0e976 100644 --- a/ica-rs/src/py/mod.rs +++ b/ica-rs/src/py/mod.rs @@ -82,30 +82,12 @@ pub fn load_py_plugins(path: &PathBuf) { let path = entry.path(); if let Some(ext) = path.extension() { if ext == "py" { - match load_py_file(&path) { - Ok((changed_time, content)) => { - let py_module: PyResult> = Python::with_gil(|py| -> PyResult> { - let module: PyResult> = PyModule::from_code( - py, - &content, - &path.to_string_lossy(), - &path.to_string_lossy() - ) - .map(|module| module.into()); - module - }); - match py_module { - Ok(py_module) => { - info!("加载到插件: {:?}", path); - PyStatus::add_file(path, changed_time, py_module); - } - Err(e) => { - warn!("failed to load file: {:?} | e: {:?}", path, e); - } - } + match load_py_module(&path) { + Some((changed_time, py_module)) => { + PyStatus::add_file(path.clone(), changed_time, py_module); } - Err(e) => { - warn!("failed to load file: {:?} | e: {:?}", path, e); + None => { + warn!("加载 Python 插件: {:?} 失败", path); } } } @@ -145,24 +127,12 @@ pub fn verify_plugins() { } info!("file change list: {:?}", need_reload_files); for reload_file in need_reload_files { - match load_py_file(&reload_file) { - Ok((changed_time, content)) => { - let py_module = Python::with_gil(|py| -> Py { - let module: Py = PyModule::from_code( - py, - &content, - &reload_file.to_string_lossy(), - &reload_file.to_string_lossy(), - // !!!! 请注意, 一定要给他一个名字, cpython 会自动把后面的重名模块覆盖掉前面的 - ) - .unwrap() - .into(); - module - }); + match load_py_module(&reload_file) { + Some((changed_time, py_module)) => { PyStatus::add_file(reload_file.clone(), changed_time, py_module); - }, - Err(e) => { - warn!("重载 Python 插件: {:?} 失败, e: {:?}", reload_file, e); + } + None => { + warn!("重载 Python 插件: {:?} 失败", reload_file); } } } @@ -180,6 +150,35 @@ pub fn load_py_file(path: &PathBuf) -> std::io::Result<(Option, Stri Ok((changed_time, content)) } +pub fn load_py_module(path: &PathBuf) -> Option<(Option, Py)> { + let (changed_time, content) = match load_py_file(&path) { + Ok((changed_time, content)) => (changed_time, content), + Err(e) => { + warn!("failed to load file: {:?} | e: {:?}", path, e); + return None; + } + }; + let py_module: PyResult> = Python::with_gil(|py| -> PyResult> { + let module: PyResult> = PyModule::from_code( + py, + &content, + &path.to_string_lossy(), + &path.to_string_lossy(), + // !!!! 请注意, 一定要给他一个名字, cpython 会自动把后面的重名模块覆盖掉前面的 + ).map(|module| module.into()); + module + }); + match py_module { + Ok(py_module) => { + Some((changed_time, py_module)) + } + Err(e) => { + warn!("failed to load file: {:?} | e: {:?}", path, e); + None + } + } +} + pub fn init_py(config: &IcaConfig) { debug!("initing python threads"); pyo3::prepare_freethreaded_python(); diff --git a/news.md b/news.md index 14af412..1b53aec 100644 --- a/news.md +++ b/news.md @@ -1,5 +1,9 @@ # 更新日志 +## 0.4.7 + +修复了重载时如果代码有问题会直接 panic 的问题 + ## 0.4.6 现在更适合部署了