Skip to content

Commit

Permalink
Merge pull request #3996 from hove-io/update_ouestgo_call_params
Browse files Browse the repository at this point in the history
Add two params while calling OuestGo
  • Loading branch information
Krishna Prasad ADHIKARI authored May 9, 2023
2 parents ce50723 + 36d8e7d commit 7f1153a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 42 deletions.
59 changes: 20 additions & 39 deletions source/jormungandr/jormungandr/scenarios/ridesharing/ouestgo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, DATE_FORMAT
from jormungandr.timezone import get_timezone_or_paris

DEFAULT_OUESTGO_FEED_PUBLISHER = {
'id': 'OUESTGO',
Expand Down Expand Up @@ -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 []

Expand All @@ -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.dropoff_place = rsj.Place(addr='', lat=dropoff_lat, lon=dropoff_lon)
res.origin_pickup_distance = 0
res.origin_pickup_duration = 0

# 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
Expand Down Expand Up @@ -189,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'
Expand All @@ -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_timezone_or_paris() # using coverage's TZ (or Paris) for mindate and maxdate
params = {
'apikey': self.api_key,
'p[passenger][state]': self.passenger_state,
Expand All @@ -211,6 +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=DATE_FORMAT
),
'p[outward][maxdate]': timestamp_to_date_str(
period_extremity.datetime + ONE_DAY, timezone, _format=DATE_FORMAT
),
}

headers = {'Authentication': 'key={}'.format(self.api_key)}
Expand All @@ -226,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, instance_params
)
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',
Expand Down
7 changes: 7 additions & 0 deletions source/jormungandr/jormungandr/timezone.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_timezone_or_paris():
try:
return get_timezone()
except Exception as _:
return pytz.timezone('Europe/Paris')
13 changes: 13 additions & 0 deletions source/jormungandr/jormungandr/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions source/jormungandr/tests/ouestgo_routing_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,16 +189,16 @@ 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')
assert len(rsj_sections) == 3

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'
Expand Down

0 comments on commit 7f1153a

Please sign in to comment.