Features: - FastAPI backend with JWT authentication - MySQL database with SQLAlchemy ORM - KPI workflow: draft → pending → approved → evaluation → completed - Ollama LLM API integration for AI features - Gitea API integration for version control - Complete API endpoints for KPI, dashboard, notifications Tables: KPI_D_* prefix naming convention 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
106 lines
2.9 KiB
Python
106 lines
2.9 KiB
Python
"""
|
|
儀表板 API
|
|
"""
|
|
from typing import Optional, List
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status, Query
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.api.deps import get_db, get_current_user, get_current_manager
|
|
from app.models.employee import Employee
|
|
from app.services.dashboard_service import DashboardService
|
|
from app.schemas.dashboard import (
|
|
DashboardProgressResponse,
|
|
DashboardDistributionResponse,
|
|
DashboardTrendsResponse,
|
|
DashboardAlertResponse,
|
|
)
|
|
from app.schemas.common import MessageResponse
|
|
|
|
router = APIRouter(prefix="/api/dashboard", tags=["儀表板"])
|
|
|
|
|
|
@router.get("/progress", response_model=DashboardProgressResponse)
|
|
def get_progress(
|
|
period_id: Optional[int] = Query(None),
|
|
department_id: Optional[int] = Query(None),
|
|
db: Session = Depends(get_db),
|
|
current_user: Employee = Depends(get_current_user),
|
|
):
|
|
"""
|
|
取得進度統計
|
|
|
|
返回指定期間的 KPI 完成進度,包含各狀態的數量統計。
|
|
"""
|
|
service = DashboardService(db)
|
|
return service.get_progress(period_id, department_id)
|
|
|
|
|
|
@router.get("/distribution", response_model=DashboardDistributionResponse)
|
|
def get_distribution(
|
|
period_id: Optional[int] = Query(None),
|
|
db: Session = Depends(get_db),
|
|
current_user: Employee = Depends(get_current_user),
|
|
):
|
|
"""
|
|
取得分佈統計
|
|
|
|
返回 KPI 的部門分佈、狀態分佈、分數區間分佈。
|
|
"""
|
|
service = DashboardService(db)
|
|
return service.get_distribution(period_id)
|
|
|
|
|
|
@router.get("/trends", response_model=DashboardTrendsResponse)
|
|
def get_trends(
|
|
limit: int = Query(4, ge=1, le=10),
|
|
db: Session = Depends(get_db),
|
|
current_user: Employee = Depends(get_current_user),
|
|
):
|
|
"""
|
|
取得趨勢統計
|
|
|
|
返回歷史期間的平均分數趨勢。
|
|
"""
|
|
service = DashboardService(db)
|
|
return service.get_trends(limit)
|
|
|
|
|
|
@router.get("/alerts", response_model=List[DashboardAlertResponse])
|
|
def get_alerts(
|
|
is_resolved: Optional[bool] = Query(False),
|
|
limit: int = Query(50, ge=1, le=200),
|
|
db: Session = Depends(get_db),
|
|
current_user: Employee = Depends(get_current_manager),
|
|
):
|
|
"""
|
|
取得儀表板警示
|
|
|
|
返回系統警示列表(主管以上可查看)。
|
|
"""
|
|
service = DashboardService(db)
|
|
return service.get_alerts(is_resolved, limit)
|
|
|
|
|
|
@router.put("/alerts/{alert_id}/resolve", response_model=DashboardAlertResponse)
|
|
def resolve_alert(
|
|
alert_id: int,
|
|
db: Session = Depends(get_db),
|
|
current_user: Employee = Depends(get_current_manager),
|
|
):
|
|
"""
|
|
解決警示
|
|
|
|
將指定警示標記為已解決。
|
|
"""
|
|
service = DashboardService(db)
|
|
alert = service.resolve_alert(alert_id, current_user.id)
|
|
|
|
if not alert:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail={"code": "ALERT001", "message": "警示不存在"},
|
|
)
|
|
|
|
return alert
|