diff --git a/database.py b/database.py index c85ae2d..3177ba4 100644 --- a/database.py +++ b/database.py @@ -1,6 +1,8 @@ -from pyodbc import connect, SQL_CHAR, SQL_WCHAR, Row +from datetime import datetime from typing import List, Union +from pyodbc import connect, SQL_CHAR, SQL_WCHAR, Row + class BasicDatabase: def __init__(self, connection_string: str): @@ -76,18 +78,19 @@ class BasicDatabase: return self._to_dict(self.cursor.fetchall()) def get_top_silent(self, chat_id: int, threshold_days: int): - # noinspection SpellCheckingInspection + current_time = int(datetime.now().timestamp()) + threshold = current_time - threshold_days * 86400 self.cursor.execute(""" - SELECT user_id, - CASE - WHEN last_message = 0 THEN 'никогда' - ELSE (unixepoch() - last_message) / 86400 - END AS value + SELECT user_id, (? - last_message) DIV 86400 as value FROM users - WHERE chat_id = ? AND value >= ? - ORDER BY value DESC - """, chat_id, threshold_days) - return self._to_dict(self.cursor.fetchall()) + WHERE chat_id = ? AND last_message <= ? + ORDER BY last_message ASC + """, current_time, chat_id, threshold) + result = self._to_dict(self.cursor.fetchall()) + for row in result: + if row['value'] > 3650: + row['value'] = 'никогда' + return result def get_top_warnings(self, chat_id: int): self.cursor.execute(""" diff --git a/tg/handlers/user.py b/tg/handlers/user.py index eaf1ab7..14c12cc 100644 --- a/tg/handlers/user.py +++ b/tg/handlers/user.py @@ -158,7 +158,7 @@ async def silent_handler(message: Message, bot: Bot): await message.answer(MESSAGE_CHAT_NOT_ACTIVE) return - top_users = database.DB.get_top_messages_today(chat_id) + top_users = database.DB.get_top_silent(chat_id, 14) if len(top_users) == 0: await message.answer('Молчунов нет. Все молодцы!') return