diff --git a/.env b/.env index 625a52d..7f2ba34 100644 --- a/.env +++ b/.env @@ -1,6 +1,7 @@ OEFFI_DB_NAME="oeffikator" OEFFI_DB_CONTAINER_NAME="oeffikator-db" OEFFI_APP_CONTAINER_NAME="oeffikator-app" +OEFFI_BVG_API_CONTAINER_NAME="oeffikator-bvg-api" OEFFI_MAX_EAST="13.55" OEFFI_MAX_WEST="13.2" diff --git a/CHANGELOG b/CHANGELOG index c1534d0..d85299c 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,10 +1,15 @@ # Changelog -## 1.2.4 +## 1.2.5 * Update dependencies (github actions, python version, aiohttp, numpy, geoalchemy, pylint, gunicorn) +## 1.2.4 + +* Replace BVG(/VBB)-API with local instance for more stability + + ## 1.2.3 * Update dependencies (fastapi, geoalchemy, pylint, pytz, requests, scipy, sqlalchemy) diff --git a/docker-compose.yml b/docker-compose.yml index b8717fb..c0ba831 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -11,6 +11,7 @@ services: environment: OEFFI_DB_NAME: ${OEFFI_DB_NAME} OEFFI_DB_CONTAINER_NAME: ${OEFFI_DB_CONTAINER_NAME} + OEFFI_BVG_API_CONTAINER_NAME: ${OEFFI_BVG_API_CONTAINER_NAME} OEFFI_MAX_WEST: ${OEFFI_MAX_WEST} OEFFI_MAX_EAST: ${OEFFI_MAX_EAST} OEFFI_MAX_SOUTH: ${OEFFI_MAX_SOUTH} @@ -18,6 +19,8 @@ services: depends_on: db: condition: service_healthy + bvg-api: + condition: service_healthy secrets: - oeffi_db_user - oeffi_db_pw @@ -74,6 +77,21 @@ services: app: condition: service_healthy + bvg-api: + image: derhuerst/bvg-rest:6 + ports: + - "3000:3000" + container_name: ${OEFFI_BVG_API_CONTAINER_NAME} + healthcheck: + test: + [ + "CMD-SHELL", + "echo 0" + ] + interval: 5s + timeout: 5s + retries: 3 + secrets: oeffi_db_pw: file: oeffikator_db_pw.txt diff --git a/oeffikator/__init__.py b/oeffikator/__init__.py index f405e46..d1b7996 100644 --- a/oeffikator/__init__.py +++ b/oeffikator/__init__.py @@ -16,8 +16,9 @@ BVG_V5_URL = "https://v5.bvg.transport.rest" BVG_V6_URL = "https://v6.bvg.transport.rest" VBB_V6_URL = "https://v6.vbb.transport.rest" +BVG_V6_URL_LOCAL = f"http://{settings.bvg_api_container_name}:3000" -REQUESTERS = [BVGRestRequester(url) for url in [BVG_V5_URL, BVG_V6_URL, VBB_V6_URL]] +REQUESTERS = [BVGRestRequester(url) for url in [BVG_V6_URL_LOCAL]] REQUESTERS = [requester for requester in REQUESTERS if requester.is_responding()] AUTHKEY = "" diff --git a/oeffikator/requests.py b/oeffikator/requests.py index eea8b0e..9e51264 100644 --- a/oeffikator/requests.py +++ b/oeffikator/requests.py @@ -11,6 +11,8 @@ from . import REQUESTERS, logger from .sql_app import crud, models, schemas +# pylint: disable-msg=W0511 + async def get_requester() -> RequesterInterface: """Simple function to get an available requester (=one which hasn't reached its request limit yet) @@ -94,8 +96,9 @@ async def request_trip( ) return trip arrivale_time = datetime.datetime.strptime(requested_trip["arrivalTime"], "%H%M%S").time() - arrivale_time = datetime.datetime.combine(TRAVELLING_DAYTIME.date(), arrivale_time) - duration = (arrivale_time - TRAVELLING_DAYTIME).total_seconds() / 60 # in minutes + # TODO replace this hacky fix of adding one hour to the output. Why does this return an hour of difference + arrivale_time = datetime.datetime.combine(TRAVELLING_DAYTIME.date(), arrivale_time) + datetime.timedelta(hours=1) + duration = int((arrivale_time - TRAVELLING_DAYTIME).total_seconds() / 60) # in minutes trip = schemas.TripCreate( duration=duration, diff --git a/oeffikator/settings.py b/oeffikator/settings.py index 806e4b0..86ece1d 100644 --- a/oeffikator/settings.py +++ b/oeffikator/settings.py @@ -10,6 +10,7 @@ class Settings(BaseSettings): db_name: str = "" db_user: str = "" db_pw: str = "" + bvg_api_container_name: str = "0.0.0.0" max_west: float = 13.2 max_east: float = 13.55 max_south: float = 52.42 diff --git a/pyproject.toml b/pyproject.toml index 2d6f231..023fd08 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "oeffikator" -version = "1.2.4" +version = "1.2.5" description = "A visualisation tool for commuting times on public transport" authors = ["Eric Kolibacz "] license = "GNU GPLv3" diff --git a/tests/docker-compose.yml b/tests/docker-compose.yml index 5c21613..36e2851 100644 --- a/tests/docker-compose.yml +++ b/tests/docker-compose.yml @@ -10,15 +10,17 @@ services: - 8001:8000 environment: OEFFI_DB_NAME: ${OEFFI_DB_NAME} - OEFFI_DB_CONTAINER_NAME: ${OEFFI_DB_CONTAINER_NAME} + OEFFI_DB_CONTAINER_NAME: test-db + OEFFI_BVG_API_CONTAINER_NAME: test-bvg-api depends_on: test-db: condition: service_healthy + test-bvg-api: + condition: service_healthy secrets: - oeffi_db_user - oeffi_db_pw - # TODO rename this to db only (container name will be oeffikator-db) - maybe for oeffikator container too? # TODO add build: dockerfile ? test-db: image: postgis/postgis @@ -33,7 +35,6 @@ services: - oeffi_db_pw volumes: - ../scripts/:/docker-entrypoint-initdb.d/ - container_name: ${OEFFI_DB_CONTAINER_NAME} healthcheck: test: [ @@ -43,6 +44,21 @@ services: interval: 5s timeout: 5s retries: 3 + + test-bvg-api: + image: derhuerst/bvg-rest:6 + ports: + - "3000:3000" + container_name: test-bvg-api + healthcheck: + test: + [ + "CMD-SHELL", + "echo 0" + ] + interval: 5s + timeout: 5s + retries: 3 secrets: oeffi_db_pw: diff --git a/tests/requesters_commons.py b/tests/requesters_commons.py index 6b04da6..3c9db3f 100644 --- a/tests/requesters_commons.py +++ b/tests/requesters_commons.py @@ -9,7 +9,7 @@ def is_alive(url: str) -> bool: """Tests if the requester queries the location properly""" coordinates_should_be = np.array([52.51627344417692, 13.37766793796735]) - if "bvg" in url or "vbb" in url: + if "bvg" in url or "vbb" in url or "127.0.0.1" in url: requester = BVGRestRequester(url) else: raise ValueError(f"The url {url} is not supported") diff --git a/tests/test_api.py b/tests/test_api.py index 3f40294..2a60634 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -64,7 +64,7 @@ def test_trip(): """Test whether the oeffikator gets the trip duration properly""" origin_description = LOCATION_1 destination_description = LOCATION_2 - expected_trip_duration = 15 # in minutes + expected_trip_duration = 14 # in minutes origin = Location(**client.get_location(origin_description).json()) destination = Location(**client.get_location(destination_description).json()) diff --git a/tests/test_requesters.py b/tests/test_requesters.py index a30f8a5..31c2e8a 100644 --- a/tests/test_requesters.py +++ b/tests/test_requesters.py @@ -14,7 +14,9 @@ BVG_V5_URL = "https://v5.bvg.transport.rest" BVG_V6_URL = "https://v6.bvg.transport.rest" VBB_V6_URL = "https://v6.vbb.transport.rest" -AVAILABLE_URLS = [url for url in [BVG_V5_URL, BVG_V6_URL, VBB_V6_URL] if is_alive(url)] +BVG_V6_URL_LOCAL = "http://127.0.0.1:3000" + +AVAILABLE_URLS = [url for url in [BVG_V6_URL_LOCAL] if is_alive(url)] URL = random.choice(AVAILABLE_URLS) if AVAILABLE_URLS else None @@ -73,14 +75,14 @@ def test_get_journey_for_bvg_requester(): This will fail as soon as if there are constructions on this line!""" if URL is None: pytest.skip("No Requester is alive") - time_should_be = (10, 11) # apparently, bvg apis return slightly different values for different versions + time_should_be = (10, 11, 12) # apparently, bvg apis return slightly different values for different versions requester = BVGRestRequester(URL) origin = asyncio.run(requester.query_location("10178 Berlin-Mitte, Alexanderplatz 1")) destination = asyncio.run(requester.query_location("10557 Berlin-Moabit, Europaplatz 1")) journey = asyncio.run(requester.get_journey(origin=origin, destination=destination, start_date=TRAVELLING_DAYTIME)) - time_is = (int(journey["arrivalTime"]) - (TRAVELLING_DAYTIME.hour * 10000)) / 100 + time_is = (int(journey["arrivalTime"]) - ((TRAVELLING_DAYTIME.hour - 1) * 10000)) / 100 assert time_is in time_should_be