Абалдеть, почти получилось, сейвлю, чтобы не вдруг не профукать

This commit is contained in:
maksim 2024-06-07 23:29:55 +04:00
parent 03d5173d70
commit 8b92541188
3 changed files with 41 additions and 21 deletions

View File

@ -185,7 +185,6 @@ def genetic_algorithm(start, end, graph, flights_data, type, departure_date, pop
for flight in flights_data:
if flight['departurePoint'] == path[i] and flight['destinationPoint'] == path[i + 1]:
path_data.append({
"id": flight['id'],
"departurePoint": flight['departurePoint'],
"destinationPoint": flight['destinationPoint']
})
@ -198,8 +197,6 @@ def genetic_algorithm(start, end, graph, flights_data, type, departure_date, pop
continue
result.append({
type: path_data,
"start_time": start_time.isoformat() if start_time else None,
"end_time": end_time.isoformat() if end_time else None,
})
return result

View File

@ -1,7 +1,7 @@
from fastapi import APIRouter, HTTPException
from genetic_algorithm.genetic_algorithm import genetic_algorithm, load_graph_from_request
from schemas import TripRequest
from schemas import TripRequest, FlightSegment, TripResult, TripOption
router = APIRouter(
prefix="/flight",
@ -9,23 +9,35 @@ router = APIRouter(
)
@router.post("/get/")
@router.post("/get/", response_model=TripResult)
async def get_flight(request: TripRequest):
graphTo, graphBack, start_point, end_point, flightsDataTo, flightsDataBack, departure_date, departure_date_return = load_graph_from_request(request)
graphTo, graphBack, start_point, end_point, flightsDataTo, flightsDataBack, departure_date, departure_date_return = load_graph_from_request(
request)
if request.returnDate:
resultTo = genetic_algorithm(start=start_point, end=end_point, graph=graphTo, flights_data=flightsDataTo, type = "to", departure_date = departure_date)
resulFrom = genetic_algorithm(end=start_point, start=end_point, graph=graphBack, flights_data=flightsDataBack, type = "back", departure_date = departure_date_return)
if not resultTo or not resulFrom:
raise HTTPException(status_code=404, detail="No valid paths found")
return resultTo, resulFrom
if not request.returnDate:
resultTo = genetic_algorithm(start=start_point, end=end_point, graph=graphTo, flights_data=flightsDataTo,
type="to", departure_date = departure_date)
if resultTo:
resultTo.append({
"back": []
})
else:
type="to", departure_date=departure_date)
resultFrom = genetic_algorithm(start=end_point, end=start_point, graph=graphBack, flights_data=flightsDataBack,
type="back", departure_date=departure_date_return)
if not resultTo or not resultFrom:
raise HTTPException(status_code=404, detail="No valid paths found")
return resultTo
trip_options = []
for to_trip in resultTo:
for back_trip in resultFrom:
to_segments = [FlightSegment(**segment) for segment in to_trip["to"]]
back_segments = [FlightSegment(**segment) for segment in back_trip["back"]]
trip_options.append(TripOption(to=to_segments, back=back_segments))
return TripResult(trips=trip_options)
else:
resultTo = genetic_algorithm(start=start_point, end=end_point, graph=graphTo, flights_data=flightsDataTo,
type="to", departure_date=departure_date)
if not resultTo:
raise HTTPException(status_code=404, detail="No valid paths found")
trip_options = [TripOption(to=[FlightSegment(**segment) for segment in trip["to"]], back=[]) for trip in
resultTo]
return TripResult(trips=trip_options)

View File

@ -43,4 +43,15 @@ class TripRequest(BaseModel):
class Review(BaseModel):
city: str
address: str
name_ru: str
name_ru: str
class FlightSegment(BaseModel):
departurePoint: str
destinationPoint: str
class TripOption(BaseModel):
to: List[FlightSegment]
back: List[FlightSegment]
class TripResult(BaseModel):
trips: List[TripOption]