4432d47ff9
- 后端:dashboard 路由(summary 聚合 + scores 列表) - 前端:驾驶舱首页(KPI 卡片 + 健康度概览) - 测试:4 个仪表盘测试(总计 37 tests passed) - 前端构建 12 路由成功
108 lines
3.6 KiB
Python
108 lines
3.6 KiB
Python
"""健康度评分 + 仪表盘路由。"""
|
|
|
|
from fastapi import APIRouter, Depends, Query
|
|
from sqlalchemy import func, select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.core.database import get_db
|
|
from app.core.dependencies import get_current_user
|
|
from app.models.company import Company
|
|
from app.models.health_score import HealthScore
|
|
from app.models.report import MonthlyReport
|
|
from app.models.risk import RiskEvent
|
|
from app.models.user import User
|
|
from app.schemas.common import ApiResponse, success
|
|
from app.schemas.health_score import DashboardSummary, HealthScoreResponse
|
|
|
|
router = APIRouter(prefix="/dashboard", tags=["dashboard"])
|
|
|
|
|
|
@router.get("/summary", response_model=ApiResponse[DashboardSummary])
|
|
async def get_dashboard_summary(
|
|
db: AsyncSession = Depends(get_db),
|
|
user: User = Depends(get_current_user),
|
|
):
|
|
"""获取仪表盘汇总数据。"""
|
|
tenant_id = user.tenant_id
|
|
|
|
# 企业总数
|
|
companies_result = await db.execute(
|
|
select(func.count()).select_from(Company).where(Company.tenant_id == tenant_id)
|
|
)
|
|
total_companies = companies_result.scalar_one()
|
|
|
|
# 平均健康度
|
|
avg_result = await db.execute(
|
|
select(func.avg(HealthScore.total_score))
|
|
.join(Company, HealthScore.company_id == Company.id)
|
|
.where(Company.tenant_id == tenant_id)
|
|
)
|
|
avg_score = avg_result.scalar_one()
|
|
avg_health_score = float(avg_score) if avg_score else 0.0
|
|
|
|
# 高风险事件数
|
|
risk_result = await db.execute(
|
|
select(func.count())
|
|
.select_from(RiskEvent)
|
|
.join(Company, RiskEvent.company_id == Company.id)
|
|
.where(Company.tenant_id == tenant_id, RiskEvent.status.in_(["open", "assigned", "in_progress"]))
|
|
)
|
|
high_risk_count = risk_result.scalar_one()
|
|
|
|
# 待审阅月报数
|
|
pending_result = await db.execute(
|
|
select(func.count())
|
|
.select_from(MonthlyReport)
|
|
.join(Company, MonthlyReport.company_id == Company.id)
|
|
.where(Company.tenant_id == tenant_id, MonthlyReport.status.in_(["submitted", "ai_parsed"]))
|
|
)
|
|
pending_reports = pending_result.scalar_one()
|
|
|
|
# 最近评分(最多 10 条)
|
|
recent_result = await db.execute(
|
|
select(HealthScore)
|
|
.join(Company, HealthScore.company_id == Company.id)
|
|
.where(Company.tenant_id == tenant_id)
|
|
.order_by(HealthScore.calculated_at.desc())
|
|
.limit(10)
|
|
)
|
|
recent_scores = [
|
|
HealthScoreResponse.model_validate(s, from_attributes=True)
|
|
for s in recent_result.scalars().all()
|
|
]
|
|
|
|
return success(
|
|
data=DashboardSummary(
|
|
total_companies=total_companies,
|
|
avg_health_score=round(avg_health_score, 1),
|
|
high_risk_count=high_risk_count,
|
|
pending_reports=pending_reports,
|
|
recent_scores=recent_scores,
|
|
)
|
|
)
|
|
|
|
|
|
@router.get("/scores", response_model=ApiResponse[list[HealthScoreResponse]])
|
|
async def list_health_scores(
|
|
company_id: str | None = Query(default=None),
|
|
limit: int = Query(default=20, ge=1, le=100),
|
|
db: AsyncSession = Depends(get_db),
|
|
user: User = Depends(get_current_user),
|
|
):
|
|
"""获取健康度评分列表。"""
|
|
query = (
|
|
select(HealthScore)
|
|
.join(Company, HealthScore.company_id == Company.id)
|
|
.where(Company.tenant_id == user.tenant_id)
|
|
)
|
|
if company_id:
|
|
query = query.where(HealthScore.company_id == company_id)
|
|
|
|
query = query.order_by(HealthScore.calculated_at.desc()).limit(limit)
|
|
result = await db.execute(query)
|
|
scores = [
|
|
HealthScoreResponse.model_validate(s, from_attributes=True)
|
|
for s in result.scalars().all()
|
|
]
|
|
return success(data=scores)
|