From 6ebaff08e8c40e2348d9d468d5f01d3e109d809e Mon Sep 17 00:00:00 2001 From: Vladless Date: Thu, 7 May 2026 15:15:27 +0000 Subject: [PATCH] stats: show linked email + tg_id counts --- database/statistics.py | 10 +++++++++- handlers/admin/stats/stats_handler.py | 9 +++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/database/statistics.py b/database/statistics.py index 726ca3b5..3f04278a 100644 --- a/database/statistics.py +++ b/database/statistics.py @@ -4,13 +4,21 @@ from sqlalchemy import and_, exists, func, not_, select from sqlalchemy.ext.asyncio import AsyncSession from core.constants import PAYMENT_SYSTEMS_EXCLUDED -from database.models import Key, Payment, Referral, Tariff, User +from database.models import Identity, Key, Payment, Referral, Tariff, User async def count_total_users(session: AsyncSession) -> int: return await session.scalar(select(func.count()).select_from(User)) +async def count_users_with_tg_id(session: AsyncSession) -> int: + return await session.scalar(select(func.count()).select_from(User).where(User.tg_id.isnot(None))) + + +async def count_identities_with_email(session: AsyncSession) -> int: + return await session.scalar(select(func.count()).select_from(Identity).where(Identity.email.isnot(None))) + + async def count_users_updated_today(session: AsyncSession, today: date) -> int: return await session.scalar(select(func.count()).select_from(User).where(User.updated_at >= today)) diff --git a/handlers/admin/stats/stats_handler.py b/handlers/admin/stats/stats_handler.py index 1c66c051..96bcc6b1 100644 --- a/handlers/admin/stats/stats_handler.py +++ b/handlers/admin/stats/stats_handler.py @@ -26,12 +26,14 @@ from database import ( count_active_paid_keys, count_active_trial_keys, count_hot_leads, + count_identities_with_email, count_total_keys, count_total_referrals, count_total_users, count_users_registered_between, count_users_registered_since, count_users_updated_today, + count_users_with_tg_id, get_tariff_distribution, get_tariff_names_groups_subgroups_durations, sum_payments_between, @@ -144,6 +146,8 @@ async def handle_stats(callback_query: CallbackQuery, session: AsyncSession): last_month_end_utc = last_month_end.astimezone(pytz.UTC).replace(tzinfo=None) total_users = await count_total_users(session) + users_with_tg = await count_users_with_tg_id(session) + identities_with_email = await count_identities_with_email(session) users_updated_today = await count_users_updated_today(session, today_start_utc) registrations_today = await count_users_registered_since(session, today_start_utc) registrations_yesterday = await count_users_registered_between(session, yesterday_start_utc, yesterday_end_utc) @@ -270,6 +274,11 @@ async def handle_stats(callback_query: CallbackQuery, session: AsyncSession): f"├ 📅 За прошлый месяц: {registrations_last_month}\n" f"└ 🌐 Всего: {total_users}\n" f"\n" + f"🔗 Связанные данные:\n" + f"
" + f"├ 📨 Учёток с e-mail: {identities_with_email}\n" + f"└ 💬 Привязанных Telegram: {users_with_tg}\n" + f"
\n" f"💡 Активность:\n" f"└ 👥 Сегодня были активны: {users_updated_today}\n\n" f"🤝 Реферальная система:\n"