From 74698d3865e2c30dd5a1a7bb47c81fd30bc51e42 Mon Sep 17 00:00:00 2001 From: kadhikari Date: Fri, 5 May 2023 11:46:31 +0200 Subject: [PATCH 1/4] Add two params while calling OuestGo --- .../scenarios/ridesharing/ouestgo.py | 47 +++++-------------- source/jormungandr/jormungandr/timezone.py | 7 +++ source/jormungandr/jormungandr/utils.py | 13 +++++ .../tests/ouestgo_routing_tests.py | 6 +-- 4 files changed, 36 insertions(+), 37 deletions(-) diff --git a/source/jormungandr/jormungandr/scenarios/ridesharing/ouestgo.py b/source/jormungandr/jormungandr/scenarios/ridesharing/ouestgo.py index 4bc4254a5f..e355444d9b 100644 --- a/source/jormungandr/jormungandr/scenarios/ridesharing/ouestgo.py +++ b/source/jormungandr/jormungandr/scenarios/ridesharing/ouestgo.py @@ -39,8 +39,8 @@ RsFeedPublisher, RidesharingServiceError, ) -from jormungandr.utils import Coords, get_weekday, make_timestamp_from_str -from jormungandr.street_network.utils import crowfly_distance_between, get_manhattan_duration +from jormungandr.utils import get_weekday, make_timestamp_from_str, timestamp_to_date_str, ONE_DAY +from jormungandr.timezone import get_default_timezone DEFAULT_OUESTGO_FEED_PUBLISHER = { 'id': 'OUESTGO', @@ -123,42 +123,19 @@ def _make_response(self, raw_json, request_datetime, from_coord, to_coord, insta res.ridesharing_ad = offer.get('journeys', {}).get('url') res.duration = offer.get('journeys', {}).get('duration') - # coord of departure on foot to arrive at ride-sharing point + # ride-sharing pick up point is the same as departure lat, lon = from_coord.split(',') - departure_coord = Coords(lat=lat, lon=lon) - - # ride-sharing pick up coord - pickup_lat = float(offer.get('journeys', {}).get('from', {}).get('latitude')) - pickup_lon = float(offer.get('journeys', {}).get('from', {}).get('longitude')) - pickup_coord = Coords(lat=pickup_lat, lon=pickup_lon) - - res.pickup_place = rsj.Place(addr='', lat=pickup_lat, lon=pickup_lon) - + res.pickup_place = rsj.Place(addr='', lat=float(lat), lon=float(lon)) res.origin_pickup_shape = None # Not specified - res.origin_pickup_distance = int(crowfly_distance_between(departure_coord, pickup_coord)) - # we choose to calculate with speed=1.12 the average speed for a walker - res.origin_pickup_duration = get_manhattan_duration( - res.origin_pickup_distance, instance_params.walking_speed - ) - - # ride-sharing drop off coord - dropoff_lat = float(offer.get('journeys', {}).get('to', {}).get('latitude')) - dropoff_lon = float(offer.get('journeys', {}).get('to', {}).get('longitude')) - dropoff_coord = Coords(lat=dropoff_lat, lon=dropoff_lon) + res.origin_pickup_distance = 0 + res.origin_pickup_duration = 0 - res.dropoff_place = rsj.Place(addr='', lat=dropoff_lat, lon=dropoff_lon) - - # arrival coord to final destination or any mode of transport + # ride-sharing drop off point is same as arrival to final destination or any mode of transport lat, lon = to_coord.split(',') - arrival_coord = Coords(lat=lat, lon=lon) - + res.dropoff_place = rsj.Place(addr='', lat=float(lat), lon=float(lon)) res.dropoff_dest_shape = None # Not specified - res.dropoff_dest_distance = int(crowfly_distance_between(dropoff_coord, arrival_coord)) - # we choose to calculate with speed=1.12 the average speed for a walker - res.dropoff_dest_duration = get_manhattan_duration( - res.dropoff_dest_distance, instance_params.walking_speed - ) - + res.dropoff_dest_distance = 0 + res.dropoff_dest_duration = 0 res.shape = None res.price = float(offer.get('journeys', {}).get('cost', {}).get('variable')) * 100.0 @@ -200,7 +177,7 @@ def _request_journeys(self, from_coord, to_coord, period_extremity, instance_par """ dep_lat, dep_lon = from_coord.split(',') arr_lat, arr_lon = to_coord.split(',') - + timezone = get_default_timezone() params = { 'apikey': self.api_key, 'p[passenger][state]': self.passenger_state, @@ -211,6 +188,8 @@ def _request_journeys(self, from_coord, to_coord, period_extremity, instance_par 'p[to][longitude]': arr_lon, 'signature': 'toto', 'timestamp': period_extremity.datetime, + 'p[outward][mindate]': timestamp_to_date_str(period_extremity.datetime, timezone, _format="%Y-%m-%d"), + 'p[outward][maxdate]': timestamp_to_date_str(period_extremity.datetime + ONE_DAY, timezone, _format="%Y-%m-%d"), } headers = {'Authentication': 'key={}'.format(self.api_key)} diff --git a/source/jormungandr/jormungandr/timezone.py b/source/jormungandr/jormungandr/timezone.py index 8ead99f028..3225677bdc 100644 --- a/source/jormungandr/jormungandr/timezone.py +++ b/source/jormungandr/jormungandr/timezone.py @@ -81,3 +81,10 @@ def get_timezone(): if not hasattr(g, 'timezone'): raise TechnicalError("No timezone set for this API") # the set_request_timezone has to be called at init return g.timezone + + +def get_default_timezone(): + try: + return get_timezone() + except: + return pytz.timezone('Europe/Paris') diff --git a/source/jormungandr/jormungandr/utils.py b/source/jormungandr/jormungandr/utils.py index 70c483b364..e8c33cdb42 100644 --- a/source/jormungandr/jormungandr/utils.py +++ b/source/jormungandr/jormungandr/utils.py @@ -62,6 +62,8 @@ WEEK_DAYS_MAPPING = ("monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday") COVERAGE_ANY_BETA = "any-beta" BEST_BOARDING_POSITION_KEY = "{}-{}" +ONE_DAY = 86400 +DATE_FORMAT = "%Y-%m-%d" MAP_STRING_PTOBJECT_TYPE = { "stop_point": type_pb2.STOP_POINT, @@ -224,6 +226,17 @@ def timestamp_to_str(timestamp): return None +def dt_to_date_str(dt, _format=DATE_FORMAT): + return dt.strftime(_format) + + +def timestamp_to_date_str(timestamp, tz=None, _format=DATE_FORMAT): + dt = timestamp_to_datetime(timestamp, tz=tz) + if dt: + return dt_to_date_str(dt, _format=_format) + return None + + def walk_dict(tree, visitor): """ depth first search on a dict. diff --git a/source/jormungandr/tests/ouestgo_routing_tests.py b/source/jormungandr/tests/ouestgo_routing_tests.py index 6683344dc9..3380a7f929 100644 --- a/source/jormungandr/tests/ouestgo_routing_tests.py +++ b/source/jormungandr/tests/ouestgo_routing_tests.py @@ -189,7 +189,7 @@ def test_basic_ride_sharing(self): rs_journeys = sections[0].get('ridesharing_journeys') assert len(rs_journeys) == 1 assert rs_journeys[0].get('distances').get('ridesharing') == 18869 - assert rs_journeys[0].get('durations').get('walking') == 2 + assert rs_journeys[0].get('durations').get('walking') == 0 assert rs_journeys[0].get('durations').get('ridesharing') == 1301 assert 'ridesharing' in rs_journeys[0].get('tags') rsj_sections = rs_journeys[0].get('sections') @@ -197,8 +197,8 @@ def test_basic_ride_sharing(self): assert rsj_sections[0].get('type') == 'crow_fly' assert rsj_sections[0].get('mode') == 'walking' - assert rsj_sections[0].get('duration') == 2 - assert rsj_sections[0].get('departure_date_time') == '20120614T085458' + assert rsj_sections[0].get('duration') == 0 + assert rsj_sections[0].get('departure_date_time') == '20120614T085500' assert rsj_sections[0].get('arrival_date_time') == '20120614T085500' assert rsj_sections[1].get('type') == 'ridesharing' From dc1d4e482278b3145c6395f60a13234c44291d54 Mon Sep 17 00:00:00 2001 From: kadhikari Date: Fri, 5 May 2023 11:54:51 +0200 Subject: [PATCH 2/4] Formatting --- .../jormungandr/scenarios/ridesharing/ouestgo.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/source/jormungandr/jormungandr/scenarios/ridesharing/ouestgo.py b/source/jormungandr/jormungandr/scenarios/ridesharing/ouestgo.py index e355444d9b..ed0a7fe379 100644 --- a/source/jormungandr/jormungandr/scenarios/ridesharing/ouestgo.py +++ b/source/jormungandr/jormungandr/scenarios/ridesharing/ouestgo.py @@ -188,8 +188,12 @@ def _request_journeys(self, from_coord, to_coord, period_extremity, instance_par 'p[to][longitude]': arr_lon, 'signature': 'toto', 'timestamp': period_extremity.datetime, - 'p[outward][mindate]': timestamp_to_date_str(period_extremity.datetime, timezone, _format="%Y-%m-%d"), - 'p[outward][maxdate]': timestamp_to_date_str(period_extremity.datetime + ONE_DAY, timezone, _format="%Y-%m-%d"), + 'p[outward][mindate]': timestamp_to_date_str( + period_extremity.datetime, timezone, _format="%Y-%m-%d" + ), + 'p[outward][maxdate]': timestamp_to_date_str( + period_extremity.datetime + ONE_DAY, timezone, _format="%Y-%m-%d" + ), } headers = {'Authentication': 'key={}'.format(self.api_key)} From 93f8448fbfd595d560f11d9ce29db428396d5788 Mon Sep 17 00:00:00 2001 From: kadhikari Date: Fri, 5 May 2023 16:35:40 +0200 Subject: [PATCH 3/4] Some minor modifications on remarks --- .../jormungandr/scenarios/ridesharing/ouestgo.py | 16 ++++++++-------- source/jormungandr/jormungandr/timezone.py | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/source/jormungandr/jormungandr/scenarios/ridesharing/ouestgo.py b/source/jormungandr/jormungandr/scenarios/ridesharing/ouestgo.py index ed0a7fe379..296623f132 100644 --- a/source/jormungandr/jormungandr/scenarios/ridesharing/ouestgo.py +++ b/source/jormungandr/jormungandr/scenarios/ridesharing/ouestgo.py @@ -39,8 +39,8 @@ RsFeedPublisher, RidesharingServiceError, ) -from jormungandr.utils import get_weekday, make_timestamp_from_str, timestamp_to_date_str, ONE_DAY -from jormungandr.timezone import get_default_timezone +from jormungandr.utils import get_weekday, make_timestamp_from_str, timestamp_to_date_str, ONE_DAY, DATE_FORMAT +from jormungandr.timezone import get_timezone_or_paris DEFAULT_OUESTGO_FEED_PUBLISHER = { 'id': 'OUESTGO', @@ -96,7 +96,7 @@ def status(self): 'network': self.network, } - def _make_response(self, raw_json, request_datetime, from_coord, to_coord, instance_params): + def _make_response(self, raw_json, request_datetime, from_coord, to_coord): if not raw_json: return [] @@ -166,7 +166,7 @@ def _make_response(self, raw_json, request_datetime, from_coord, to_coord, insta return ridesharing_journeys - def _request_journeys(self, from_coord, to_coord, period_extremity, instance_params, limit=None): + def _request_journeys(self, from_coord, to_coord, period_extremity, instance_params=None, limit=None): """ :param from_coord: lat,lon ex: '48.109377,-1.682103' @@ -177,7 +177,7 @@ def _request_journeys(self, from_coord, to_coord, period_extremity, instance_par """ dep_lat, dep_lon = from_coord.split(',') arr_lat, arr_lon = to_coord.split(',') - timezone = get_default_timezone() + timezone = get_timezone_or_paris() # using coverage's TZ (or Paris) for mindate and maxdate params = { 'apikey': self.api_key, 'p[passenger][state]': self.passenger_state, @@ -189,10 +189,10 @@ def _request_journeys(self, from_coord, to_coord, period_extremity, instance_par 'signature': 'toto', 'timestamp': period_extremity.datetime, 'p[outward][mindate]': timestamp_to_date_str( - period_extremity.datetime, timezone, _format="%Y-%m-%d" + period_extremity.datetime, timezone, _format=DATE_FORMAT ), 'p[outward][maxdate]': timestamp_to_date_str( - period_extremity.datetime + ONE_DAY, timezone, _format="%Y-%m-%d" + period_extremity.datetime + ONE_DAY, timezone, _format=DATE_FORMAT ), } @@ -210,7 +210,7 @@ def _request_journeys(self, from_coord, to_coord, period_extremity, instance_par if resp: r = self._make_response( - resp.json(), period_extremity.datetime, from_coord, to_coord, instance_params + resp.json(), period_extremity.datetime, from_coord, to_coord ) self.record_additional_info('Received ridesharing offers', nb_ridesharing_offers=len(r)) logging.getLogger('stat.ridesharing.ouestgo').info( diff --git a/source/jormungandr/jormungandr/timezone.py b/source/jormungandr/jormungandr/timezone.py index 3225677bdc..6e2bc40756 100644 --- a/source/jormungandr/jormungandr/timezone.py +++ b/source/jormungandr/jormungandr/timezone.py @@ -83,8 +83,8 @@ def get_timezone(): return g.timezone -def get_default_timezone(): +def get_timezone_or_paris(): try: return get_timezone() - except: + except Exception as _: return pytz.timezone('Europe/Paris') From 36d8e7db0d24ca4a7f8b540857cb05cf04efe34e Mon Sep 17 00:00:00 2001 From: kadhikari Date: Fri, 5 May 2023 16:40:28 +0200 Subject: [PATCH 4/4] pre-commit --- .../jormungandr/jormungandr/scenarios/ridesharing/ouestgo.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/source/jormungandr/jormungandr/scenarios/ridesharing/ouestgo.py b/source/jormungandr/jormungandr/scenarios/ridesharing/ouestgo.py index 296623f132..b3b5ee540f 100644 --- a/source/jormungandr/jormungandr/scenarios/ridesharing/ouestgo.py +++ b/source/jormungandr/jormungandr/scenarios/ridesharing/ouestgo.py @@ -209,9 +209,7 @@ def _request_journeys(self, from_coord, to_coord, period_extremity, instance_par raise RidesharingServiceError('non 200 response', resp.status_code, resp.reason, resp.text) if resp: - r = self._make_response( - resp.json(), period_extremity.datetime, from_coord, to_coord - ) + r = self._make_response(resp.json(), period_extremity.datetime, from_coord, to_coord) self.record_additional_info('Received ridesharing offers', nb_ridesharing_offers=len(r)) logging.getLogger('stat.ridesharing.ouestgo').info( 'Received ridesharing offers : %s',