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,4 +1,5 @@
import logging
from datetime import datetime
from bson import ObjectId
from fastapi import HTTPException
@@ -29,6 +30,41 @@ async def retrieve_flights(user: str = "", sort: str = "date", order: int = -1)
return flights
async def retrieve_totals(user: str, start_date: datetime = None, end_date: datetime = None) -> dict:
"""
Retrieve total times for the given user
:param user:
:return:
"""
match = {"user": ObjectId(user)}
if start_date is not None:
match.setdefault("date", {}).setdefault("$gte", start_date)
if end_date is not None:
match.setdefault("date", {}).setdefault("$lte", end_date)
cursor = flight_collection.aggregate([
{"$match": match},
{"$group": {
"_id": None,
"total_time": {"$sum": "$time_total"},
"total_solo": {"$sum": "$time_solo"},
"total_night": {"$sum": "$time_night"},
"total_pic": {"$sum": "$time_pic"},
"total_sic": {"$sum": "$time_sic"},
}
},
{"$project": {"_id": 0}},
])
result = await cursor.to_list(length=None)
if not result:
raise HTTPException(404, "No flights found")
return result[0]
async def retrieve_flight(id: str) -> FlightDisplaySchema:
"""
Get detailed information about the given flight