feat(message_summary): add group overview stats section

This commit is contained in:
liuwei
2026-04-13 09:18:44 +08:00
parent 0e3eda8865
commit 0f0acc1729
3 changed files with 95 additions and 3 deletions
+34
View File
@@ -557,3 +557,37 @@ class MessageStorageDB(BaseDBOperator):
group_id)
result = self.execute_query(sql, params)
return result[0]['count'] if result else 0
def get_message_stats_by_date_range(self, group_id: str, start_time: datetime, end_time: datetime) -> Dict:
"""统计指定时间范围内的群消息概览"""
sql = """
SELECT
COUNT(*) AS total_count,
COUNT(DISTINCT sender) AS participant_count,
SUM(CASE WHEN message_type = 1 THEN 1 ELSE 0 END) AS text_count,
SUM(CASE WHEN message_type = 3 THEN 1 ELSE 0 END) AS image_count,
SUM(CASE WHEN message_type IN (43, 62) THEN 1 ELSE 0 END) AS video_count,
SUM(CASE WHEN message_type = 49 THEN 1 ELSE 0 END) AS link_count,
SUM(CASE WHEN message_type IN (47, 1048625, 1090519089) THEN 1 ELSE 0 END) AS emoji_count
FROM messages
WHERE timestamp >= %s
AND timestamp <= %s
AND group_id = %s
AND sender IS NOT NULL
AND sender <> ''
"""
params = (
start_time.strftime('%Y-%m-%d %H:%M:%S'),
end_time.strftime('%Y-%m-%d %H:%M:%S'),
group_id,
)
result = self.execute_query(sql, params, fetch_one=True) or {}
return {
"total_count": int(result.get("total_count") or 0),
"participant_count": int(result.get("participant_count") or 0),
"text_count": int(result.get("text_count") or 0),
"image_count": int(result.get("image_count") or 0),
"video_count": int(result.get("video_count") or 0),
"link_count": int(result.get("link_count") or 0),
"emoji_count": int(result.get("emoji_count") or 0),
}