mirror of
http://shenjack.top:5100/shenjack/icalingua-python-bot.git
synced 2024-11-23 12:41:05 +08:00
开摆(0.4.7
This commit is contained in:
parent
ef61b3a6b4
commit
f1abfd4f9d
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "ica-rs"
|
name = "ica-rs"
|
||||||
version = "0.4.6"
|
version = "0.4.7"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
|
@ -10,7 +10,7 @@ else:
|
||||||
NewMessage = TypeVar("NewMessage")
|
NewMessage = TypeVar("NewMessage")
|
||||||
IcaClient = TypeVar("IcaClient")
|
IcaClient = TypeVar("IcaClient")
|
||||||
|
|
||||||
_version_ = "2.1.0-rs"
|
_version_ = "2.1.1-rs"
|
||||||
|
|
||||||
def format_data_size(data_bytes: float) -> str:
|
def format_data_size(data_bytes: float) -> str:
|
||||||
data_lens = ["B", "KB", "MB", "GB", "TB"]
|
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:
|
def parse_rank(data: dict) -> dict:
|
||||||
rank_data = {"hits": 0, "bytes": 0}
|
rank_data = {"hits": 0, "bytes": 0}
|
||||||
if "metric" in rank_data:
|
if "metric" in data:
|
||||||
rank_data["hits"] = data["metric"]["hits"]
|
rank_data["hits"] = data["metric"]["hits"]
|
||||||
rank_data["bytes"] = data["metric"]["bytes"]
|
rank_data["bytes"] = data["metric"]["bytes"]
|
||||||
return {
|
return {
|
||||||
"name": data["name"],
|
"name": data["name"],
|
||||||
"start": data["isEnabled"],
|
"start": data["isEnabled"],
|
||||||
"full": data["fullSize"],
|
"full": "全量" if "fullSize" in data else "分片",
|
||||||
"version": data["version"] if "version" in data else "未知版本",
|
# "version": data["version"] if "version" in data else "未知版本",
|
||||||
"owner": data["user"]["name"],
|
"owner": data["sponsor"]["name"] if "sponsor" in data else "未知用户",
|
||||||
"rank": rank_data
|
"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]
|
ranks = [parse_rank(data) for data in rank_data]
|
||||||
if name is None:
|
if name is None:
|
||||||
# 全部排名
|
# 全部排名
|
||||||
# 显示前10名
|
# 显示前3名
|
||||||
if len(ranks) < 10:
|
limit = 3
|
||||||
|
if len(ranks) < limit:
|
||||||
show_ranks = ranks
|
show_ranks = ranks
|
||||||
else:
|
else:
|
||||||
show_ranks = ranks[:10]
|
show_ranks = ranks[:limit]
|
||||||
rank_msg = (
|
rank_msg = (
|
||||||
f"名称: {r["name"]}-{"全" if r["full"] else "分"} 版本: {r["version"]}\n"
|
f"名称: {r['name']}\n"
|
||||||
f"拥有者: {r["owner"]} 状态: {r["start"]}\n"
|
# f"-{rank['full']} \n"
|
||||||
f"h/d {format_hit_count(r["rank"]["hits"])}|{format_data_size(r["rank"]["bytes"])}\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
|
for r in show_ranks
|
||||||
)
|
)
|
||||||
|
rank_msg = "\n".join(rank_msg)
|
||||||
report_msg = (
|
report_msg = (
|
||||||
f"OpenBMCLAPI 面板v{_version_}-排名\n"
|
f"OpenBMCLAPI 面板v{_version_}-排名\n{rank_msg}\n"
|
||||||
f"{'\n'.join(rank_msg)}"
|
|
||||||
f"请求时间: {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(req_time))}\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)
|
reply = msg.reply_with(report_msg)
|
||||||
client.info(report_msg)
|
client.info(report_msg)
|
||||||
|
@ -133,8 +135,8 @@ def bmcl_rank(msg: NewMessage, client: IcaClient, name: Optional[str]) -> None:
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
# 搜索是否有这个名字的节点
|
# 搜索是否有这个名字的节点
|
||||||
names = [r["name"] for r in ranks]
|
names = [r["name"].lower() for r in ranks]
|
||||||
finds = [re.search(name, n) for n in names]
|
finds = [re.search(name.lower(), n) for n in names]
|
||||||
if not finds:
|
if not finds:
|
||||||
reply = msg.reply_with(f"未找到名为{name}的节点")
|
reply = msg.reply_with(f"未找到名为{name}的节点")
|
||||||
client.send_message(reply)
|
client.send_message(reply)
|
||||||
|
@ -149,16 +151,13 @@ def bmcl_rank(msg: NewMessage, client: IcaClient, name: Optional[str]) -> None:
|
||||||
if find:
|
if find:
|
||||||
rank = ranks[i]
|
rank = ranks[i]
|
||||||
rank_msg = (
|
rank_msg = (
|
||||||
f"名称: {rank["name"]}-{"全" if rank["full"] else "分"} 版本: {rank["version"]}\n"
|
f"名称: {rank['name']}\n"
|
||||||
f"拥有者: {rank["owner"]} 状态: {rank["start"]}\n"
|
# f"-{rank['full']} \n"
|
||||||
f"h/d {format_hit_count(rank["rank"]["hits"])}|{format_data_size(rank["rank"]["bytes"])}\n"
|
# f"版本: {rank['version']}\n"
|
||||||
)
|
f"拥有者: {rank['owner']} 状态: {rank['start']}|"
|
||||||
report_msg = (
|
f"h/d {format_hit_count(rank['rank']['hits'])}|{format_data_size(rank['rank']['bytes'])}"
|
||||||
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"
|
|
||||||
)
|
)
|
||||||
|
report_msg = f"OpenBMCLAPI 面板v{_version_}-排名\n{rank_msg}\n"
|
||||||
reply = msg.reply_with(report_msg)
|
reply = msg.reply_with(report_msg)
|
||||||
client.info(report_msg)
|
client.info(report_msg)
|
||||||
client.send_message(reply)
|
client.send_message(reply)
|
||||||
|
|
|
@ -82,30 +82,12 @@ pub fn load_py_plugins(path: &PathBuf) {
|
||||||
let path = entry.path();
|
let path = entry.path();
|
||||||
if let Some(ext) = path.extension() {
|
if let Some(ext) = path.extension() {
|
||||||
if ext == "py" {
|
if ext == "py" {
|
||||||
match load_py_file(&path) {
|
match load_py_module(&path) {
|
||||||
Ok((changed_time, content)) => {
|
Some((changed_time, py_module)) => {
|
||||||
let py_module: PyResult<Py<PyAny>> = Python::with_gil(|py| -> PyResult<Py<PyAny>> {
|
PyStatus::add_file(path.clone(), changed_time, py_module);
|
||||||
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) => {
|
None => {
|
||||||
warn!("failed to load file: {:?} | e: {:?}", path, e);
|
warn!("加载 Python 插件: {:?} 失败", path);
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
warn!("failed to load file: {:?} | e: {:?}", path, e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -145,24 +127,12 @@ pub fn verify_plugins() {
|
||||||
}
|
}
|
||||||
info!("file change list: {:?}", need_reload_files);
|
info!("file change list: {:?}", need_reload_files);
|
||||||
for reload_file in need_reload_files {
|
for reload_file in need_reload_files {
|
||||||
match load_py_file(&reload_file) {
|
match load_py_module(&reload_file) {
|
||||||
Ok((changed_time, content)) => {
|
Some((changed_time, py_module)) => {
|
||||||
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
|
|
||||||
});
|
|
||||||
PyStatus::add_file(reload_file.clone(), changed_time, py_module);
|
PyStatus::add_file(reload_file.clone(), changed_time, py_module);
|
||||||
},
|
}
|
||||||
Err(e) => {
|
None => {
|
||||||
warn!("重载 Python 插件: {:?} 失败, e: {:?}", reload_file, e);
|
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))
|
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) {
|
pub fn init_py(config: &IcaConfig) {
|
||||||
debug!("initing python threads");
|
debug!("initing python threads");
|
||||||
pyo3::prepare_freethreaded_python();
|
pyo3::prepare_freethreaded_python();
|
||||||
|
|
Loading…
Reference in New Issue
Block a user