group tariffs and sort by duration
This commit is contained in:
@@ -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))
|
||||
|
||||
|
||||
@@ -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}: <b>{count}</b>\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}: <b>{count}</b>\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}: <b>{count}</b>\n"
|
||||
|
||||
tariff_stats_text = (
|
||||
"└ По тарифам и срокам:\n" + tariff_stats_text
|
||||
if tariff_stats_text
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user