mirror of
https://github.com/cdryzun/tg_bot_collections.git
synced 2025-11-05 01:06:42 +08:00
feat: get user stats in the group
Signed-off-by: Keming <kemingy94@gmail.com>
This commit is contained in:
@ -20,6 +20,13 @@ class StatsEntry:
|
||||
message_count: int
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class UserStatsEntry:
|
||||
user_id: int
|
||||
user_name: str
|
||||
message_count: int
|
||||
|
||||
|
||||
class MessageStore:
|
||||
def __init__(self, db_file: str):
|
||||
parent_folder = os.path.dirname(db_file)
|
||||
@ -124,6 +131,25 @@ class MessageStore:
|
||||
rows = cursor.fetchall()
|
||||
return [StatsEntry(date=row[0], message_count=row[1]) for row in rows]
|
||||
|
||||
def get_user_stats(self, chat_id: int, topk: int = 10) -> list[UserStatsEntry]:
|
||||
with self.connect() as conn:
|
||||
self._clean_old_messages(chat_id, conn)
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(
|
||||
"""
|
||||
SELECT user_id,
|
||||
(SELECT user_name FROM messages m0 WHERE m0.user_id = m1.user_id LIMIT 1) AS name,
|
||||
COUNT(*) AS num
|
||||
FROM messages m1
|
||||
WHERE chat_id = ?
|
||||
GROUP BY user_id
|
||||
ORDER BY num DESC
|
||||
LIMIT ?;""",
|
||||
(chat_id, topk),
|
||||
)
|
||||
rows = cursor.fetchall()
|
||||
return [UserStatsEntry(*row) for row in rows]
|
||||
|
||||
def search_messages(
|
||||
self, chat_id: int, keyword: str, limit: int = 10
|
||||
) -> list[ChatMessage]:
|
||||
|
||||
Reference in New Issue
Block a user