Add simple statistics endpoint

This commit is contained in:
april
2024-01-08 13:58:10 -06:00
parent cabae55677
commit 9c2eb7ea0e
2 changed files with 56 additions and 0 deletions

View File

@@ -1,5 +1,6 @@
import json
import logging
from datetime import datetime
from typing import Dict, Union, List
from fastapi import APIRouter, HTTPException, Depends
@@ -53,6 +54,25 @@ async def get_flights_by_date(user: UserDisplaySchema = Depends(get_current_user
return flights_ordered
@router.get('/totals', summary="Get total statistics for the current user", status_code=200, response_model=dict)
async def get_flight_totals(user: UserDisplaySchema = Depends(get_current_user), start_date: str = "",
end_date: str = "") -> dict:
"""
Get the total statistics for the currently logged-in user
:param user: Current user
:param start_date: Only count statistics after this date (optional)
:param end_date: Only count statistics before this date (optional)
:return: Dict of totals
"""
try:
start = datetime.strptime(start_date, "%Y-%m-%d") if start_date != "" else None
end = datetime.strptime(end_date, "%Y-%m-%d") if end_date != "" else None
except (TypeError, ValueError):
raise HTTPException(400, "Date range not processable")
return await db.retrieve_totals(user.id, start, end)
@router.get('/all', summary="Get all flights logged by all users", status_code=200,
dependencies=[Depends(admin_required)], response_model=list[FlightConciseSchema])
async def get_all_flights(sort: str = "date", order: int = -1) -> list[FlightConciseSchema]: