Improve performance by JOINing postings when extracting transactions

This commit is contained in:
RunasSudo 2023-01-05 01:15:39 +11:00
parent e3900589e6
commit 2e5ae4c20b
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
4 changed files with 17 additions and 9 deletions

View File

@ -41,7 +41,7 @@ def tax_summary_report():
# Get trial balance # Get trial balance
balancer = TrialBalancer() balancer = TrialBalancer()
#balancer.apply_transactions(all_transactions()) #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())) accounts = dict(sorted(balancer.accounts.items()))

View File

@ -27,10 +27,12 @@ from datetime import datetime
@app.route('/journal') @app.route('/journal')
def journal(): def journal():
transactions = db.session.scalars(db.select(Transaction).options(db.selectinload(Transaction.postings)).order_by(Transaction.dt)).all()
return render_template( return render_template(
'journal/journal.html', 'journal/journal.html',
commodity_detail=request.args.get('commodity-detail', '0') == '1', 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']) @app.route('/journal/new-transaction', methods=['GET', 'POST'])

View File

@ -24,12 +24,15 @@ from .models import StatementLine, StatementLineReconciliation
@app.route('/statement-lines') @app.route('/statement-lines')
def statement_lines(): def statement_lines():
if 'account' in request.args: # JOIN all associated postings (called in is_complex/charge_account)
statement_lines = db.select(StatementLine).where(StatementLine.source_account == request.args['account']).order_by(StatementLine.dt) statement_lines = db.select(StatementLine).options(
else: db.joinedload(StatementLine.reconciliation).joinedload(StatementLineReconciliation.posting).joinedload(Posting.transaction).joinedload(Transaction.postings)
statement_lines = db.select(StatementLine).order_by(StatementLine.dt) ).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) return render_template('statements/statement_lines.html', page=page)

View File

@ -30,9 +30,12 @@ app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///drcr.db'
app.config['SQLALCHEMY_RECORD_QUERIES'] = app.debug app.config['SQLALCHEMY_RECORD_QUERIES'] = app.debug
db.init_app(app) db.init_app(app)
def all_transactions(): def all_transactions(join_postings=True):
# All Transactions in database # 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 # Unreconciled StatementLines
transactions.extend(line.into_transaction() for line in StatementLine.query.filter(StatementLine.reconciliation == None)) transactions.extend(line.into_transaction() for line in StatementLine.query.filter(StatementLine.reconciliation == None))