06/05 Optimize app
This commit is contained in:
@@ -5,48 +5,45 @@ plus any incoming transfers minus outgoing transfers.
|
||||
"""
|
||||
|
||||
from decimal import Decimal
|
||||
from sqlalchemy import func
|
||||
from sqlalchemy import func, case, or_, and_
|
||||
from app.extensions import db
|
||||
from app.models.account import Account
|
||||
from app.models.transaction import Transaction
|
||||
|
||||
|
||||
def calc_balance(account_id):
|
||||
"""Recalculate and persist the balance for a given account."""
|
||||
# Income credited to this account
|
||||
income = db.session.query(
|
||||
func.coalesce(func.sum(Transaction.amount), 0)
|
||||
"""
|
||||
Recalculate and persist the balance for a given account.
|
||||
Uses a single aggregation query with CASE expressions instead of 4 queries.
|
||||
"""
|
||||
row = db.session.query(
|
||||
func.coalesce(func.sum(case(
|
||||
(and_(Transaction.account_id == account_id,
|
||||
Transaction.transaction_type == 'income'), Transaction.amount),
|
||||
else_=0
|
||||
)), 0),
|
||||
func.coalesce(func.sum(case(
|
||||
(and_(Transaction.account_id == account_id,
|
||||
Transaction.transaction_type == 'expense'), Transaction.amount),
|
||||
else_=0
|
||||
)), 0),
|
||||
func.coalesce(func.sum(case(
|
||||
(and_(Transaction.account_id == account_id,
|
||||
Transaction.transaction_type == 'transfer'), Transaction.amount),
|
||||
else_=0
|
||||
)), 0),
|
||||
func.coalesce(func.sum(case(
|
||||
(and_(Transaction.to_account_id == account_id,
|
||||
Transaction.transaction_type == 'transfer'), Transaction.amount),
|
||||
else_=0
|
||||
)), 0),
|
||||
).filter(
|
||||
Transaction.account_id == account_id,
|
||||
Transaction.transaction_type == 'income'
|
||||
).scalar()
|
||||
or_(Transaction.account_id == account_id,
|
||||
Transaction.to_account_id == account_id)
|
||||
).one()
|
||||
|
||||
# Expenses debited from this account
|
||||
expense = db.session.query(
|
||||
func.coalesce(func.sum(Transaction.amount), 0)
|
||||
).filter(
|
||||
Transaction.account_id == account_id,
|
||||
Transaction.transaction_type == 'expense'
|
||||
).scalar()
|
||||
|
||||
# Transfers out (this account is source)
|
||||
transfer_out = db.session.query(
|
||||
func.coalesce(func.sum(Transaction.amount), 0)
|
||||
).filter(
|
||||
Transaction.account_id == account_id,
|
||||
Transaction.transaction_type == 'transfer'
|
||||
).scalar()
|
||||
|
||||
# Transfers in (this account is destination)
|
||||
transfer_in = db.session.query(
|
||||
func.coalesce(func.sum(Transaction.amount), 0)
|
||||
).filter(
|
||||
Transaction.to_account_id == account_id,
|
||||
Transaction.transaction_type == 'transfer'
|
||||
).scalar()
|
||||
|
||||
balance = Decimal(str(income)) - Decimal(str(expense)) \
|
||||
- Decimal(str(transfer_out)) + Decimal(str(transfer_in))
|
||||
income, expense, transfer_out, transfer_in = (Decimal(str(v)) for v in row)
|
||||
balance = income - expense - transfer_out + transfer_in
|
||||
|
||||
account = db.session.get(Account, account_id)
|
||||
if account:
|
||||
|
||||
Reference in New Issue
Block a user