Compute passage delay

This commit is contained in:
Gabriel Augendre 2021-12-04 15:57:16 +01:00
parent 6d649dae8c
commit 81b2b8961e
4 changed files with 51 additions and 2 deletions

18
main.py
View file

@ -1,5 +1,6 @@
import collections import collections
import re import re
from datetime import datetime
from typing import Dict, List, Optional, Tuple from typing import Dict, List, Optional, Tuple
import httpx import httpx
@ -83,7 +84,9 @@ async def stop(
"[A-Z]$", "", ligne "[A-Z]$", "", ligne
) # Remove letter suffix to group by commercial line name ) # Remove letter suffix to group by commercial line name
destination = passage.get("idtarretdestination") destination = passage.get("idtarretdestination")
passages[(ligne, destination)].append(passage.get("delaipassage")) heure_passage = passage.get("heurepassage")
delai = get_delai(heure_passage)
passages[(ligne, destination)].append(delai)
stop_ids.add(destination) stop_ids.add(destination)
if not passages: if not passages:
@ -104,3 +107,16 @@ async def stop(
) )
return Passages(passages=passages_list, stop=stop_infos.get(monitored_stop_id)) return Passages(passages=passages_list, stop=stop_infos.get(monitored_stop_id))
def get_delai(heure_passage: str) -> str:
dt = datetime.strptime(heure_passage, "%Y-%m-%d %H:%M:%S")
now = datetime.now()
delai = dt - now
minutes = delai.seconds // 60
seconds = delai.seconds % 60
if seconds > 30:
minutes += 1
if minutes <= 0:
return "Proche"
return f"{minutes} min"

32
poetry.lock generated
View file

@ -180,6 +180,17 @@ python-versions = ">=3.6"
docs = ["furo (>=2021.8.17b43)", "sphinx (>=4.1)", "sphinx-autodoc-typehints (>=1.12)"] docs = ["furo (>=2021.8.17b43)", "sphinx (>=4.1)", "sphinx-autodoc-typehints (>=1.12)"]
testing = ["covdefaults (>=1.2.0)", "coverage (>=4)", "pytest (>=4)", "pytest-cov", "pytest-timeout (>=1.4.2)"] testing = ["covdefaults (>=1.2.0)", "coverage (>=4)", "pytest (>=4)", "pytest-cov", "pytest-timeout (>=1.4.2)"]
[[package]]
name = "freezegun"
version = "1.1.0"
description = "Let your Python tests travel through time"
category = "dev"
optional = false
python-versions = ">=3.5"
[package.dependencies]
python-dateutil = ">=2.7"
[[package]] [[package]]
name = "h11" name = "h11"
version = "0.12.0" version = "0.12.0"
@ -431,6 +442,17 @@ python-versions = "*"
pytest = ">=3.6.0" pytest = ">=3.6.0"
vcrpy = "*" vcrpy = "*"
[[package]]
name = "python-dateutil"
version = "2.8.2"
description = "Extensions to the standard Python datetime module"
category = "dev"
optional = false
python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,>=2.7"
[package.dependencies]
six = ">=1.5"
[[package]] [[package]]
name = "python-dotenv" name = "python-dotenv"
version = "0.19.1" version = "0.19.1"
@ -668,7 +690,7 @@ multidict = ">=4.0"
[metadata] [metadata]
lock-version = "1.1" lock-version = "1.1"
python-versions = "^3.9" python-versions = "^3.9"
content-hash = "d131772b18f7dd30b32986b2c02a2805a552285b9c1c83073263347b89640e12" content-hash = "8c17e2b88379ea9c4a8bacf465dac27c59fe1719eb9f1cbb6ab2ca16b013aa89"
[metadata.files] [metadata.files]
anyio = [ anyio = [
@ -731,6 +753,10 @@ filelock = [
{file = "filelock-3.3.2-py3-none-any.whl", hash = "sha256:bb2a1c717df74c48a2d00ed625e5a66f8572a3a30baacb7657add1d7bac4097b"}, {file = "filelock-3.3.2-py3-none-any.whl", hash = "sha256:bb2a1c717df74c48a2d00ed625e5a66f8572a3a30baacb7657add1d7bac4097b"},
{file = "filelock-3.3.2.tar.gz", hash = "sha256:7afc856f74fa7006a289fd10fa840e1eebd8bbff6bffb69c26c54a0512ea8cf8"}, {file = "filelock-3.3.2.tar.gz", hash = "sha256:7afc856f74fa7006a289fd10fa840e1eebd8bbff6bffb69c26c54a0512ea8cf8"},
] ]
freezegun = [
{file = "freezegun-1.1.0-py2.py3-none-any.whl", hash = "sha256:2ae695f7eb96c62529f03a038461afe3c692db3465e215355e1bb4b0ab408712"},
{file = "freezegun-1.1.0.tar.gz", hash = "sha256:177f9dd59861d871e27a484c3332f35a6e3f5d14626f2bf91be37891f18927f3"},
]
h11 = [ h11 = [
{file = "h11-0.12.0-py3-none-any.whl", hash = "sha256:36a3cb8c0a032f56e2da7084577878a035d3b61d104230d4bd49c0c6b555a9c6"}, {file = "h11-0.12.0-py3-none-any.whl", hash = "sha256:36a3cb8c0a032f56e2da7084577878a035d3b61d104230d4bd49c0c6b555a9c6"},
{file = "h11-0.12.0.tar.gz", hash = "sha256:47222cb6067e4a307d535814917cd98fd0a57b6788ce715755fa2b6c28b56042"}, {file = "h11-0.12.0.tar.gz", hash = "sha256:47222cb6067e4a307d535814917cd98fd0a57b6788ce715755fa2b6c28b56042"},
@ -975,6 +1001,10 @@ pytest-vcr = [
{file = "pytest-vcr-1.0.2.tar.gz", hash = "sha256:23ee51b75abbcc43d926272773aae4f39f93aceb75ed56852d0bf618f92e1896"}, {file = "pytest-vcr-1.0.2.tar.gz", hash = "sha256:23ee51b75abbcc43d926272773aae4f39f93aceb75ed56852d0bf618f92e1896"},
{file = "pytest_vcr-1.0.2-py2.py3-none-any.whl", hash = "sha256:2f316e0539399bea0296e8b8401145c62b6f85e9066af7e57b6151481b0d6d9c"}, {file = "pytest_vcr-1.0.2-py2.py3-none-any.whl", hash = "sha256:2f316e0539399bea0296e8b8401145c62b6f85e9066af7e57b6151481b0d6d9c"},
] ]
python-dateutil = [
{file = "python-dateutil-2.8.2.tar.gz", hash = "sha256:0123cacc1627ae19ddf3c27a5de5bd67ee4586fbdd6440d9748f8abb483d3e86"},
{file = "python_dateutil-2.8.2-py2.py3-none-any.whl", hash = "sha256:961d03dc3453ebbc59dbdea9e4e11c5651520a876d0f4db161e8674aae935da9"},
]
python-dotenv = [ python-dotenv = [
{file = "python-dotenv-0.19.1.tar.gz", hash = "sha256:14f8185cc8d494662683e6914addcb7e95374771e707601dfc70166946b4c4b8"}, {file = "python-dotenv-0.19.1.tar.gz", hash = "sha256:14f8185cc8d494662683e6914addcb7e95374771e707601dfc70166946b4c4b8"},
{file = "python_dotenv-0.19.1-py2.py3-none-any.whl", hash = "sha256:bbd3da593fc49c249397cbfbcc449cf36cb02e75afc8157fcc6a81df6fb7750a"}, {file = "python_dotenv-0.19.1-py2.py3-none-any.whl", hash = "sha256:bbd3da593fc49c249397cbfbcc449cf36cb02e75afc8157fcc6a81df6fb7750a"},

View file

@ -13,6 +13,7 @@ httpx = "^0.20.0"
pre-commit = "^2.15.0" pre-commit = "^2.15.0"
pytest = "^6.2.5" pytest = "^6.2.5"
pytest-vcr = "^1.0.2" pytest-vcr = "^1.0.2"
freezegun = "^1.1.0"
[build-system] [build-system]
requires = ["poetry-core>=1.0.0"] requires = ["poetry-core>=1.0.0"]

View file

@ -1,3 +1,4 @@
import freezegun as freezegun
import pytest import pytest
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
@ -7,6 +8,7 @@ client = TestClient(app)
@pytest.mark.vcr() @pytest.mark.vcr()
@freezegun.freeze_time("2021-11-14 11:47:34")
def test_get_stop(): def test_get_stop():
response = client.get("/stop/290", headers={"Authorization": "Basic token"}) response = client.get("/stop/290", headers={"Authorization": "Basic token"})
assert response.status_code == 200 assert response.status_code == 200