Skip to content

Commit

Permalink
ref: improve queries to db
Browse files Browse the repository at this point in the history
  • Loading branch information
RezenkovD committed Sep 6, 2023
1 parent 40d7b27 commit 0b254bd
Showing 1 changed file with 21 additions and 95 deletions.
116 changes: 21 additions & 95 deletions src/services/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,42 +84,18 @@ def read_group_expenses(
Expense.user_id == user_id,
)
.group_by(Expense.category_id)
.subquery()
)
if filter_date:
categories_expenses_subquery = (
db.query(
Expense.category_id.label("id"),
func.coalesce(func.sum(Expense.amount), 0).label("amount"),
)
.filter(
and_(
Expense.group_id == group_id,
Expense.user_id == user_id,
extract("year", Expense.time) == filter_date.year,
extract("month", Expense.time) == filter_date.month,
)
)
.group_by(Expense.category_id)
.subquery()
categories_expenses_subquery = categories_expenses_subquery.filter(
extract("year", Expense.time) == filter_date.year,
extract("month", Expense.time) == filter_date.month,
)
elif start_date and end_date:
categories_expenses_subquery = (
db.query(
Expense.category_id.label("id"),
func.coalesce(func.sum(Expense.amount), 0).label("amount"),
)
.filter(
and_(
Expense.group_id == group_id,
Expense.user_id == user_id,
Expense.time >= start_date,
Expense.time <= end_date,
),
)
.group_by(Expense.category_id)
.subquery()
categories_expenses_subquery = categories_expenses_subquery.filter(
Expense.time >= start_date,
Expense.time <= end_date,
)
categories_expenses_subquery = categories_expenses_subquery.subquery()
categories_group = (
db.query(
Category.id.label("id"),
Expand Down Expand Up @@ -171,46 +147,18 @@ def read_category_expenses(
.filter(Expense.user_id == user_id)
.group_by(Category.id, Category.title)
.order_by(func.sum(Expense.amount).desc())
.all()
)
if filter_date:
category_expenses = (
db.query(
Category.id.label("id"),
Category.title.label("title"),
func.sum(Expense.amount).label("amount"),
)
.join(Category, Expense.category_id == Category.id)
.filter(
and_(
Expense.user_id == user_id,
extract("year", Expense.time) == filter_date.year,
extract("month", Expense.time) == filter_date.month,
)
)
.group_by(Category.id, Category.title)
.order_by(func.sum(Expense.amount).desc())
.all()
category_expenses = category_expenses.filter(
extract("year", Expense.time) == filter_date.year,
extract("month", Expense.time) == filter_date.month,
)
elif start_date and end_date:
category_expenses = (
db.query(
Category.id.label("id"),
Category.title.label("title"),
func.sum(Expense.amount).label("amount"),
)
.join(Category, Expense.category_id == Category.id)
.filter(
and_(
Expense.user_id == user_id,
Expense.time >= start_date,
Expense.time <= end_date,
)
)
.group_by(Category.id, Category.title)
.order_by(func.sum(Expense.amount).desc())
.all()
category_expenses = category_expenses.filter(
Expense.time >= start_date,
Expense.time <= end_date,
)
category_expenses = category_expenses.all()
return category_expenses


Expand Down Expand Up @@ -276,40 +224,18 @@ def read_user_daily_expenses(
)
.filter_by(user_id=user_id)
.group_by(func.date(Expense.time))
.all()
)
if filter_date:
daily_expenses = (
db.query(
func.date(Expense.time).label("date"),
func.sum(Expense.amount).label("amount"),
)
.filter(
and_(
Expense.user_id == user_id,
extract("year", Expense.time) == filter_date.year,
extract("month", Expense.time) == filter_date.month,
)
)
.group_by(func.date(Expense.time))
.all()
daily_expenses = daily_expenses.filter(
extract("year", Expense.time) == filter_date.year,
extract("month", Expense.time) == filter_date.month,
)
elif start_date and end_date:
daily_expenses = (
db.query(
func.date(Expense.time).label("date"),
func.sum(Expense.amount).label("amount"),
)
.filter(
and_(
Expense.user_id == user_id,
Expense.time >= start_date,
Expense.time <= end_date,
)
)
.group_by(func.date(Expense.time))
.all()
daily_expenses = daily_expenses.filter(
Expense.time >= start_date,
Expense.time <= end_date,
)
daily_expenses = daily_expenses.all()
return daily_expenses


Expand Down

0 comments on commit 0b254bd

Please sign in to comment.