加入装饰器,进行指令数据统计

This commit is contained in:
liuwei
2025-03-19 11:55:31 +08:00
parent 443c6d7d8e
commit e5a6d09cf9
3 changed files with 54 additions and 35 deletions

View File

@@ -40,6 +40,8 @@ class MessageSummaryPlugin(MessagePluginInterface):
def __init__(self):
super().__init__()
# 在初始化时动态应用装饰器
self.process_message = plugin_stats_decorator(plugin_name=self.name)(self.process_message)
def initialize(self, context: Dict[str, Any]) -> bool:
"""初始化插件"""

View File

@@ -7,6 +7,7 @@ from wcferry import Wcf
from plugin_common.message_plugin_interface import MessagePluginInterface
from plugin_common.plugin_interface import PluginStatus
from plugins.stats_collector.decorators import plugin_stats_decorator
from robot_cmd.robot_command import Feature, PermissionStatus, GroupBotManager
@@ -39,6 +40,8 @@ class MusicPlugin(MessagePluginInterface):
def __init__(self):
super().__init__()
# 在初始化时动态应用装饰器
self.process_message = plugin_stats_decorator(plugin_name=self.name)(self.process_message)
def initialize(self, context: Dict[str, Any]) -> bool:
"""初始化插件"""

View File

@@ -1,10 +1,11 @@
import functools
import time
import traceback
from datetime import datetime
from typing import Callable, Dict, Any, Tuple
from event_system.event_manager import EventManager
from event_system.events.plugin_events import PluginCallStartEvent, PluginCallEndEvent, PluginCallErrorEvent
from db.stats_db import StatsDBOperator
from db.connection import DBConnectionManager
def plugin_stats_decorator(plugin_name: str) -> Callable:
@@ -19,53 +20,66 @@ def plugin_stats_decorator(plugin_name: str) -> Callable:
def decorator(func: Callable) -> Callable:
@functools.wraps(func)
def wrapper(self, message: Dict[str, Any]) -> Tuple[bool, str]:
# 获取事件管理器
event_manager = EventManager.get_instance()
# 获取数据库连接
db_manager = DBConnectionManager.get_instance()
stats_db = StatsDBOperator(db_manager)
# 提取消息信息
content = message.get("content", "")
sender = message.get("sender", "")
roomid = message.get("roomid", "")
# 发布插件调用开始事件
start_time = datetime.now()
event_manager.publish(PluginCallStartEvent, {
"plugin_name": plugin_name,
"command": content,
"user_id": sender,
"group_id": roomid,
"start_time": start_time
})
# 记录开始时间
start_time = time.time()
try:
# 调用原始方法
success, response = func(self, message)
# 发布插件调用结束事件
end_time = datetime.now()
event_manager.publish(PluginCallEndEvent, {
"plugin_name": plugin_name,
"command": content,
"user_id": sender,
"group_id": roomid,
"start_time": start_time,
"end_time": end_time,
"success": success,
"response": response
})
# 计算执行时间(毫秒)
process_time_ms = (time.time() - start_time) * 1000
# 记录插件调用
stats_db.record_plugin_call(
plugin_name=plugin_name,
command=content,
user_id=sender,
group_id=roomid,
success=success,
process_time_ms=process_time_ms
)
return success, response
except Exception as e:
# 发布插件调用错误事件
event_manager.publish(PluginCallErrorEvent, {
"plugin_name": plugin_name,
"command": content,
"user_id": sender,
"group_id": roomid,
"start_time": start_time,
"error_message": str(e),
"stack_trace": traceback.format_exc()
})
# 计算执行时间(毫秒)
process_time_ms = (time.time() - start_time) * 1000
# 记录错误
error_message = str(e)
stack_trace = traceback.format_exc()
try:
# 记录插件调用(失败)
stats_db.record_plugin_call(
plugin_name=plugin_name,
command=content,
user_id=sender,
group_id=roomid,
success=False,
process_time_ms=process_time_ms
)
# 记录错误详情
stats_db.record_error(
plugin_name=plugin_name,
command=content,
user_id=sender,
group_id=roomid,
error_message=error_message,
stack_trace=stack_trace
)
except Exception as db_error:
print(f"记录插件统计数据失败: {db_error}")
# 重新抛出异常,让上层处理
raise