From 2e5ae4c20b470abf9c1ce9aafafebdde514eb006 Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Thu, 5 Jan 2023 01:15:39 +1100 Subject: [PATCH] Improve performance by JOINing postings when extracting transactions --- austax/reports.py | 2 +- drcr/journal/views.py | 4 +++- drcr/statements/views.py | 13 ++++++++----- drcr/webapp.py | 7 +++++-- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/austax/reports.py b/austax/reports.py index 1a6c28d..5f66251 100644 --- a/austax/reports.py +++ b/austax/reports.py @@ -41,7 +41,7 @@ def tax_summary_report(): # Get trial balance balancer = TrialBalancer() #balancer.apply_transactions(all_transactions()) - balancer.apply_transactions(db.session.scalars(db.select(Transaction)).all()) + balancer.apply_transactions(db.session.scalars(db.select(Transaction).options(db.selectinload(Transaction.postings))).all()) accounts = dict(sorted(balancer.accounts.items())) diff --git a/drcr/journal/views.py b/drcr/journal/views.py index 5795ad3..e666c35 100644 --- a/drcr/journal/views.py +++ b/drcr/journal/views.py @@ -27,10 +27,12 @@ from datetime import datetime @app.route('/journal') def journal(): + transactions = db.session.scalars(db.select(Transaction).options(db.selectinload(Transaction.postings)).order_by(Transaction.dt)).all() + return render_template( 'journal/journal.html', commodity_detail=request.args.get('commodity-detail', '0') == '1', - transactions=sorted(Transaction.query.all(), key=lambda t: t.dt) + transactions=transactions ) @app.route('/journal/new-transaction', methods=['GET', 'POST']) diff --git a/drcr/statements/views.py b/drcr/statements/views.py index 994a82d..7171725 100644 --- a/drcr/statements/views.py +++ b/drcr/statements/views.py @@ -24,12 +24,15 @@ from .models import StatementLine, StatementLineReconciliation @app.route('/statement-lines') def statement_lines(): - if 'account' in request.args: - statement_lines = db.select(StatementLine).where(StatementLine.source_account == request.args['account']).order_by(StatementLine.dt) - else: - statement_lines = db.select(StatementLine).order_by(StatementLine.dt) + # JOIN all associated postings (called in is_complex/charge_account) + statement_lines = db.select(StatementLine).options( + db.joinedload(StatementLine.reconciliation).joinedload(StatementLineReconciliation.posting).joinedload(Posting.transaction).joinedload(Transaction.postings) + ).order_by(StatementLine.dt) - page = db.paginate(statement_lines, per_page=request.args.get('per_page', 100)) + if 'account' in request.args: + statement_lines = statement_lines.where(StatementLine.source_account == request.args['account']) + + page = db.paginate(statement_lines, per_page=int(request.args.get('per_page', 1000))) return render_template('statements/statement_lines.html', page=page) diff --git a/drcr/webapp.py b/drcr/webapp.py index 68a371c..f02a84e 100644 --- a/drcr/webapp.py +++ b/drcr/webapp.py @@ -30,9 +30,12 @@ app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///drcr.db' app.config['SQLALCHEMY_RECORD_QUERIES'] = app.debug db.init_app(app) -def all_transactions(): +def all_transactions(join_postings=True): # All Transactions in database - transactions = db.session.scalars(db.select(Transaction)).all() + if join_postings: + transactions = db.session.scalars(db.select(Transaction).options(db.selectinload(Transaction.postings))).all() + else: + transactions = db.session.scalars(db.select(Transaction)).all() # Unreconciled StatementLines transactions.extend(line.into_transaction() for line in StatementLine.query.filter(StatementLine.reconciliation == None))