Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add schedule pre-caching #84

Merged
merged 1 commit into from
Jun 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/ferry_planner/route.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# ruff: noqa: DTZ007
from __future__ import annotations

import asyncio
import hashlib
from collections.abc import Generator, Iterable, Iterator, Sequence
from copy import deepcopy
Expand Down Expand Up @@ -254,12 +255,31 @@ def __init__(self, *, connection_db: ConnectionDB, schedule_getter: ScheduleGett
self._connection_db = connection_db
self._schedule_getter = schedule_getter

async def _pre_cache_schedules(
self,
*,
routes: Iterable[Route],
options: RoutePlansOptions,
) -> None:
connections: set[FerryConnection] = set()
for route in routes:
for i in range(1, len(route)):
origin = route[i - 1]
destination = route[i]
connection = self._connection_db.from_to_location(origin, destination)
if isinstance(connection, FerryConnection):
connections.add(connection)
tasks = [self._schedule_getter(c.origin.id, c.destination.id, date=options.date) for c in connections]
await asyncio.gather(*tasks)

async def make_route_plans(
self,
*,
routes: Iterable[Route],
options: RoutePlansOptions,
) -> Sequence[RoutePlan]:
routes = tuple(routes)
await self._pre_cache_schedules(routes=routes, options=options)
route_plans = []
for route in routes:
await self._add_plan_segment(
Expand Down
Loading