Skip to content

Commit

Permalink
Merge pull request #21 from HeySlava/by_category
Browse files Browse the repository at this point in the history
Group by category and year_month
  • Loading branch information
HeySlava authored Dec 8, 2023
2 parents 6899e8e + 2033cc6 commit f4aabe6
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions bot/services/report_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def get_report_by_month(
)

return [
f'{getattr(r, by_month_year)} | {r.unit} {r.total}' for
f'{getattr(r, by_month_year)} | {r.total} {r.unit}' for
r in
session.execute(select(stmt))
]
Expand All @@ -61,9 +61,11 @@ def get_report_by_category(
user: User,
session: Session,
) -> List[str]:
stmt = (
by_month_year = 'month_year'
subq = (
select(
Expense.user_id,
func.strftime('%m-%Y', Expense.cdate_tz).label(by_month_year),
Expense.item_name,
Expense.unit,
func.sum(Expense.price).label('total'),
Expand All @@ -72,9 +74,13 @@ def get_report_by_category(
Expense.user_id == user.id,
~Expense.is_replenishment,
)
.group_by(Expense.user_id, Expense.item_name, Expense.unit)
.order_by(Expense.item_name)
.group_by(Expense.user_id, by_month_year, Expense.item_name, Expense.unit)
.order_by(by_month_year, Expense.item_name)
.subquery()
)

rows = session.execute(stmt)
return [f'{row.tuple()[1]} | {row.tuple()[3]} {row.tuple()[2]}' for row in rows]
return [
f'{r.item_name} ({getattr(r, by_month_year)}) | {r.total} {r.unit}' for
r in
session.execute(select(subq))
]

0 comments on commit f4aabe6

Please sign in to comment.