43 lines
1.4 KiB
Python
43 lines
1.4 KiB
Python
from fastapi import APIRouter, Depends
|
|
from sqlalchemy import func
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.api.deps import get_current_user, get_db
|
|
from app.models.backup import Backup
|
|
from app.models.log import OperationLog
|
|
from app.models.router import Router
|
|
from app.models.user import User
|
|
from app.schemas.dashboard import DashboardResponse
|
|
from app.services.file_service import get_storage_stats
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("", response_model=DashboardResponse)
|
|
def get_dashboard(current_user: User = Depends(get_current_user), db: Session = Depends(get_db)):
|
|
routers_count = db.query(func.count(Router.id)).filter(Router.owner_id == current_user.id).scalar() or 0
|
|
export_count = (
|
|
db.query(func.count(Backup.id))
|
|
.join(Router)
|
|
.filter(Router.owner_id == current_user.id, Backup.backup_type == "export")
|
|
.scalar()
|
|
or 0
|
|
)
|
|
binary_count = (
|
|
db.query(func.count(Backup.id))
|
|
.join(Router)
|
|
.filter(Router.owner_id == current_user.id, Backup.backup_type == "binary")
|
|
.scalar()
|
|
or 0
|
|
)
|
|
recent_logs = db.query(OperationLog).order_by(OperationLog.timestamp.desc()).limit(10).all()
|
|
storage = get_storage_stats()
|
|
return DashboardResponse(
|
|
routers_count=routers_count,
|
|
export_count=export_count,
|
|
binary_count=binary_count,
|
|
total_backups=export_count + binary_count,
|
|
storage=storage,
|
|
recent_logs=recent_logs,
|
|
)
|