1
0
forked from bot/app

优化状态卡片速度

This commit is contained in:
远野千束 2024-06-01 15:32:41 +08:00
parent 4cdf29557c
commit be28116a98

View File

@ -16,18 +16,19 @@ from liteyuki.utils.message.html_tool import template2image
from liteyuki.utils import satori_utils from liteyuki.utils import satori_utils
from .counter_for_satori import satori_counter from .counter_for_satori import satori_counter
from git import Repo from git import Repo
require("nonebot_plugin_apscheduler") require("nonebot_plugin_apscheduler")
from nonebot_plugin_apscheduler import scheduler from nonebot_plugin_apscheduler import scheduler
commit_hash = Repo(".").head.commit.hexsha commit_hash = Repo(".").head.commit.hexsha
protocol_names = { protocol_names = {
0: "iPad", 0: "iPad",
1: "Android Phone", 1: "Android Phone",
2: "Android Watch", 2: "Android Watch",
3: "Mac", 3: "Mac",
5: "iPad", 5: "iPad",
6: "Android Pad", 6: "Android Pad",
} }
""" """
@ -84,19 +85,19 @@ async def refresh_status_card():
async def generate_status_card(bot: dict, hardware: dict, liteyuki: dict, lang="zh-CN", bot_id="0", async def generate_status_card(bot: dict, hardware: dict, liteyuki: dict, lang="zh-CN", bot_id="0",
use_cache=False) -> bytes: use_cache=False
) -> bytes:
if not use_cache: if not use_cache:
return await template2image( return await template2image(
get_path("templates/status.html", abs_path=True), get_path("templates/status.html", abs_path=True),
{ {
"data": { "data": {
"bot": bot, "bot" : bot,
"hardware": hardware, "hardware" : hardware,
"liteyuki": liteyuki, "liteyuki" : liteyuki,
"localization": get_local_data(lang) "localization": get_local_data(lang)
} }
}, },
debug=True
) )
else: else:
if lang not in status_card_cache: if lang not in status_card_cache:
@ -107,32 +108,32 @@ async def generate_status_card(bot: dict, hardware: dict, liteyuki: dict, lang="
def get_local_data(lang_code) -> dict: def get_local_data(lang_code) -> dict:
lang = Language(lang_code) lang = Language(lang_code)
return { return {
"friends": lang.get("status.friends"), "friends" : lang.get("status.friends"),
"groups": lang.get("status.groups"), "groups" : lang.get("status.groups"),
"plugins": lang.get("status.plugins"), "plugins" : lang.get("status.plugins"),
"bots": lang.get("status.bots"), "bots" : lang.get("status.bots"),
"message_sent": lang.get("status.message_sent"), "message_sent" : lang.get("status.message_sent"),
"message_received": lang.get("status.message_received"), "message_received": lang.get("status.message_received"),
"cpu": lang.get("status.cpu"), "cpu" : lang.get("status.cpu"),
"memory": lang.get("status.memory"), "memory" : lang.get("status.memory"),
"swap": lang.get("status.swap"), "swap" : lang.get("status.swap"),
"disk": lang.get("status.disk"), "disk" : lang.get("status.disk"),
"usage": lang.get("status.usage"), "usage" : lang.get("status.usage"),
"total": lang.get("status.total"), "total" : lang.get("status.total"),
"used": lang.get("status.used"), "used" : lang.get("status.used"),
"free": lang.get("status.free"), "free" : lang.get("status.free"),
"days": lang.get("status.days"), "days" : lang.get("status.days"),
"hours": lang.get("status.hours"), "hours" : lang.get("status.hours"),
"minutes": lang.get("status.minutes"), "minutes" : lang.get("status.minutes"),
"seconds": lang.get("status.seconds"), "seconds" : lang.get("status.seconds"),
"runtime": lang.get("status.runtime"), "runtime" : lang.get("status.runtime"),
"threads": lang.get("status.threads"), "threads" : lang.get("status.threads"),
"cores": lang.get("status.cores"), "cores" : lang.get("status.cores"),
"process": lang.get("status.process"), "process" : lang.get("status.process"),
"resources": lang.get("status.resources"), "resources" : lang.get("status.resources"),
"description": lang.get("status.description"), "description" : lang.get("status.description"),
} }
@ -141,8 +142,8 @@ async def get_bots_data(self_id: str = "0") -> dict:
Returns: Returns:
""" """
result = { result = {
"self_id": self_id, "self_id": self_id,
"bots": [], "bots" : [],
} }
for bot_id, bot in nonebot.get_bots().items(): for bot_id, bot in nonebot.get_bots().items():
groups = 0 groups = 0
@ -180,15 +181,15 @@ async def get_bots_data(self_id: str = "0") -> dict:
else: else:
icon = None icon = None
bot_data = { bot_data = {
"name": bot_name, "name" : bot_name,
"icon": icon, "icon" : icon,
"id": bot_id, "id" : bot_id,
"protocol_name": protocol_names.get(version_info.get("protocol_name"), "Online"), "protocol_name" : protocol_names.get(version_info.get("protocol_name"), "Online"),
"groups": groups, "groups" : groups,
"friends": friends, "friends" : friends,
"message_sent": satori_counter.msg_sent if isinstance(bot, satori.Bot) else statistics.get("message_sent", 0), "message_sent" : satori_counter.msg_sent if isinstance(bot, satori.Bot) else statistics.get("message_sent", 0),
"message_received": satori_counter.msg_received if isinstance(bot, satori.Bot) else statistics.get("message_received", 0), "message_received": satori_counter.msg_received if isinstance(bot, satori.Bot) else statistics.get("message_received", 0),
"app_name": app_name "app_name" : app_name
} }
result["bots"].append(bot_data) result["bots"].append(bot_data)
@ -220,27 +221,27 @@ async def get_hardware_data() -> dict:
else: else:
brand = "Unknown" brand = "Unknown"
result = { result = {
"cpu": { "cpu" : {
"percent": psutil.cpu_percent(), "percent": psutil.cpu_percent(),
"name": f"{brand} {cpuinfo.get_cpu_info().get('arch', 'Unknown')}", "name" : f"{brand} {cpuinfo.get_cpu_info().get('arch', 'Unknown')}",
"cores": psutil.cpu_count(logical=False), "cores" : psutil.cpu_count(logical=False),
"threads": psutil.cpu_count(logical=True), "threads": psutil.cpu_count(logical=True),
"freq": psutil.cpu_freq().current # MHz "freq" : psutil.cpu_freq().current # MHz
}, },
"memory": { "memory": {
"percent": mem.percent, "percent" : mem.percent,
"total": mem.total, "total" : mem.total,
"used": mem.used, "used" : mem.used,
"free": mem.free, "free" : mem.free,
"usedProcess": mem_used_process, "usedProcess": mem_used_process,
}, },
"swap": { "swap" : {
"percent": swap.percent, "percent": swap.percent,
"total": swap.total, "total" : swap.total,
"used": swap.used, "used" : swap.used,
"free": swap.free "free" : swap.free
}, },
"disk": [], "disk" : [],
} }
for disk in psutil.disk_partitions(all=True): for disk in psutil.disk_partitions(all=True):
@ -249,11 +250,11 @@ async def get_hardware_data() -> dict:
if disk_usage.total == 0: if disk_usage.total == 0:
continue # 虚拟磁盘 continue # 虚拟磁盘
result["disk"].append({ result["disk"].append({
"name": disk.mountpoint, "name" : disk.mountpoint,
"percent": disk_usage.percent, "percent": disk_usage.percent,
"total": disk_usage.total, "total" : disk_usage.total,
"used": disk_usage.used, "used" : disk_usage.used,
"free": disk_usage.free "free" : disk_usage.free
}) })
except: except:
pass pass
@ -264,14 +265,14 @@ async def get_hardware_data() -> dict:
async def get_liteyuki_data() -> dict: async def get_liteyuki_data() -> dict:
temp_data: TempConfig = common_db.where_one(TempConfig(), default=TempConfig()) temp_data: TempConfig = common_db.where_one(TempConfig(), default=TempConfig())
result = { result = {
"name": list(get_config("nickname", [__NAME__]))[0], "name" : list(get_config("nickname", [__NAME__]))[0],
"version": f"{__VERSION__}({commit_hash[:7] if (commit_hash and len(commit_hash) > 8) else ''})", "version" : f"{__VERSION__}{'-' + commit_hash[:7] if (commit_hash and len(commit_hash) > 8) else ''}",
"plugins": len(nonebot.get_loaded_plugins()), "plugins" : len(nonebot.get_loaded_plugins()),
"resources": len(get_loaded_resource_packs()), "resources": len(get_loaded_resource_packs()),
"nonebot": f"{nonebot.__version__}", "nonebot" : f"{nonebot.__version__}",
"python": f"{platform.python_implementation()} {platform.python_version()}", "python" : f"{platform.python_implementation()} {platform.python_version()}",
"system": f"{platform.system()} {platform.release()}", "system" : f"{platform.system()} {platform.release()}",
"runtime": time.time() - temp_data.data.get("start_time", time.time()), # 运行时间秒数 "runtime" : time.time() - temp_data.data.get("start_time", time.time()), # 运行时间秒数
"bots": len(nonebot.get_bots()) "bots" : len(nonebot.get_bots())
} }
return result return result