From fbda8e29804518ce910613ec2feb862954ea284b Mon Sep 17 00:00:00 2001 From: padreug Date: Mon, 10 Nov 2025 03:59:24 +0100 Subject: [PATCH] Improves readability and reduces logging verbosity Removes excessive logging to improve readability and reduce verbosity. Streamlines balance processing and improves logging for settlement amounts. Adds a note about Fava's internal normalization behavior to the beancount formatting. --- beancount_format.py | 4 ++++ fava_client.py | 19 ++----------------- tasks.py | 8 +------- 3 files changed, 7 insertions(+), 24 deletions(-) diff --git a/beancount_format.py b/beancount_format.py index f0ec9c7..adc97c6 100644 --- a/beancount_format.py +++ b/beancount_format.py @@ -576,6 +576,10 @@ def format_net_settlement_entry( Fava API entry dict """ # Build postings for net settlement + # Note: We use @@ (total price) syntax for cleaner formatting, but Fava's API + # will convert this to @ (per-unit price) with a long decimal when writing to file. + # This is Fava's internal normalization behavior and cannot be changed via API. + # The accounting is still 100% correct, just not as visually clean. postings = [ { "account": payment_account, diff --git a/fava_client.py b/fava_client.py index 4e38507..5ae0da3 100644 --- a/fava_client.py +++ b/fava_client.py @@ -193,12 +193,9 @@ class FavaClient: # Get all journal entries for this user all_entries = await self.get_journal_entries() - logger.info(f"Processing {len(all_entries)} journal entries for user {user_id[:8]}") - total_sats = 0 fiat_balances = {} accounts_dict = {} # Track balances per account - processed_count = 0 for entry in all_entries: # Skip non-transactions, pending (!), and voided @@ -209,11 +206,6 @@ class FavaClient: if "voided" in entry.get("tags", []): continue - # Check if this entry has any User-375ec158 postings - has_user_posting = any(f":User-{user_id[:8]}" in p.get("account", "") for p in entry.get("postings", [])) - if has_user_posting: - logger.info(f"Entry '{entry.get('narration', 'N/A')}' has {len(entry.get('postings', []))} postings") - # Process postings for this user for posting in entry.get("postings", []): account_name = posting.get("account", "") @@ -229,9 +221,6 @@ class FavaClient: if not isinstance(amount_str, str) or not amount_str: continue - logger.info(f"Processing posting in {account_name}: amount_str='{amount_str}'") - processed_count += 1 - import re # Try to extract EUR/USD amount first (new format) fiat_match = re.match(r'^(-?[\d.]+)\s+([A-Z]{3})$', amount_str) @@ -244,7 +233,6 @@ class FavaClient: fiat_balances[fiat_currency] = Decimal(0) fiat_balances[fiat_currency] += fiat_amount - logger.info(f"Found fiat in {account_name}: {fiat_amount} {fiat_currency} (direct), running total: {fiat_balances[fiat_currency]}") # Also track SATS equivalent from metadata if available posting_meta = posting.get("meta", {}) @@ -288,16 +276,13 @@ class FavaClient: fiat_total = -fiat_total fiat_balances[fiat_currency] += fiat_total - logger.info(f"Found fiat in {account_name}: {fiat_total} {fiat_currency} (from SATS metadata), running total: {fiat_balances[fiat_currency]}") - result = { + logger.info(f"User {user_id[:8]} balance: {total_sats} sats, fiat: {dict(fiat_balances)}") + return { "balance": total_sats, "fiat_balances": fiat_balances, "accounts": list(accounts_dict.values()) } - logger.info(f"Processed {processed_count} postings for user {user_id[:8]}") - logger.info(f"Returning balance for user {user_id[:8]}: sats={total_sats}, fiat_balances={fiat_balances}") - return result async def get_all_user_balances(self) -> List[Dict[str, Any]]: """ diff --git a/tasks.py b/tasks.py index 208fc09..d6e6c56 100644 --- a/tasks.py +++ b/tasks.py @@ -183,11 +183,9 @@ async def on_invoice_paid(payment: Payment) -> None: # Extract fiat metadata from invoice (if present) fiat_currency = None fiat_amount = None - logger.info(f"Payment.extra in webhook: {payment.extra}") if payment.extra: fiat_currency = payment.extra.get("fiat_currency") fiat_amount_str = payment.extra.get("fiat_amount") - logger.info(f"Extracted from extra - fiat_currency: {fiat_currency}, fiat_amount_str: {fiat_amount_str}") if fiat_amount_str: fiat_amount = Decimal(str(fiat_amount_str)) @@ -195,15 +193,11 @@ async def on_invoice_paid(payment: Payment) -> None: logger.error(f"Payment {payment.payment_hash} missing fiat currency/amount metadata") return - logger.info(f"Final fiat values for payment entry - currency: {fiat_currency}, amount: {fiat_amount}") - # Get user's current balance to determine receivables and payables balance = await fava.get_user_balance(user_id) fiat_balances = balance.get("fiat_balances", {}) total_fiat_balance = fiat_balances.get(fiat_currency, Decimal(0)) - logger.info(f"User {user_id[:8]} current balance: {total_fiat_balance} {fiat_currency}") - # Determine receivables and payables based on balance # Positive balance = user owes castle (receivable) # Negative balance = castle owes user (payable) @@ -216,7 +210,7 @@ async def on_invoice_paid(payment: Payment) -> None: total_receivable = Decimal(0) total_payable = abs(total_fiat_balance) - logger.info(f"Settlement amounts - Receivable: {total_receivable}, Payable: {total_payable}, Net: {fiat_amount}") + logger.info(f"Settlement: {fiat_amount} {fiat_currency} (Receivable: {total_receivable}, Payable: {total_payable})") # Get account names user_receivable = await get_or_create_user_account(