feat: add group detail dashboard insights
This commit is contained in:
@@ -269,6 +269,72 @@ class MessageStorageDB(BaseDBOperator):
|
||||
"""
|
||||
return self.execute_query(sql, (group_id, days)) or []
|
||||
|
||||
def get_group_member_message_ranking(self, group_id: str, start_time: datetime,
|
||||
end_time: datetime, limit: int = 10) -> List[Dict]:
|
||||
"""获取群成员发言排行"""
|
||||
sql = """
|
||||
SELECT
|
||||
sender,
|
||||
COUNT(*) AS message_count,
|
||||
MAX(timestamp) AS last_message_time
|
||||
FROM messages
|
||||
WHERE timestamp >= %s
|
||||
AND timestamp <= %s
|
||||
AND group_id = %s
|
||||
AND sender IS NOT NULL
|
||||
AND sender <> ''
|
||||
GROUP BY sender
|
||||
ORDER BY message_count DESC, last_message_time DESC
|
||||
LIMIT %s
|
||||
"""
|
||||
params = (
|
||||
start_time.strftime('%Y-%m-%d %H:%M:%S'),
|
||||
end_time.strftime('%Y-%m-%d %H:%M:%S'),
|
||||
group_id,
|
||||
limit,
|
||||
)
|
||||
rows = self.execute_query(sql, params) or []
|
||||
for row in rows:
|
||||
dt = row.get("last_message_time")
|
||||
if isinstance(dt, datetime):
|
||||
row["last_message_time"] = dt.strftime("%Y-%m-%d %H:%M:%S")
|
||||
return rows
|
||||
|
||||
def get_group_hourly_distribution(self, group_id: str, days: int = 30) -> List[Dict]:
|
||||
"""获取群消息小时分布"""
|
||||
sql = """
|
||||
SELECT
|
||||
HOUR(timestamp) AS hour_slot,
|
||||
COUNT(*) AS message_count
|
||||
FROM messages
|
||||
WHERE group_id = %s
|
||||
AND timestamp >= DATE_SUB(NOW(), INTERVAL %s DAY)
|
||||
GROUP BY HOUR(timestamp)
|
||||
ORDER BY hour_slot
|
||||
"""
|
||||
rows = self.execute_query(sql, (group_id, days)) or []
|
||||
return [
|
||||
{
|
||||
"hour": int(row.get("hour_slot") or 0),
|
||||
"message_count": int(row.get("message_count") or 0),
|
||||
}
|
||||
for row in rows
|
||||
]
|
||||
|
||||
def get_group_last_message(self, group_id: str) -> Optional[Dict]:
|
||||
"""获取群最后一条消息信息"""
|
||||
sql = """
|
||||
SELECT sender, content, message_type, timestamp
|
||||
FROM messages
|
||||
WHERE group_id = %s
|
||||
ORDER BY timestamp DESC
|
||||
LIMIT 1
|
||||
"""
|
||||
row = self.execute_query(sql, (group_id,), fetch_one=True)
|
||||
if row and isinstance(row.get("timestamp"), datetime):
|
||||
row["timestamp"] = row["timestamp"].strftime("%Y-%m-%d %H:%M:%S")
|
||||
return row
|
||||
|
||||
def get_messages_by_filter(self, group_id=None, start_date=None, end_date=None,
|
||||
search_text=None, page=1, page_size=20) -> Dict:
|
||||
"""按条件筛选消息并支持分页和模糊搜索
|
||||
|
||||
Reference in New Issue
Block a user