now = datetime.datetime.now()
one_day_ago = now - datetime.timedelta(days=1)
one_week_ago = now - datetime.timedelta(weeks=1)
two_weeks_ago = now - datetime.timedelta(weeks=2)
one_month_ago = now - datetime.timedelta(weeks=4)
statement = select(
func.sum(case((Transaction.created_at > one_day_ago, Transaction.amount), else_=0)).label('day'),
func.sum(case((Transaction.created_at > one_week_ago, Transaction.amount), else_=0)).label('week'),
func.sum(case((Transaction.created_at > two_weeks_ago, Transaction.amount), else_=0)).label('two_weeks'),
func.sum(case((Transaction.created_at > one_month_ago, Transaction.amount), else_=0)).label('month'),
).where(Transaction.comment == "topup")
result = await self.session.execute(statement)
day_stats, week_stats, two_weeks_stats, month_stats = result.fetchone()
day_stats = 0 if day_stats is None else day_stats
week_stats = 0 if week_stats is None else week_stats
two_weeks_stats = 0 if two_weeks_stats is None else two_weeks_stats
month_stats = 0 if month_stats is None else month_stats
return day_stats, week_stats, two_weeks_stats, month_stats
выглядит как оверговнокод, как можно сделать по-другому? Беру статистику пополнений за день, неделю, две недели и месяц?
Можно сократить в 2 раза с помощью coalesce
оно нон хуярило
Обсуждают сегодня