Enhance bot functionality and refactor database interactions
- Added `ca-certificates` installation to Dockerfile for improved network security. - Updated health check command in Dockerfile to include better timeout handling. - Refactored `run_helper.py` to implement proper signal handling and logging during shutdown. - Transitioned database operations to an asynchronous model in `async_db.py`, improving performance and responsiveness. - Updated database schema to support new foreign key relationships and optimized indexing for better query performance. - Enhanced various bot handlers to utilize async database methods, improving overall efficiency and user experience. - Removed obsolete database and fix scripts to streamline the project structure.
This commit is contained in:
@@ -40,7 +40,8 @@ admin_router.message.middleware(AdminAccessMiddleware())
|
||||
)
|
||||
async def admin_panel(
|
||||
message: types.Message,
|
||||
state: FSMContext
|
||||
state: FSMContext,
|
||||
**kwargs
|
||||
):
|
||||
"""Главное меню администратора"""
|
||||
try:
|
||||
@@ -66,11 +67,11 @@ async def get_last_users(
|
||||
try:
|
||||
logger.info(f"Получение списка последних пользователей. Пользователь: {message.from_user.full_name}")
|
||||
admin_service = AdminService(bot_db)
|
||||
users = admin_service.get_last_users()
|
||||
users = await admin_service.get_last_users()
|
||||
|
||||
# Преобразуем в формат для клавиатуры (кортежи как ожидает create_keyboard_with_pagination)
|
||||
users_data = [
|
||||
(user.full_name, user.username) # (full_name, username) - формат кортежей
|
||||
(user.full_name, user.user_id)
|
||||
for user in users
|
||||
]
|
||||
|
||||
@@ -97,7 +98,7 @@ async def get_banned_users(
|
||||
try:
|
||||
logger.info(f"Получение списка заблокированных пользователей. Пользователь: {message.from_user.full_name}")
|
||||
admin_service = AdminService(bot_db)
|
||||
message_text, buttons_list = admin_service.get_banned_users_for_display(0)
|
||||
message_text, buttons_list = await admin_service.get_banned_users_for_display(0)
|
||||
|
||||
if buttons_list:
|
||||
keyboard = create_keyboard_with_pagination(1, len(buttons_list), buttons_list, 'unlock')
|
||||
@@ -120,6 +121,7 @@ async def get_banned_users(
|
||||
async def start_ban_process(
|
||||
message: types.Message,
|
||||
state: FSMContext,
|
||||
**kwargs
|
||||
):
|
||||
"""Начало процесса блокировки пользователя"""
|
||||
try:
|
||||
@@ -151,15 +153,15 @@ async def process_ban_target(
|
||||
|
||||
# Определяем пользователя
|
||||
if ban_type == "username":
|
||||
user = admin_service.get_user_by_username(message.text)
|
||||
user = await admin_service.get_user_by_username(message.text)
|
||||
if not user:
|
||||
await message.answer(f"Пользователь с username '{escape_html(message.text)}' не найден.")
|
||||
await return_to_admin_menu(message, state)
|
||||
return
|
||||
else: # ban_type == "id"
|
||||
try:
|
||||
user_id = admin_service.validate_user_input(message.text)
|
||||
user = admin_service.get_user_by_id(user_id)
|
||||
user_id = await admin_service.validate_user_input(message.text)
|
||||
user = await admin_service.get_user_by_id(user_id)
|
||||
if not user:
|
||||
await message.answer(f"Пользователь с ID {user_id} не найден в базе данных.")
|
||||
await return_to_admin_menu(message, state)
|
||||
@@ -195,7 +197,8 @@ async def process_ban_target(
|
||||
)
|
||||
async def process_ban_reason(
|
||||
message: types.Message,
|
||||
state: FSMContext
|
||||
state: FSMContext,
|
||||
**kwargs
|
||||
):
|
||||
"""Обработка причины блокировки"""
|
||||
try:
|
||||
@@ -218,6 +221,7 @@ async def process_ban_reason(
|
||||
async def process_ban_duration(
|
||||
message: types.Message,
|
||||
state: FSMContext,
|
||||
**kwargs
|
||||
):
|
||||
"""Обработка срока блокировки"""
|
||||
try:
|
||||
@@ -260,7 +264,8 @@ async def process_ban_duration(
|
||||
async def confirm_ban(
|
||||
message: types.Message,
|
||||
state: FSMContext,
|
||||
bot_db: MagicData("bot_db")
|
||||
bot_db: MagicData("bot_db"),
|
||||
**kwargs
|
||||
):
|
||||
"""Подтверждение блокировки пользователя"""
|
||||
try:
|
||||
@@ -269,7 +274,7 @@ async def confirm_ban(
|
||||
|
||||
|
||||
# Выполняем блокировку
|
||||
admin_service.ban_user(
|
||||
await admin_service.ban_user(
|
||||
user_id=user_data['target_user_id'],
|
||||
username=user_data['target_username'],
|
||||
reason=user_data['ban_reason'],
|
||||
@@ -298,7 +303,8 @@ async def confirm_ban(
|
||||
)
|
||||
async def cancel_ban_process(
|
||||
message: types.Message,
|
||||
state: FSMContext
|
||||
state: FSMContext,
|
||||
**kwargs
|
||||
):
|
||||
"""Отмена процесса блокировки"""
|
||||
try:
|
||||
@@ -312,7 +318,8 @@ async def cancel_ban_process(
|
||||
@admin_router.message(Command("test_metrics"))
|
||||
async def test_metrics_handler(
|
||||
message: types.Message,
|
||||
bot_db: MagicData("bot_db")
|
||||
bot_db: MagicData("bot_db"),
|
||||
**kwargs
|
||||
):
|
||||
"""Тестовый хендлер для проверки метрик"""
|
||||
from helper_bot.utils.metrics import metrics
|
||||
@@ -325,18 +332,23 @@ async def test_metrics_handler(
|
||||
|
||||
# Проверяем активных пользователей
|
||||
if hasattr(bot_db, 'connect') and hasattr(bot_db, 'cursor'):
|
||||
# Используем UNIX timestamp для сравнения с date_changed
|
||||
import time
|
||||
current_timestamp = int(time.time())
|
||||
one_day_ago = current_timestamp - (24 * 60 * 60) # 24 часа назад
|
||||
|
||||
active_users_query = """
|
||||
SELECT COUNT(DISTINCT user_id) as active_users
|
||||
FROM our_users
|
||||
WHERE date_changed > datetime('now', '-1 day')
|
||||
WHERE date_changed > ?
|
||||
"""
|
||||
try:
|
||||
bot_db.connect()
|
||||
bot_db.cursor.execute(active_users_query)
|
||||
result = bot_db.cursor.fetchone()
|
||||
await bot_db.connect()
|
||||
await bot_db.cursor.execute(active_users_query, (one_day_ago,))
|
||||
result = await bot_db.cursor.fetchone()
|
||||
active_users = result[0] if result else 0
|
||||
finally:
|
||||
bot_db.close()
|
||||
await bot_db.close()
|
||||
else:
|
||||
active_users = "N/A"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user