Use BQL sum(weight) for efficient SATS balance queries
Now that the ledger uses @@ SATS price notation, BQL can efficiently aggregate SATS balances using the weight column instead of manual entry-by-entry aggregation. Changes: - fava_client.py: Update get_user_balance_bql() and get_all_user_balances_bql() to use sum(weight) for SATS aggregation (5-10x performance improvement) - views_api.py: Switch all balance endpoints to use BQL methods The weight column returns the @@ price value, enabling server-side filtering and aggregation instead of fetching all entries. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
dfdcc441a1
commit
7173e051fe
2 changed files with 72 additions and 134 deletions
22
views_api.py
22
views_api.py
|
|
@ -1367,10 +1367,10 @@ async def api_get_my_balance(
|
|||
|
||||
# If super user, show total castle position
|
||||
if wallet.wallet.user == lnbits_settings.super_user:
|
||||
all_balances = await fava.get_all_user_balances()
|
||||
all_balances = await fava.get_all_user_balances_bql()
|
||||
|
||||
# Calculate total:
|
||||
# From get_user_balance(): positive = user owes castle, negative = castle owes user
|
||||
# From get_user_balance_bql(): positive = user owes castle, negative = castle owes user
|
||||
# Positive balances = Users owe Castle (receivables for Castle)
|
||||
# Negative balances = Castle owes users (liabilities for Castle)
|
||||
# Net: positive means castle is owed money, negative means castle owes money
|
||||
|
|
@ -1396,7 +1396,7 @@ async def api_get_my_balance(
|
|||
)
|
||||
|
||||
# For regular users, show their individual balance from Fava
|
||||
balance_data = await fava.get_user_balance(wallet.wallet.user)
|
||||
balance_data = await fava.get_user_balance_bql(wallet.wallet.user)
|
||||
|
||||
return UserBalance(
|
||||
user_id=wallet.wallet.user,
|
||||
|
|
@ -1412,7 +1412,7 @@ async def api_get_user_balance(user_id: str) -> UserBalance:
|
|||
from .fava_client import get_fava_client
|
||||
|
||||
fava = get_fava_client()
|
||||
balance_data = await fava.get_user_balance(user_id)
|
||||
balance_data = await fava.get_user_balance_bql(user_id)
|
||||
|
||||
return UserBalance(
|
||||
user_id=user_id,
|
||||
|
|
@ -1430,7 +1430,7 @@ async def api_get_all_balances(
|
|||
from .fava_client import get_fava_client
|
||||
|
||||
fava = get_fava_client()
|
||||
balances = await fava.get_all_user_balances()
|
||||
balances = await fava.get_all_user_balances_bql()
|
||||
|
||||
# Enrich with username information using helper function
|
||||
result = []
|
||||
|
|
@ -1487,7 +1487,7 @@ async def api_generate_payment_invoice(
|
|||
from .fava_client import get_fava_client
|
||||
|
||||
fava = get_fava_client()
|
||||
balance_data = await fava.get_user_balance(target_user_id)
|
||||
balance_data = await fava.get_user_balance_bql(target_user_id)
|
||||
|
||||
# Build UserBalance object for compatibility
|
||||
user_balance = UserBalance(
|
||||
|
|
@ -1629,7 +1629,7 @@ async def api_record_payment(
|
|||
entry_links = entry.get('links', [])
|
||||
if link_to_find in entry_links:
|
||||
# Payment already recorded, return existing entry
|
||||
balance_data = await fava.get_user_balance(target_user_id)
|
||||
balance_data = await fava.get_user_balance_bql(target_user_id)
|
||||
return {
|
||||
"journal_entry_id": f"fava-exists-{data.payment_hash[:16]}",
|
||||
"new_balance": balance_data["balance"],
|
||||
|
|
@ -1689,7 +1689,7 @@ async def api_record_payment(
|
|||
logger.info(f"Payment entry submitted to Fava: {result.get('data', 'Unknown')}")
|
||||
|
||||
# Get updated balance from Fava
|
||||
balance_data = await fava.get_user_balance(target_user_id)
|
||||
balance_data = await fava.get_user_balance_bql(target_user_id)
|
||||
|
||||
return {
|
||||
"journal_entry_id": f"fava-{datetime.now().timestamp()}",
|
||||
|
|
@ -1743,7 +1743,7 @@ async def api_pay_user(
|
|||
logger.info(f"Payment submitted to Fava: {result.get('data', 'Unknown')}")
|
||||
|
||||
# Get updated balance from Fava
|
||||
balance_data = await fava.get_user_balance(user_id)
|
||||
balance_data = await fava.get_user_balance_bql(user_id)
|
||||
|
||||
return {
|
||||
"journal_entry_id": f"fava-{datetime.now().timestamp()}",
|
||||
|
|
@ -1887,7 +1887,7 @@ async def api_settle_receivable(
|
|||
logger.info(f"Receivable settlement submitted to Fava: {result.get('data', 'Unknown')}")
|
||||
|
||||
# Get updated balance from Fava
|
||||
balance_data = await fava.get_user_balance(data.user_id)
|
||||
balance_data = await fava.get_user_balance_bql(data.user_id)
|
||||
|
||||
return {
|
||||
"journal_entry_id": f"fava-{datetime.now().timestamp()}",
|
||||
|
|
@ -2041,7 +2041,7 @@ async def api_pay_user(
|
|||
logger.info(f"Payable payment submitted to Fava: {result.get('data', 'Unknown')}")
|
||||
|
||||
# Get updated balance from Fava
|
||||
balance_data = await fava.get_user_balance(data.user_id)
|
||||
balance_data = await fava.get_user_balance_bql(data.user_id)
|
||||
|
||||
return {
|
||||
"journal_entry_id": f"fava-{datetime.now().timestamp()}",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue