refactor: 将多个方法修改为静态方法以提高代码可读性和一致性
This commit is contained in:
@@ -24,7 +24,8 @@ class LLMStatistics:
|
||||
self._init_database()
|
||||
self.name_dict: Dict[List] = {}
|
||||
|
||||
def _init_database(self):
|
||||
@staticmethod
|
||||
def _init_database():
|
||||
"""初始化数据库集合"""
|
||||
if "online_time" not in db.list_collection_names():
|
||||
db.create_collection("online_time")
|
||||
@@ -51,7 +52,8 @@ class LLMStatistics:
|
||||
if self.console_thread:
|
||||
self.console_thread.join()
|
||||
|
||||
def _record_online_time(self):
|
||||
@staticmethod
|
||||
def _record_online_time():
|
||||
"""记录在线时间"""
|
||||
current_time = datetime.now()
|
||||
# 检查5分钟内是否已有记录
|
||||
@@ -187,7 +189,7 @@ class LLMStatistics:
|
||||
|
||||
# 按模型统计
|
||||
output.append("按模型统计:")
|
||||
output.append(("模型名称 调用次数 Token总量 累计花费"))
|
||||
output.append("模型名称 调用次数 Token总量 累计花费")
|
||||
for model_name, count in sorted(stats["requests_by_model"].items()):
|
||||
tokens = stats["tokens_by_model"][model_name]
|
||||
cost = stats["costs_by_model"][model_name]
|
||||
@@ -198,7 +200,7 @@ class LLMStatistics:
|
||||
|
||||
# 按请求类型统计
|
||||
output.append("按请求类型统计:")
|
||||
output.append(("模型名称 调用次数 Token总量 累计花费"))
|
||||
output.append("模型名称 调用次数 Token总量 累计花费")
|
||||
for req_type, count in sorted(stats["requests_by_type"].items()):
|
||||
tokens = stats["tokens_by_type"][req_type]
|
||||
cost = stats["costs_by_type"][req_type]
|
||||
@@ -209,7 +211,7 @@ class LLMStatistics:
|
||||
|
||||
# 修正用户统计列宽
|
||||
output.append("按用户统计:")
|
||||
output.append(("用户ID 调用次数 Token总量 累计花费"))
|
||||
output.append("用户ID 调用次数 Token总量 累计花费")
|
||||
for user_id, count in sorted(stats["requests_by_user"].items()):
|
||||
tokens = stats["tokens_by_user"][user_id]
|
||||
cost = stats["costs_by_user"][user_id]
|
||||
@@ -225,7 +227,7 @@ class LLMStatistics:
|
||||
|
||||
# 添加聊天统计
|
||||
output.append("群组统计:")
|
||||
output.append(("群组名称 消息数量"))
|
||||
output.append("群组名称 消息数量")
|
||||
for group_id, count in sorted(stats["messages_by_chat"].items()):
|
||||
output.append(f"{self.name_dict[group_id][0][:32]:<32} {count:>10}")
|
||||
|
||||
@@ -246,7 +248,7 @@ class LLMStatistics:
|
||||
|
||||
# 按模型统计
|
||||
output.append("按模型统计:")
|
||||
output.append(("模型名称 调用次数 Token总量 累计花费"))
|
||||
output.append("模型名称 调用次数 Token总量 累计花费")
|
||||
for model_name, count in sorted(stats["requests_by_model"].items()):
|
||||
tokens = stats["tokens_by_model"][model_name]
|
||||
cost = stats["costs_by_model"][model_name]
|
||||
@@ -284,7 +286,7 @@ class LLMStatistics:
|
||||
|
||||
# 添加聊天统计
|
||||
output.append("群组统计:")
|
||||
output.append(("群组名称 消息数量"))
|
||||
output.append("群组名称 消息数量")
|
||||
for group_id, count in sorted(stats["messages_by_chat"].items()):
|
||||
output.append(f"{self.name_dict[group_id][0][:32]:<32} {count:>10}")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user