From 0b254bd282d0820db8c207525651762da5db4d90 Mon Sep 17 00:00:00 2001 From: RezenkovD Date: Wed, 6 Sep 2023 13:51:45 +0300 Subject: [PATCH] ref: improve queries to db --- src/services/user.py | 116 ++++++++----------------------------------- 1 file changed, 21 insertions(+), 95 deletions(-) diff --git a/src/services/user.py b/src/services/user.py index 8ca6fa1..2873244 100644 --- a/src/services/user.py +++ b/src/services/user.py @@ -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"), @@ -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 @@ -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