From 75e7e0085a190acaf1eea7ff6a95f929041a3d18 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Thu, 22 Feb 2024 13:06:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8Arust=EF=BC=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ica-rs/ica_typing.py | 7 ++- ica-rs/plugins/{test.py => base.py} | 7 +-- ica-rs/plugins/bmcl.py | 86 +++++++++++++++++++++++++++++ ica-rs/src/py/class.rs | 13 +++++ 4 files changed, 106 insertions(+), 7 deletions(-) rename ica-rs/plugins/{test.py => base.py} (73%) create mode 100644 ica-rs/plugins/bmcl.py diff --git a/ica-rs/ica_typing.py b/ica-rs/ica_typing.py index 0462e46..1c411f5 100644 --- a/ica-rs/ica_typing.py +++ b/ica-rs/ica_typing.py @@ -61,9 +61,14 @@ class IcaClient: @staticmethod async def send_message_a(client: "IcaClient", message: SendMessage) -> bool: ... - def send_message(self, message: SendMessage) -> bool: ... + def debug(self, message: str) -> None: + ... + def info(self, message: str) -> None: + ... + def warn(self, message: str) -> None: + ... def on_message(msg: NewMessage, client: IcaClient) -> None: diff --git a/ica-rs/plugins/test.py b/ica-rs/plugins/base.py similarity index 73% rename from ica-rs/plugins/test.py rename to ica-rs/plugins/base.py index e5e5f36..7258a86 100644 --- a/ica-rs/plugins/test.py +++ b/ica-rs/plugins/base.py @@ -6,14 +6,9 @@ else: NewMessage = TypeVar("NewMessage") IcaClient = TypeVar("IcaClient") -_version_ = "0.1.0" +_version_ = "1.0.0" def on_message(msg: NewMessage, client: IcaClient) -> None: - # print(msg) - # reply = msg.reply_with("Hello, world!") - # print(reply) - # print(msg.content) - # print(msg.is_from_self) if not msg.is_from_self: if msg.content == "/bot-rs-py": reply = msg.reply_with(f"ica-async-rs-sync-py {_version_}") diff --git a/ica-rs/plugins/bmcl.py b/ica-rs/plugins/bmcl.py new file mode 100644 index 0000000..24d86ee --- /dev/null +++ b/ica-rs/plugins/bmcl.py @@ -0,0 +1,86 @@ + +import time +import requests + +from typing import TYPE_CHECKING, TypeVar + +if TYPE_CHECKING: + from ica_typing import NewMessage, IcaClient +else: + NewMessage = TypeVar("NewMessage") + IcaClient = TypeVar("IcaClient") + +_version_ = "2.0.0-rs" + +def format_data_size(data_bytes: float) -> str: + data_lens = ["B", "KB", "MB", "GB", "TB"] + data_len = "0B" + for i in range(5): + if data_bytes < 1024: + data_bytes = round(data_bytes, 5) + data_len = f"{data_bytes}{data_lens[i]}" + break + else: + data_bytes /= 1024 + return data_len + + +def format_hit_count(count: int) -> str: + """数据分段, 四位一个下划线 + + Args: + count (int): 数据 + + Returns: + str: 格式化后的数据 + 1 -> 1 + 1000 -> 1000 + 10000 -> 1_0000 + 100000 -> 10_0000 + 1000000 -> 100_0000 + """ + count_str = str(count) + count_len = len(count_str) + if count_len <= 4: + return count_str + else: + return "_".join(count_str[i:i + 4] for i in range(0, count_len, 4)) + +def bmcl(msg: NewMessage, client: IcaClient) -> None: + req_time = time.time() + # 记录请求时间 + response = requests.get("https://bd.bangbang93.com/openbmclapi/metric/dashboard") + if not response.status_code == 200 or response.reason != "OK": + reply = msg.reply_with(f"请求数据失败\n{response.status_code}") + client.warn( + f"数据请求失败, 请检查网络\n{response.status}" + ) + client.send_message(reply) + return + data = response.json() + data_bytes: float = data["bytes"] + data_hits: int = data["hits"] + data_bandwidth: float = data["currentBandwidth"] + load_str: float = data["load"] * 100 + online_node: int = data["currentNodes"] + online_bandwidth: int = data["bandwidth"] + data_len = format_data_size(data_bytes) + hits_count = format_hit_count(data_hits) + + report_msg = ( + f"OpenBMCLAPI 状态面板v{_version_} :\n" + f"实时信息: {online_node} 带宽: {online_bandwidth}Mbps\n" + f"负载: {load_str:.2f}% 带宽: {data_bandwidth:.2f}Mbps\n" + f"当日请求: {hits_count} 数据量: {data_len}\n" + f"请求时间: {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(req_time))}\n" + "数据源: https://bd.bangbang93.com/pages/dashboard" + ) + client.info(report_msg) + reply = msg.reply_with(report_msg) + client.send_message(reply) + + +def on_message(msg: NewMessage, client: IcaClient) -> None: + if not msg.is_from_self: + if msg.content == "/bmcl-rs": + bmcl(msg, client) diff --git a/ica-rs/src/py/class.rs b/ica-rs/src/py/class.rs index c6afccd..60431bc 100644 --- a/ica-rs/src/py/class.rs +++ b/ica-rs/src/py/class.rs @@ -1,4 +1,5 @@ use pyo3::prelude::*; +use tracing::{debug, info, warn}; use rust_socketio::asynchronous::Client; use tokio::runtime::Runtime; @@ -225,6 +226,18 @@ impl IcaClientPy { Ok(send_message(&client.client, &message.msg).await) }) } + + pub fn debug(&self, content: String) { + debug!("{}", content); + } + + pub fn info(&self, content: String) { + info!("{}", content); + } + + pub fn warn(&self, content: String) { + warn!("{}", content); + } } impl IcaClientPy {