80 lines
2.6 KiB
Python
80 lines
2.6 KiB
Python
import logging
|
|
|
|
from fastapi import APIRouter, Depends
|
|
from sqlalchemy import func, select
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.core.rbac import get_user_permission_names, require_permission
|
|
from app.db.database import get_db
|
|
from app.models.rbac import Role
|
|
from app.models.user import User
|
|
from app.repositories.customer_repository import CustomerRepository
|
|
from app.repositories.user_repository import UserRepository
|
|
from app.schemas.dashboard import DashboardSummary, EmptyWidget, MetricCard
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
router = APIRouter(
|
|
prefix="/dashboard",
|
|
tags=["Dashboard"],
|
|
)
|
|
|
|
|
|
@router.get("/summary", response_model=DashboardSummary)
|
|
def get_dashboard_summary(
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(require_permission("dashboard.read")),
|
|
):
|
|
permissions = get_user_permission_names(current_user)
|
|
customers: list[MetricCard] = []
|
|
latest_customers = []
|
|
users: list[MetricCard] = []
|
|
roles: list[MetricCard] = []
|
|
|
|
if "customers.read" in permissions:
|
|
customers = [
|
|
MetricCard(label="Kunden", value=CustomerRepository.count_all(db)),
|
|
MetricCard(label="Aktive Kunden", value=CustomerRepository.count_active(db)),
|
|
MetricCard(label="Neue Kunden", value=CustomerRepository.count_recent(db)),
|
|
]
|
|
latest_customers = CustomerRepository.get_latest(db)
|
|
|
|
if "users.read" in permissions:
|
|
all_users = UserRepository.get_all(db)
|
|
users = [
|
|
MetricCard(label="Benutzer", value=len(all_users)),
|
|
MetricCard(label="Aktive Benutzer", value=len([user for user in all_users if user.is_active])),
|
|
]
|
|
|
|
if "roles.read" in permissions:
|
|
roles = [
|
|
MetricCard(
|
|
label="Rollen",
|
|
value=db.scalar(select(func.count(Role.id))) or 0,
|
|
)
|
|
]
|
|
|
|
logger.info("dashboard.summary", extra={"actor_user_id": current_user.id})
|
|
|
|
return DashboardSummary(
|
|
customers=customers,
|
|
latest_customers=latest_customers,
|
|
users=users,
|
|
roles=roles,
|
|
activities=EmptyWidget(
|
|
title="Letzte Aktivitäten",
|
|
message="Noch keine Aktivitäten vorhanden.",
|
|
),
|
|
tasks=EmptyWidget(
|
|
title="Offene Aufgaben",
|
|
message="Noch keine Aufgaben vorhanden.",
|
|
),
|
|
tickets=EmptyWidget(
|
|
title="Offene Tickets",
|
|
message="Noch keine Tickets vorhanden.",
|
|
),
|
|
projects=EmptyWidget(
|
|
title="Projekte",
|
|
message="Noch keine Projektdaten vorhanden.",
|
|
),
|
|
)
|