feat(api): 增强消息统计接口,支持格式化输出
为 `/messages/stats/by_chat` 端点添加了一个新的 `format` 查询参数。当 `format=True` 时,API 响应将包含更友好的信息,如群聊名称和用户昵称,而不仅仅是 ID。 这通过集成 `chat_api` 和 `person_api` 来获取相应的会话和用户信息,从而提高了接口的可用性和数据可读性。
This commit is contained in:
@@ -4,7 +4,11 @@ from typing import Literal
|
||||
from fastapi import APIRouter, HTTPException, Query
|
||||
|
||||
from src.config.config import global_config
|
||||
from src.plugin_system.apis import message_api
|
||||
from src.plugin_system.apis import message_api, chat_api, person_api
|
||||
from src.chat.message_receive.chat_stream import get_chat_manager
|
||||
from src.common.logger import get_logger
|
||||
|
||||
logger = get_logger("HTTP消息API")
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@@ -52,7 +56,7 @@ async def get_message_stats(
|
||||
async def get_message_stats_by_chat(
|
||||
days: int = Query(1, ge=1, description="指定查询过去多少天的数据"),
|
||||
group_by_user: bool = Query(False, description="是否按用户进行分组统计"),
|
||||
filter_bot: bool = Query(False, description="是否过滤BOT自身的消息")
|
||||
format: bool = Query(False, description="是否格式化输出,包含群聊和用户信息"),
|
||||
):
|
||||
"""
|
||||
获取BOT在指定天数内按聊天流或按用户统计的消息数据。
|
||||
@@ -63,7 +67,6 @@ async def get_message_stats_by_chat(
|
||||
messages = await message_api.get_messages_by_time(start_time, end_time)
|
||||
bot_qq = str(global_config.bot.qq_account)
|
||||
|
||||
if filter_bot:
|
||||
messages = [msg for msg in messages if msg.get("user_id") != bot_qq]
|
||||
|
||||
stats = {}
|
||||
@@ -91,8 +94,41 @@ async def get_message_stats_by_chat(
|
||||
stats[chat_id]["user_stats"][user_id] += 1
|
||||
|
||||
if not group_by_user:
|
||||
# 如果不按用户分组,则只返回总统计信息
|
||||
return {chat_id: data["total_stats"] for chat_id, data in stats.items()}
|
||||
stats = {chat_id: data["total_stats"] for chat_id, data in stats.items()}
|
||||
|
||||
if format:
|
||||
formatted_stats = {}
|
||||
for chat_id, data in stats.items():
|
||||
# 尝试获取群聊流
|
||||
logger.info(f"这是一个测试日志:{chat_id}")
|
||||
group_stream = chat_api.get_stream_by_group_id(chat_id)
|
||||
if group_stream and group_stream.group_info:
|
||||
chat_name = group_stream.group_info.group_name
|
||||
else:
|
||||
# 只有当它不是一个已知的群聊时,才尝试作为私聊处理
|
||||
private_stream = chat_api.get_stream_by_user_id(chat_id)
|
||||
if private_stream and private_stream.user_info:
|
||||
chat_name = private_stream.user_info.user_nickname
|
||||
else:
|
||||
chat_name = f"未知会话 ({chat_id})"
|
||||
|
||||
formatted_data = {
|
||||
"chat_name": chat_name,
|
||||
"total_stats": data if not group_by_user else data["total_stats"],
|
||||
}
|
||||
|
||||
if group_by_user and "user_stats" in data:
|
||||
formatted_data["user_stats"] = {}
|
||||
for user_id, count in data["user_stats"].items():
|
||||
person_id = person_api.get_person_id("qq", user_id)
|
||||
nickname = await person_api.get_person_value(person_id, "nickname", "未知用户")
|
||||
formatted_data["user_stats"][user_id] = {
|
||||
"nickname": nickname,
|
||||
"count": count
|
||||
}
|
||||
|
||||
formatted_stats[chat_id] = formatted_data
|
||||
return formatted_stats
|
||||
|
||||
return stats
|
||||
|
||||
|
||||
Reference in New Issue
Block a user