44 lines
1.8 KiB
Python
44 lines
1.8 KiB
Python
"""Routes — tableau de bord principal."""
|
||
from fastapi import APIRouter, Depends, Request
|
||
from fastapi.responses import HTMLResponse
|
||
from app.auth import require_auth
|
||
from app.baserow import list_rows
|
||
from app.templates import templates
|
||
from app.config import TABLE_VEHICULES, TABLE_APPROVISIONNEMENTS, TABLE_ANOMALIES
|
||
|
||
router = APIRouter()
|
||
|
||
|
||
@router.get("/", response_class=HTMLResponse)
|
||
async def dashboard(request: Request, user=Depends(require_auth)):
|
||
vehicules = await list_rows(TABLE_VEHICULES)
|
||
appros = await list_rows(TABLE_APPROVISIONNEMENTS, size=50)
|
||
anomalies = await list_rows(TABLE_ANOMALIES, size=20)
|
||
|
||
# Stats globales
|
||
total_appros = len(appros)
|
||
total_anomalies = len([a for a in anomalies if not a.get("تمت_المعالجة")])
|
||
total_vehicules = len(vehicules)
|
||
|
||
# Consommation moyenne par véhicule
|
||
stats_vehicules = []
|
||
for v in vehicules:
|
||
matricule = v.get("رقم_الماتريكول", "")
|
||
appros_v = [a for a in appros if a.get("رقم_الماتريكول") == matricule]
|
||
conso_values = [a.get("الاستهلاك_100كم") for a in appros_v if a.get("الاستهلاك_100كم")]
|
||
conso_moy = round(sum(conso_values) / len(conso_values), 2) if conso_values else 0
|
||
stats_vehicules.append({
|
||
"matricule": matricule,
|
||
"type": v.get("النوع", ""),
|
||
"nb_appros": len(appros_v),
|
||
"conso_moyenne": conso_moy,
|
||
"nb_anomalies": len([a for a in anomalies if a.get("رقم_الماتريكول") == matricule and not a.get("تمت_المعالجة")]),
|
||
})
|
||
|
||
return templates.TemplateResponse("dashboard.html", {
|
||
"request": request,
|
||
"total_v": total_vehicules,
|
||
"total_appro": total_appros,
|
||
"total_anomalies": total_anomalies,
|
||
"stats_vehicules": stats_vehicules,
|
||
}) |