import datetime from typing import Optional, List from fastapi import APIRouter, HTTPException from pydantic import BaseModel import api_database router = APIRouter(prefix="/dashboard/{station_id}", tags=["dashboard", "local"]) def validate_daterange(start_date: datetime.date, end_date: datetime.date): days_requested = (end_date - start_date).days if days_requested < 0: raise HTTPException(status_code=400, detail="Requested date-range is negative") class StationDashboard(BaseModel): id: Optional[int] commonName: Optional[str] lat: Optional[float] lon: Optional[float] maxEndDate: Optional[datetime.date] maxStartDate: Optional[datetime.date] @router.get("/", response_model=StationDashboard) def get_general_dashboard(station_id: int): return api_database.get_dashboard(station_id)[0] class StationDashboardTopStationsEntry(BaseModel): startStationName: str endStationName: str number: int avgDuration: int @router.get("/to", response_model=List[StationDashboardTopStationsEntry]) def get_to_dashboard_for_station(station_id: int, start_date: datetime.date, end_date: datetime.date): validate_daterange(start_date, end_date) return api_database.get_dashboard_to(station_id, start_date, end_date) @router.get("/from", response_model=List[StationDashboardTopStationsEntry]) def get_from_dashboard_for_station(station_id: int, start_date: datetime.date, end_date: datetime.date): validate_daterange(start_date, end_date) return api_database.get_dashboard_from(station_id, start_date, end_date) class StationDashboardDurationGroup(BaseModel): number: int minutesGroup: str @router.get("/duration", response_model=List[StationDashboardDurationGroup]) def get_duration_dashboard_for_station(station_id: int, start_date: datetime.date, end_date: datetime.date): validate_daterange(start_date, end_date) return api_database.get_dashboard_duration(station_id, start_date, end_date) class StationDashboardTimeGroup(BaseModel): timeFrame: str number: int avgDuration: int @router.get("/time", response_model=List[StationDashboardTimeGroup]) def get_time_dashboard_for_station(station_id: int, start_date: datetime.date, end_date: datetime.date): validate_daterange(start_date, end_date) return api_database.get_dashboard_time(station_id, start_date, end_date)