"""Dashboard: KPIs, gastos por mês e por fornecedor, últimas notas.""" from __future__ import annotations from fastapi import APIRouter, Query, Request from .. import database as db from ..dates import format_competencia from ..storage import money from ..templating import render router = APIRouter() @router.get("/") def dashboard(request: Request, category: str = Query("")): category = category or "" with db.session() as conn: categorias = [dict(r) for r in db.list_categorias(conn)] totals = db.overall_totals(conn, category=category or None) months = [dict(r) for r in db.monthly_totals(conn, category=category or None)] suppliers = [dict(r) for r in db.supplier_totals(conn, limit=8, category=category or None)] categories = [dict(r) for r in db.category_totals(conn)] recent = [dict(r) for r in db.list_fiscal(conn, category=category or None)][:10] max_month = max((m["total"] for m in months), default=0) or 1 for m in months: m["label"] = format_competencia(m["mes"], m["ano"]) m["pct"] = round(m["total"] / max_month * 100, 1) max_sup = max((s["total"] for s in suppliers), default=0) or 1 for s in suppliers: s["pct"] = round(s["total"] / max_sup * 100, 1) max_cat = max((c["total"] for c in categories), default=0) or 1 for c in categories: c["pct"] = round(c["total"] / max_cat * 100, 1) return render( request, "dashboard.html", totals=totals, months=months, suppliers=suppliers, categories=categories, categorias=categorias, recent=recent, selected_category=category, money=money, )