Skip to content

Commit 7f1153a

Browse files
author
Krishna Prasad ADHIKARI
authored
Merge pull request #3996 from hove-io/update_ouestgo_call_params
Add two params while calling OuestGo
2 parents ce50723 + 36d8e7d commit 7f1153a

File tree

4 files changed

+43
-42
lines changed

4 files changed

+43
-42
lines changed

source/jormungandr/jormungandr/scenarios/ridesharing/ouestgo.py

+20-39
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
RsFeedPublisher,
4040
RidesharingServiceError,
4141
)
42-
from jormungandr.utils import Coords, get_weekday, make_timestamp_from_str
43-
from jormungandr.street_network.utils import crowfly_distance_between, get_manhattan_duration
42+
from jormungandr.utils import get_weekday, make_timestamp_from_str, timestamp_to_date_str, ONE_DAY, DATE_FORMAT
43+
from jormungandr.timezone import get_timezone_or_paris
4444

4545
DEFAULT_OUESTGO_FEED_PUBLISHER = {
4646
'id': 'OUESTGO',
@@ -96,7 +96,7 @@ def status(self):
9696
'network': self.network,
9797
}
9898

99-
def _make_response(self, raw_json, request_datetime, from_coord, to_coord, instance_params):
99+
def _make_response(self, raw_json, request_datetime, from_coord, to_coord):
100100
if not raw_json:
101101
return []
102102

@@ -123,42 +123,19 @@ def _make_response(self, raw_json, request_datetime, from_coord, to_coord, insta
123123
res.ridesharing_ad = offer.get('journeys', {}).get('url')
124124
res.duration = offer.get('journeys', {}).get('duration')
125125

126-
# coord of departure on foot to arrive at ride-sharing point
126+
# ride-sharing pick up point is the same as departure
127127
lat, lon = from_coord.split(',')
128-
departure_coord = Coords(lat=lat, lon=lon)
129-
130-
# ride-sharing pick up coord
131-
pickup_lat = float(offer.get('journeys', {}).get('from', {}).get('latitude'))
132-
pickup_lon = float(offer.get('journeys', {}).get('from', {}).get('longitude'))
133-
pickup_coord = Coords(lat=pickup_lat, lon=pickup_lon)
134-
135-
res.pickup_place = rsj.Place(addr='', lat=pickup_lat, lon=pickup_lon)
136-
128+
res.pickup_place = rsj.Place(addr='', lat=float(lat), lon=float(lon))
137129
res.origin_pickup_shape = None # Not specified
138-
res.origin_pickup_distance = int(crowfly_distance_between(departure_coord, pickup_coord))
139-
# we choose to calculate with speed=1.12 the average speed for a walker
140-
res.origin_pickup_duration = get_manhattan_duration(
141-
res.origin_pickup_distance, instance_params.walking_speed
142-
)
143-
144-
# ride-sharing drop off coord
145-
dropoff_lat = float(offer.get('journeys', {}).get('to', {}).get('latitude'))
146-
dropoff_lon = float(offer.get('journeys', {}).get('to', {}).get('longitude'))
147-
dropoff_coord = Coords(lat=dropoff_lat, lon=dropoff_lon)
148-
149-
res.dropoff_place = rsj.Place(addr='', lat=dropoff_lat, lon=dropoff_lon)
130+
res.origin_pickup_distance = 0
131+
res.origin_pickup_duration = 0
150132

151-
# arrival coord to final destination or any mode of transport
133+
# ride-sharing drop off point is same as arrival to final destination or any mode of transport
152134
lat, lon = to_coord.split(',')
153-
arrival_coord = Coords(lat=lat, lon=lon)
154-
135+
res.dropoff_place = rsj.Place(addr='', lat=float(lat), lon=float(lon))
155136
res.dropoff_dest_shape = None # Not specified
156-
res.dropoff_dest_distance = int(crowfly_distance_between(dropoff_coord, arrival_coord))
157-
# we choose to calculate with speed=1.12 the average speed for a walker
158-
res.dropoff_dest_duration = get_manhattan_duration(
159-
res.dropoff_dest_distance, instance_params.walking_speed
160-
)
161-
137+
res.dropoff_dest_distance = 0
138+
res.dropoff_dest_duration = 0
162139
res.shape = None
163140

164141
res.price = float(offer.get('journeys', {}).get('cost', {}).get('variable')) * 100.0
@@ -189,7 +166,7 @@ def _make_response(self, raw_json, request_datetime, from_coord, to_coord, insta
189166

190167
return ridesharing_journeys
191168

192-
def _request_journeys(self, from_coord, to_coord, period_extremity, instance_params, limit=None):
169+
def _request_journeys(self, from_coord, to_coord, period_extremity, instance_params=None, limit=None):
193170
"""
194171
195172
:param from_coord: lat,lon ex: '48.109377,-1.682103'
@@ -200,7 +177,7 @@ def _request_journeys(self, from_coord, to_coord, period_extremity, instance_par
200177
"""
201178
dep_lat, dep_lon = from_coord.split(',')
202179
arr_lat, arr_lon = to_coord.split(',')
203-
180+
timezone = get_timezone_or_paris() # using coverage's TZ (or Paris) for mindate and maxdate
204181
params = {
205182
'apikey': self.api_key,
206183
'p[passenger][state]': self.passenger_state,
@@ -211,6 +188,12 @@ def _request_journeys(self, from_coord, to_coord, period_extremity, instance_par
211188
'p[to][longitude]': arr_lon,
212189
'signature': 'toto',
213190
'timestamp': period_extremity.datetime,
191+
'p[outward][mindate]': timestamp_to_date_str(
192+
period_extremity.datetime, timezone, _format=DATE_FORMAT
193+
),
194+
'p[outward][maxdate]': timestamp_to_date_str(
195+
period_extremity.datetime + ONE_DAY, timezone, _format=DATE_FORMAT
196+
),
214197
}
215198

216199
headers = {'Authentication': 'key={}'.format(self.api_key)}
@@ -226,9 +209,7 @@ def _request_journeys(self, from_coord, to_coord, period_extremity, instance_par
226209
raise RidesharingServiceError('non 200 response', resp.status_code, resp.reason, resp.text)
227210

228211
if resp:
229-
r = self._make_response(
230-
resp.json(), period_extremity.datetime, from_coord, to_coord, instance_params
231-
)
212+
r = self._make_response(resp.json(), period_extremity.datetime, from_coord, to_coord)
232213
self.record_additional_info('Received ridesharing offers', nb_ridesharing_offers=len(r))
233214
logging.getLogger('stat.ridesharing.ouestgo').info(
234215
'Received ridesharing offers : %s',

source/jormungandr/jormungandr/timezone.py

+7
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,10 @@ def get_timezone():
8181
if not hasattr(g, 'timezone'):
8282
raise TechnicalError("No timezone set for this API") # the set_request_timezone has to be called at init
8383
return g.timezone
84+
85+
86+
def get_timezone_or_paris():
87+
try:
88+
return get_timezone()
89+
except Exception as _:
90+
return pytz.timezone('Europe/Paris')

source/jormungandr/jormungandr/utils.py

+13
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
WEEK_DAYS_MAPPING = ("monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday")
6363
COVERAGE_ANY_BETA = "any-beta"
6464
BEST_BOARDING_POSITION_KEY = "{}-{}"
65+
ONE_DAY = 86400
66+
DATE_FORMAT = "%Y-%m-%d"
6567

6668
MAP_STRING_PTOBJECT_TYPE = {
6769
"stop_point": type_pb2.STOP_POINT,
@@ -224,6 +226,17 @@ def timestamp_to_str(timestamp):
224226
return None
225227

226228

229+
def dt_to_date_str(dt, _format=DATE_FORMAT):
230+
return dt.strftime(_format)
231+
232+
233+
def timestamp_to_date_str(timestamp, tz=None, _format=DATE_FORMAT):
234+
dt = timestamp_to_datetime(timestamp, tz=tz)
235+
if dt:
236+
return dt_to_date_str(dt, _format=_format)
237+
return None
238+
239+
227240
def walk_dict(tree, visitor):
228241
"""
229242
depth first search on a dict.

source/jormungandr/tests/ouestgo_routing_tests.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -189,16 +189,16 @@ def test_basic_ride_sharing(self):
189189
rs_journeys = sections[0].get('ridesharing_journeys')
190190
assert len(rs_journeys) == 1
191191
assert rs_journeys[0].get('distances').get('ridesharing') == 18869
192-
assert rs_journeys[0].get('durations').get('walking') == 2
192+
assert rs_journeys[0].get('durations').get('walking') == 0
193193
assert rs_journeys[0].get('durations').get('ridesharing') == 1301
194194
assert 'ridesharing' in rs_journeys[0].get('tags')
195195
rsj_sections = rs_journeys[0].get('sections')
196196
assert len(rsj_sections) == 3
197197

198198
assert rsj_sections[0].get('type') == 'crow_fly'
199199
assert rsj_sections[0].get('mode') == 'walking'
200-
assert rsj_sections[0].get('duration') == 2
201-
assert rsj_sections[0].get('departure_date_time') == '20120614T085458'
200+
assert rsj_sections[0].get('duration') == 0
201+
assert rsj_sections[0].get('departure_date_time') == '20120614T085500'
202202
assert rsj_sections[0].get('arrival_date_time') == '20120614T085500'
203203

204204
assert rsj_sections[1].get('type') == 'ridesharing'

0 commit comments

Comments
 (0)