加入装饰器,进行指令数据统计
This commit is contained in:
@@ -40,6 +40,8 @@ class MessageSummaryPlugin(MessagePluginInterface):
|
|||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
# 在初始化时动态应用装饰器
|
||||||
|
self.process_message = plugin_stats_decorator(plugin_name=self.name)(self.process_message)
|
||||||
|
|
||||||
def initialize(self, context: Dict[str, Any]) -> bool:
|
def initialize(self, context: Dict[str, Any]) -> bool:
|
||||||
"""初始化插件"""
|
"""初始化插件"""
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ from wcferry import Wcf
|
|||||||
|
|
||||||
from plugin_common.message_plugin_interface import MessagePluginInterface
|
from plugin_common.message_plugin_interface import MessagePluginInterface
|
||||||
from plugin_common.plugin_interface import PluginStatus
|
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
|
from robot_cmd.robot_command import Feature, PermissionStatus, GroupBotManager
|
||||||
|
|
||||||
|
|
||||||
@@ -39,6 +40,8 @@ class MusicPlugin(MessagePluginInterface):
|
|||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
# 在初始化时动态应用装饰器
|
||||||
|
self.process_message = plugin_stats_decorator(plugin_name=self.name)(self.process_message)
|
||||||
|
|
||||||
def initialize(self, context: Dict[str, Any]) -> bool:
|
def initialize(self, context: Dict[str, Any]) -> bool:
|
||||||
"""初始化插件"""
|
"""初始化插件"""
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
import functools
|
import functools
|
||||||
|
import time
|
||||||
import traceback
|
import traceback
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from typing import Callable, Dict, Any, Tuple
|
from typing import Callable, Dict, Any, Tuple
|
||||||
|
|
||||||
from event_system.event_manager import EventManager
|
from db.stats_db import StatsDBOperator
|
||||||
from event_system.events.plugin_events import PluginCallStartEvent, PluginCallEndEvent, PluginCallErrorEvent
|
from db.connection import DBConnectionManager
|
||||||
|
|
||||||
|
|
||||||
def plugin_stats_decorator(plugin_name: str) -> Callable:
|
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:
|
def decorator(func: Callable) -> Callable:
|
||||||
@functools.wraps(func)
|
@functools.wraps(func)
|
||||||
def wrapper(self, message: Dict[str, Any]) -> Tuple[bool, str]:
|
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", "")
|
content = message.get("content", "")
|
||||||
sender = message.get("sender", "")
|
sender = message.get("sender", "")
|
||||||
roomid = message.get("roomid", "")
|
roomid = message.get("roomid", "")
|
||||||
|
|
||||||
# 发布插件调用开始事件
|
# 记录开始时间
|
||||||
start_time = datetime.now()
|
start_time = time.time()
|
||||||
event_manager.publish(PluginCallStartEvent, {
|
|
||||||
"plugin_name": plugin_name,
|
|
||||||
"command": content,
|
|
||||||
"user_id": sender,
|
|
||||||
"group_id": roomid,
|
|
||||||
"start_time": start_time
|
|
||||||
})
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# 调用原始方法
|
# 调用原始方法
|
||||||
success, response = func(self, message)
|
success, response = func(self, message)
|
||||||
|
|
||||||
# 发布插件调用结束事件
|
# 计算执行时间(毫秒)
|
||||||
end_time = datetime.now()
|
process_time_ms = (time.time() - start_time) * 1000
|
||||||
event_manager.publish(PluginCallEndEvent, {
|
|
||||||
"plugin_name": plugin_name,
|
# 记录插件调用
|
||||||
"command": content,
|
stats_db.record_plugin_call(
|
||||||
"user_id": sender,
|
plugin_name=plugin_name,
|
||||||
"group_id": roomid,
|
command=content,
|
||||||
"start_time": start_time,
|
user_id=sender,
|
||||||
"end_time": end_time,
|
group_id=roomid,
|
||||||
"success": success,
|
success=success,
|
||||||
"response": response
|
process_time_ms=process_time_ms
|
||||||
})
|
)
|
||||||
|
|
||||||
return success, response
|
return success, response
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
# 发布插件调用错误事件
|
# 计算执行时间(毫秒)
|
||||||
event_manager.publish(PluginCallErrorEvent, {
|
process_time_ms = (time.time() - start_time) * 1000
|
||||||
"plugin_name": plugin_name,
|
|
||||||
"command": content,
|
# 记录错误
|
||||||
"user_id": sender,
|
error_message = str(e)
|
||||||
"group_id": roomid,
|
stack_trace = traceback.format_exc()
|
||||||
"start_time": start_time,
|
|
||||||
"error_message": str(e),
|
try:
|
||||||
"stack_trace": traceback.format_exc()
|
# 记录插件调用(失败)
|
||||||
})
|
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
|
raise
|
||||||
|
|||||||
Reference in New Issue
Block a user