开摆(0.4.7

This commit is contained in:
shenjack 2024-02-22 19:08:09 +08:00
parent ef61b3a6b4
commit f1abfd4f9d
Signed by: shenjack
GPG Key ID: 7B1134A979775551
4 changed files with 68 additions and 66 deletions

View File

@ -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

View File

@ -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)

View File

@ -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<Py<PyAny>> = Python::with_gil(|py| -> PyResult<Py<PyAny>> {
let module: PyResult<Py<PyAny>> = 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<PyAny> {
let module: Py<PyAny> = 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<SystemTime>, Stri
Ok((changed_time, content))
}
pub fn load_py_module(path: &PathBuf) -> Option<(Option<SystemTime>, Py<PyAny>)> {
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<Py<PyAny>> = Python::with_gil(|py| -> PyResult<Py<PyAny>> {
let module: PyResult<Py<PyAny>> = 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();

View File

@ -1,5 +1,9 @@
# 更新日志
## 0.4.7
修复了重载时如果代码有问题会直接 panic 的问题
## 0.4.6
现在更适合部署了