50 lines
2.0 KiB
Python
50 lines
2.0 KiB
Python
from fastapi import APIRouter
|
|
from ..clients.firefly_client import firefly_client
|
|
|
|
router = APIRouter()
|
|
|
|
@router.get("/dashboard")
|
|
async def dashboard_summary():
|
|
transactions = await firefly_client.get_transactions()
|
|
total_transactions = len(transactions)
|
|
total_expenses = 0.0
|
|
total_income = 0.0
|
|
category_totals = {}
|
|
|
|
for transaction in transactions:
|
|
attrs = transaction.get("attributes", {})
|
|
legs = attrs.get("transactions", []) or []
|
|
|
|
# Each transaction may contain one or more legs; sum per-leg amounts
|
|
for leg in legs:
|
|
# amount is typically a string like "4.870000000000"
|
|
try:
|
|
leg_amount = float(leg.get("amount", 0))
|
|
except Exception:
|
|
leg_amount = 0.0
|
|
|
|
leg_type = (leg.get("type") or "").lower()
|
|
# classify by leg type: 'withdrawal' -> expense, 'deposit' -> income
|
|
if leg_type == "withdrawal" or (leg.get("destination_type") or "").lower().startswith("expense"):
|
|
total_expenses += abs(leg_amount)
|
|
category_name = leg.get("category_name") or leg.get("category") or attrs.get("category_name") or attrs.get("category_id") or "Uncategorized"
|
|
category_totals[category_name] = category_totals.get(category_name, 0) + abs(leg_amount)
|
|
elif leg_type == "deposit" or (leg.get("destination_type") or "").lower().startswith("asset"):
|
|
total_income += leg_amount
|
|
else:
|
|
# fallback: if leg_amount > 0 treat as income
|
|
if leg_amount > 0:
|
|
total_income += leg_amount
|
|
|
|
top_categories = [
|
|
{"name": name, "amount": amount}
|
|
for name, amount in sorted(category_totals.items(), key=lambda item: item[1], reverse=True)
|
|
][:5]
|
|
|
|
return {
|
|
"total_transactions": total_transactions,
|
|
"total_expenses": total_expenses,
|
|
"total_income": total_income,
|
|
"top_categories": top_categories,
|
|
}
|