From 5b4a39924ed4df95be7a1b08705f423f2b35d162 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?IT=E7=8B=97?= Date: Mon, 20 Jul 2026 23:55:39 +0800 Subject: [PATCH] Add missing dashboard endpoints: /api/dashboard/summary, /api/attendance/stats, /api/accident/dashboard/* Frontend files: export HTML buttons on Dashboard + List pages --- backend/main.py | 96 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) diff --git a/backend/main.py b/backend/main.py index d46a310..4673740 100644 --- a/backend/main.py +++ b/backend/main.py @@ -617,6 +617,102 @@ async def list_records( "data": data } +# ============ Dashboard Summary (combined) ============ +@app.get("/api/dashboard/summary") +async def dashboard_summary(current_user: User = Depends(get_current_user)): + """Combined dashboard data for frontend Dashboard page.""" + # Attendance data + att_headers, att_rows = read_excel_file("attendance") + # Accident data + acc_headers, acc_rows = read_excel_file("accident") + + return { + "attendance": { + "total": len(att_rows) if att_rows else 0, + "this_month": 0, + }, + "accident": { + "total": len(acc_rows) if acc_rows else 0, + "this_month": 0, + } + } + +# ============ Attendance Stats ============ +@app.get("/api/attendance/stats") +async def attendance_stats(current_user: User = Depends(get_current_user)): + """Stats for attendance dashboard.""" + headers, rows = read_excel_file("attendance") + if headers is None: + return {"total": 0, "by_department": {}, "by_status": {}} + + dept_col = None + for i, h in enumerate(headers): + if h and any(k in str(h).lower() for k in ["company", "dept", "部門", "公司"]): + dept_col = i; break + + dept_map = {} + for row in rows: + if dept_col is not None and dept_col < len(row): + d = str(row[dept_col] or "Unknown") + dept_map[d] = dept_map.get(d, 0) + 1 + + return {"total": len(rows), "by_department": dept_map, "by_status": {}} + +# ============ Accident Dashboard ============ +@app.get("/api/accident/dashboard/summary") +async def accident_dashboard_summary(current_user: User = Depends(get_current_user)): + """Accident dashboard summary.""" + headers, rows = read_excel_file("accident") + if headers is None: + return {"total": 0, "by_severity": {}, "by_location": {}, "recent": []} + + sev_col = None; loc_col = None; date_col = None + for i, h in enumerate(headers): + if h and any(k in str(h) for k in ["緊急", "程度", "severity", "Severity"]): + sev_col = i + elif h and any(k in str(h) for k in ["地點", "部門", "location", "Location"]): + loc_col = i + elif h and any(k in str(h) for k in ["日期", "date", "Date"]): + date_col = i + + sev_map = {}; loc_map = {}; recent = [] + for row in rows: + if sev_col is not None and sev_col < len(row) and row[sev_col]: + sev_map[str(row[sev_col])] = sev_map.get(str(row[sev_col]), 0) + 1 + if loc_col is not None and loc_col < len(row) and row[loc_col]: + loc_map[str(row[loc_col])] = loc_map.get(str(row[loc_col]), 0) + 1 + if len(recent) < 10: + recent.append({"date": str(row[date_col] if date_col and date_col < len(row) else "")}) + + return {"total": len(rows), "by_severity": sev_map, "by_location": loc_map, "recent": recent} + +@app.get("/api/accident/dashboard/stats") +async def accident_dashboard_stats(current_user: User = Depends(get_current_user)): + """Accident dashboard stats.""" + headers, rows = read_excel_file("accident") + if headers is None: + return {"total": 0, "by_severity": {}, "by_location": {}, "recent": []} + + sev_col = None; loc_col = None; date_col = None + for i, h in enumerate(headers): + if h and any(k in str(h) for k in ["緊急", "程度", "severity", "Severity"]): + sev_col = i + elif h and any(k in str(h) for k in ["地點", "部門", "location", "Location"]): + loc_col = i + elif h and any(k in str(h) for k in ["日期", "date", "Date"]): + date_col = i + + sev_map = {}; loc_map = {}; recent = [] + for row in rows: + if sev_col is not None and sev_col < len(row) and row[sev_col]: + sev_map[str(row[sev_col])] = sev_map.get(str(row[sev_col]), 0) + 1 + if loc_col is not None and loc_col < len(row) and row[loc_col]: + loc_map[str(row[loc_col])] = loc_map.get(str(row[loc_col]), 0) + 1 + if len(recent) < 10: + recent.append({"date": str(row[date_col] if date_col and date_col < len(row) else "")}) + + return {"total": len(rows), "by_severity": sev_map, "by_location": loc_map, "recent": recent} + # ============ Dashboard (from Excel) ============ @app.get("/api/dashboard/{section}") async def dashboard(section: str, current_user: User = Depends(get_current_user)):