diff --git a/backend/src/main.py b/backend/src/main.py index c8c1f40..765cbcb 100644 --- a/backend/src/main.py +++ b/backend/src/main.py @@ -1,8 +1,9 @@ """Main app for backend api""" import logging -from fastapi import FastAPI, HTTPException, Query +import time from contextlib import asynccontextmanager +from fastapi import FastAPI, HTTPException, Query from .logging_config import configure_logging from .structs.landmark import Landmark, Toilets @@ -81,6 +82,7 @@ def new_trip(preferences: Preferences, must_do=True, n_tags=0) + start_time = time.time() # Generate the landmarks from the start location landmarks, landmarks_short = manager.generate_landmarks_list( center_coordinates = start, @@ -91,18 +93,34 @@ def new_trip(preferences: Preferences, landmarks_short.insert(0, start_landmark) landmarks_short.append(end_landmark) + t_generate_landmarks = time.time() - start_time + start_time = time.time() + # First stage optimization try: base_tour = optimizer.solve_optimization(preferences.max_time_minute, landmarks_short) except ArithmeticError as exc: - raise HTTPException(status_code=500, detail="No solution found") from exc + raise HTTPException(status_code=500) from exc except TimeoutError as exc: raise HTTPException(status_code=500, detail="Optimzation took too long") from exc + except Exception as exc: + raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(exc)}") from exc + t_first_stage = time.time() - start_time + start_time = time.time() # Second stage optimization - refined_tour = refiner.refine_optimization(landmarks, base_tour, + try : + refined_tour = refiner.refine_optimization(landmarks, base_tour, preferences.max_time_minute, preferences.detour_tolerance_minute) + except Exception as exc : + raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(exc)}") from exc + + t_second_stage = time.time() - start_time + logger.debug(f'Generating landmarks : {round(t_generate_landmarks,3)} seconds') + logger.debug(f'First stage optimization : {round(t_first_stage,3)} seconds') + logger.debug(f'Second stage optimization : {round(t_second_stage,3)} seconds') + logger.info(f'Total computation time : {round(t_generate_landmarks + t_first_stage + t_second_stage,3)} seconds') linked_tour = LinkedLandmarks(refined_tour) # upon creation of the trip, persistence of both the trip and its landmarks is ensured. @@ -165,7 +183,7 @@ def get_toilets(location: tuple[float, float] = Query(...), radius: int = 500) - raise HTTPException(status_code=406, detail="Coordinates not provided or invalid") if not (-90 <= location[0] <= 90 or -180 <= location[1] <= 180): raise HTTPException(status_code=422, detail="Start coordinates not in range") - + toilets_manager = ToiletsManager(location, radius) try :