FastAPI backend with auth, user, instance, ticket, query, dashboard APIs. React + TypeScript frontend with mock data UI (10 pages). SQLite + async SQLAlchemy, JWT auth, role-based access control. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
63 lines
2.2 KiB
Python
63 lines
2.2 KiB
Python
from datetime import datetime, timedelta, timezone
|
|
from fastapi import APIRouter, Depends
|
|
from sqlalchemy import select, func
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.core.database import get_db
|
|
from app.core.security import get_current_user
|
|
from app.models.user import User
|
|
from app.models.ticket import Ticket
|
|
from app.models.instance import Instance
|
|
from app.models.audit_log import AuditLog
|
|
from app.schemas.dashboard import DashboardStats
|
|
|
|
router = APIRouter(prefix="/dashboard", tags=["仪表盘"])
|
|
|
|
|
|
@router.get("/stats", response_model=DashboardStats)
|
|
async def get_stats(
|
|
db: AsyncSession = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
now = datetime.now(timezone.utc)
|
|
month_ago = now - timedelta(days=30)
|
|
today_start = now.replace(hour=0, minute=0, second=0, microsecond=0)
|
|
|
|
ticket_total = (await db.execute(select(func.count(Ticket.id)))).scalar() or 0
|
|
ticket_pending = (await db.execute(
|
|
select(func.count(Ticket.id)).where(Ticket.status.in_(["pending", "reviewing"]))
|
|
)).scalar() or 0
|
|
ticket_month = (await db.execute(
|
|
select(func.count(Ticket.id)).where(Ticket.create_time >= month_ago)
|
|
)).scalar() or 0
|
|
ticket_executing = (await db.execute(
|
|
select(func.count(Ticket.id)).where(Ticket.status == "executing")
|
|
)).scalar() or 0
|
|
|
|
instance_total = (await db.execute(select(func.count(Instance.id)))).scalar() or 0
|
|
instance_online = (await db.execute(
|
|
select(func.count(Instance.id)).where(Instance.status == "connected")
|
|
)).scalar() or 0
|
|
|
|
user_total = (await db.execute(
|
|
select(func.count(User.id)).where(User.is_active == True)
|
|
)).scalar() or 0
|
|
|
|
queries_today = (await db.execute(
|
|
select(func.count(AuditLog.id)).where(
|
|
AuditLog.action == "QUERY",
|
|
AuditLog.created_at >= today_start,
|
|
)
|
|
)).scalar() or 0
|
|
|
|
return DashboardStats(
|
|
ticket_total=ticket_total,
|
|
ticket_pending=ticket_pending,
|
|
ticket_month=ticket_month,
|
|
ticket_executing=ticket_executing,
|
|
instance_total=instance_total,
|
|
instance_online=instance_online,
|
|
user_total=user_total,
|
|
queries_today=queries_today,
|
|
)
|