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.
This commit is contained in:
padreug 2025-11-10 03:59:24 +01:00
parent 313265b185
commit fbda8e2980
3 changed files with 7 additions and 24 deletions

View file

@ -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,

View file

@ -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]]:
"""

View file

@ -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(