From 89045095b16266c92d903bf0dac8c1b45528e8c9 Mon Sep 17 00:00:00 2001 From: Egor Date: Sat, 13 Sep 2025 05:42:27 +0300 Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D1=81=D0=B8=D0=BD=D0=BA=D0=B0=20?= =?UTF-8?q?=D1=81=D1=87=D0=B5=D1=82=D1=87=D0=B8=D0=BA=D0=B0=20=D1=8E=D0=B7?= =?UTF-8?q?=D0=B5=D1=80=D0=BE=D0=B2=20=D0=BD=D0=B0=20=D1=81=D0=B5=D1=80?= =?UTF-8?q?=D0=B2=D0=B5=D1=80=D0=B0=D1=85=20=D0=B2=20=D0=B0=D0=B4=D0=BC?= =?UTF-8?q?=D0=B8=D0=BD=20=D0=BF=D0=B0=D0=BD=D0=B5=D0=BB=D0=B8=20=D0=B2=20?= =?UTF-8?q?=D1=80=D0=B0=D0=B7=D0=B4=D0=B5=D0=BB=D0=B5=20=D1=81=20=D0=BF?= =?UTF-8?q?=D0=BE=D0=B4=D0=BF=D0=B8=D1=81=D0=BA=D0=B0=D0=BC=D0=B8=20=D1=8E?= =?UTF-8?q?=D0=B7=D0=B5=D1=80=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/database/crud/server_squad.py | 38 ++++++++++++++++++------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/app/database/crud/server_squad.py b/app/database/crud/server_squad.py index 6e8f31fc..907c3e9d 100644 --- a/app/database/crud/server_squad.py +++ b/app/database/crud/server_squad.py @@ -1,10 +1,10 @@ import logging from typing import List, Optional, Tuple -from sqlalchemy import select, and_, func, update, delete +from sqlalchemy import select, and_, func, update, delete, text from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.orm import selectinload -from app.database.models import ServerSquad, SubscriptionServer +from app.database.models import ServerSquad, SubscriptionServer, Subscription logger = logging.getLogger(__name__) @@ -137,7 +137,7 @@ async def delete_server_squad(db: AsyncSession, server_id: int) -> bool: connections_count = connections_result.scalar() if connections_count > 0: - logger.warning(f"❌ Нельзя удалить сервер {server_id}: есть активные подключения ({connections_count})") + logger.warning(f"⚠ Нельзя удалить сервер {server_id}: есть активные подключения ({connections_count})") return False await db.execute( @@ -324,20 +324,26 @@ async def get_server_ids_by_uuids( async def sync_server_user_counts(db: AsyncSession) -> int: try: - result = await db.execute( - select( - ServerSquad.id, - ServerSquad.squad_uuid, - func.count(SubscriptionServer.id).label('actual_users') - ) - .outerjoin(SubscriptionServer, ServerSquad.id == SubscriptionServer.server_squad_id) - .join(Subscription, SubscriptionServer.subscription_id == Subscription.id) - .where(Subscription.status == 'active') - .group_by(ServerSquad.id, ServerSquad.squad_uuid) - ) + all_servers_result = await db.execute(select(ServerSquad.id, ServerSquad.squad_uuid)) + all_servers = all_servers_result.fetchall() + + logger.info(f"🔍 Найдено серверов для синхронизации: {len(all_servers)}") updated_count = 0 - for server_id, squad_uuid, actual_users in result.fetchall(): + for server_id, squad_uuid in all_servers: + count_result = await db.execute( + text(""" + SELECT COUNT(s.id) + FROM subscriptions s + WHERE s.status IN ('active', 'trial') + AND s.connected_squads::text LIKE :uuid_pattern + """), + {"uuid_pattern": f'%"{squad_uuid}"%'} + ) + actual_users = count_result.scalar() or 0 + + logger.info(f"📊 Сервер {server_id} ({squad_uuid[:8]}): {actual_users} пользователей") + await db.execute( update(ServerSquad) .where(ServerSquad.id == server_id) @@ -352,4 +358,4 @@ async def sync_server_user_counts(db: AsyncSession) -> int: except Exception as e: logger.error(f"Ошибка синхронизации счетчиков пользователей: {e}") await db.rollback() - return 0 \ No newline at end of file + return 0