Add missing dashboard endpoints: /api/dashboard/summary, /api/attendance/stats, /api/accident/dashboard/*
Frontend files: export HTML buttons on Dashboard + List pages
This commit is contained in:
@@ -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)):
|
||||
|
||||
Reference in New Issue
Block a user