From 8a9a48fed0dd7e7a1fbcc08802764d53e183d051 Mon Sep 17 00:00:00 2001 From: Capybara-z Date: Sat, 31 May 2025 16:48:40 +0300 Subject: [PATCH 1/2] group tariffs and sort by duration --- database/statistics.py | 24 ++++++++++++++++++++++++ handlers/admin/stats/stats_handler.py | 25 +++++++++++++++++++++---- handlers/keys/key_mode/key_create.py | 2 +- 3 files changed, 46 insertions(+), 5 deletions(-) diff --git a/database/statistics.py b/database/statistics.py index e79b539d..434f34e5 100644 --- a/database/statistics.py +++ b/database/statistics.py @@ -89,6 +89,30 @@ async def get_tariff_names( return dict(result.all()) +async def get_tariff_groups( + session: AsyncSession, tariff_ids: list[int] +) -> dict[int, str]: + if not tariff_ids: + return {} + + result = await session.execute( + select(Tariff.id, Tariff.group_code).where(Tariff.id.in_(tariff_ids)) + ) + return dict(result.all()) + + +async def get_tariff_durations( + session: AsyncSession, tariff_ids: list[int] +) -> dict[int, int]: + if not tariff_ids: + return {} + + result = await session.execute( + select(Tariff.id, Tariff.duration_days).where(Tariff.id.in_(tariff_ids)) + ) + return dict(result.all()) + + async def count_total_referrals(session: AsyncSession) -> int: return await session.scalar(select(func.count()).select_from(Referral)) diff --git a/handlers/admin/stats/stats_handler.py b/handlers/admin/stats/stats_handler.py index ad0b8232..c02f8d35 100644 --- a/handlers/admin/stats/stats_handler.py +++ b/handlers/admin/stats/stats_handler.py @@ -21,6 +21,8 @@ from database import ( count_users_updated_today, get_tariff_distribution, get_tariff_names, + get_tariff_groups, + get_tariff_durations, sum_payments_between, sum_payments_since, sum_total_payments, @@ -72,12 +74,17 @@ async def handle_stats(callback_query: CallbackQuery, session: AsyncSession): tariff_counts, no_tariff_keys = await get_tariff_distribution(session, include_unbound=True) tariff_names = await get_tariff_names(session, [tid for tid, _ in tariff_counts]) + tariff_groups = await get_tariff_groups(session, [tid for tid, _ in tariff_counts]) + tariff_durations = await get_tariff_durations(session, [tid for tid, _ in tariff_counts]) + + grouped_tariffs = {} + for tid, count in tariff_counts: + group = tariff_groups.get(tid, "unknown") + if group not in grouped_tariffs: + grouped_tariffs[group] = [] + grouped_tariffs[group].append((tid, count)) tariff_stats_text = "" - for tid, count in tariff_counts: - name = tariff_names.get(tid, f"ID {tid}") - tariff_stats_text += f"├ {name}: {count}\n" - duration_buckets = Counter() now_ts = int(datetime.utcnow().timestamp() * 1000) @@ -98,6 +105,16 @@ async def handle_stats(callback_query: CallbackQuery, session: AsyncSession): for name, count in duration_buckets.items(): tariff_stats_text += f"├ {name}: {count}\n" + for group, tariffs in grouped_tariffs.items(): + tariff_stats_text += f"Тариф {group}\n" + sorted_tariffs = sorted( + tariffs, + key=lambda x: tariff_durations.get(x[0], 0) + ) + for tid, count in sorted_tariffs: + name = tariff_names.get(tid, f"ID {tid}") + tariff_stats_text += f" ├ {name}: {count}\n" + tariff_stats_text = ( "└ По тарифам и срокам:\n" + tariff_stats_text if tariff_stats_text diff --git a/handlers/keys/key_mode/key_create.py b/handlers/keys/key_mode/key_create.py index fed1937c..5dbd2dc5 100644 --- a/handlers/keys/key_mode/key_create.py +++ b/handlers/keys/key_mode/key_create.py @@ -99,7 +99,7 @@ async def handle_key_creation( if isinstance(message_or_query, CallbackQuery) else message_or_query ), - text="❌ Нет доступных тарифов для выбранного кластера.", + text="❌ Нет доступных тарифов для выбора.", reply_markup=None, ) return From 7cd99073d0aa12a364178824f72612bd4ea8f390 Mon Sep 17 00:00:00 2001 From: Capybara-z Date: Sat, 31 May 2025 18:08:52 +0300 Subject: [PATCH 2/2] group tariffs and sort by duration --- handlers/admin/stats/stats_handler.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/handlers/admin/stats/stats_handler.py b/handlers/admin/stats/stats_handler.py index c02f8d35..af5f9015 100644 --- a/handlers/admin/stats/stats_handler.py +++ b/handlers/admin/stats/stats_handler.py @@ -102,7 +102,19 @@ async def handle_stats(callback_query: CallbackQuery, session: AsyncSession): bucket = "Без тарифа: прочее" duration_buckets[bucket] += 1 - for name, count in duration_buckets.items(): + bucket_order = { + "Без тарифа: 1 мес": 1, + "Без тарифа: 3 мес": 2, + "Без тарифа: 6 мес": 3, + "Без тарифа: 12 мес": 4, + "Без тарифа: прочее": 5 + } + sorted_buckets = sorted( + duration_buckets.items(), + key=lambda x: bucket_order.get(x[0], 999) + ) + + for name, count in sorted_buckets: tariff_stats_text += f"├ {name}: {count}\n" for group, tariffs in grouped_tariffs.items():