Skip to content

Commit

Permalink
Merge pull request #4321 from hove-io/fix_ridesharing_request_date
Browse files Browse the repository at this point in the history
[jormun]: Fix request_datetime in ridesharing
  • Loading branch information
kadhikari authored Dec 10, 2024
2 parents f050705 + dd86b1d commit e3587c7
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,12 @@ def _make_response(self, raw_json, request_datetime, from_coord, to_coord):

return ridesharing_journeys

def _request_journeys(self, from_coord, to_coord, period_extremity, instance, limit=None):
def _request_journeys(self, from_coord, to_coord, request_dates, instance, limit=None):
"""
:param from_coord: lat,lon ex: '48.109377,-1.682103'
:param to_coord: lat,lon ex: '48.020335,-1.743929'
:param period_extremity: a tuple of [timestamp(utc), clockwise]
:param request_dates: a tuple of [timestamp(utc), timestamp(utc), clockwise]
:param limit: optional
:return:
"""
Expand All @@ -190,18 +190,18 @@ def _request_journeys(self, from_coord, to_coord, period_extremity, instance, li
dt = datetime.datetime.now()
now = calendar.timegm(dt.utctimetuple())

if period_extremity.datetime < now + MIN_BLABLALINES_MARGIN_DEPARTURE_TIME:
if request_dates.departure_datetime < now + MIN_BLABLALINES_MARGIN_DEPARTURE_TIME:
logging.getLogger(__name__).info(
'blablalines ridesharing request departure time < now + 15 min. Force to now + 15 min'
)
departure_epoch = now + MIN_BLABLALINES_MARGIN_DEPARTURE_TIME
elif period_extremity.datetime > now + MAX_BLABLALINES_MARGIN_DEPARTURE_TIME:
elif request_dates.departure_datetime > now + MAX_BLABLALINES_MARGIN_DEPARTURE_TIME:
logging.getLogger(__name__).error(
'Blablalines error, request departure time should be between now to 1 week from now. departure is greater than now + 1 week'
)
return []
else:
departure_epoch = period_extremity.datetime
departure_epoch = request_dates.departure_datetime

# Paramaeters documenation : https://www.blablalines.com/public-api-v2
params = {
Expand All @@ -225,7 +225,7 @@ def _request_journeys(self, from_coord, to_coord, period_extremity, instance, li
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(), request_dates.departure_datetime, from_coord, to_coord)
self.record_additional_info('Received ridesharing offers', nb_ridesharing_offers=len(r))
logging.getLogger('stat.ridesharing.blablalines').info(
'Received ridesharing offers : %s',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,30 +230,33 @@ def _make_response(self, raw_json, from_coord, to_coord, instance_params):

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, request_dates, instance_params, limit=None):
"""
:param from_coord: lat,lon ex: '48.109377,-1.682103'
:param to_coord: lat,lon ex: '48.020335,-1.743929'
:param period_extremity: a tuple of [timestamp(utc), clockwise]
:param request_dates: a tuple of [timestamp(utc), timestamp(utc), clockwise]
:param limit: optional
:return:
"""
# format of datetime: 2017-12-25T07:00:00Z
datetime_str = datetime.datetime.fromtimestamp(period_extremity.datetime, pytz.utc).strftime(
'%Y-%m-%dT%H:%M:%SZ'
)
if period_extremity.represents_start:
if request_dates.represents_start:
# format of datetime: 2017-12-25T07:00:00Z
datetime_str = datetime.datetime.fromtimestamp(request_dates.departure_datetime, pytz.utc).strftime(
'%Y-%m-%dT%H:%M:%SZ'
)
datetime_str = '{}/PT{}S'.format(datetime_str, self.timeframe_duration)
else:
datetime_str = datetime.datetime.fromtimestamp(request_dates.arrival_datetime, pytz.utc).strftime(
'%Y-%m-%dT%H:%M:%SZ'
)
datetime_str = 'PT{}S/{}'.format(self.timeframe_duration, datetime_str)

params = {
'from': from_coord,
'to': to_coord,
'fromRadius': self.crowfly_radius,
'toRadius': self.crowfly_radius,
('arrivalDate', 'departureDate')[bool(period_extremity.represents_start)]: datetime_str,
('arrivalDate', 'departureDate')[bool(request_dates.represents_start)]: datetime_str,
}

if limit is not None:
Expand Down
6 changes: 3 additions & 3 deletions source/jormungandr/jormungandr/scenarios/ridesharing/karos.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,12 @@ def _make_response(self, raw_json):

return ridesharing_journeys

def _request_journeys(self, from_coord, to_coord, period_extremity, instance, limit=None):
def _request_journeys(self, from_coord, to_coord, request_dates, instance, limit=None):
"""
:param from_coord: lat,lon ex: '48.109377,-1.682103'
:param to_coord: lat,lon ex: '48.020335,-1.743929'
:param period_extremity: a tuple of [timestamp(utc), clockwise]
:param request_dates: a tuple of [timestamp(utc), timestamp(utc), clockwise]
:param limit: optional
:return:
"""
Expand All @@ -195,7 +195,7 @@ def _request_journeys(self, from_coord, to_coord, period_extremity, instance, li
'departureLng': dep_lon,
'arrivalLat': arr_lat,
'arrivalLng': arr_lon,
'date': period_extremity.datetime,
'date': request_dates.departure_datetime,
'timeDelta': self.timedelta,
'departureRadius': self.departure_radius,
'arrivalRadius': self.arrival_radius,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,12 @@ def _make_response(self, raw_json):

return ridesharing_journeys

def _request_journeys(self, from_coord, to_coord, period_extremity, instance, limit=None):
def _request_journeys(self, from_coord, to_coord, request_dates, instance, limit=None):
"""
:param from_coord: lat,lon ex: '48.109377,-1.682103'
:param to_coord: lat,lon ex: '48.020335,-1.743929'
:param period_extremity: a tuple of [timestamp(utc), clockwise]
:param request_dates: a tuple of [timestamp(utc), timestamp(utc), clockwise]
:param limit: optional
:return:
"""
Expand All @@ -182,7 +182,7 @@ def _request_journeys(self, from_coord, to_coord, period_extremity, instance, li
'departureLng': dep_lon,
'arrivalLat': arr_lat,
'arrivalLng': arr_lon,
'date': period_extremity.datetime,
'date': request_dates.departure_datetime,
'timeDelta': self.timedelta,
'departureRadius': 2,
'arrivalRadius': 2,
Expand Down
14 changes: 8 additions & 6 deletions source/jormungandr/jormungandr/scenarios/ridesharing/ouestgo.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,12 @@ def _make_response(self, raw_json, request_datetime, from_coord, to_coord, timez

return ridesharing_journeys

def _request_journeys(self, from_coord, to_coord, period_extremity, instance_params=None, limit=None):
def _request_journeys(self, from_coord, to_coord, request_dates, instance_params=None, limit=None):
"""
:param from_coord: lat,lon ex: '48.109377,-1.682103'
:param to_coord: lat,lon ex: '48.020335,-1.743929'
:param period_extremity: a tuple of [timestamp(utc), clockwise]
:param request_dates: a tuple of [timestamp(utc), timestamp(utc), clockwise]
:param limit: optional
:return:
"""
Expand All @@ -204,12 +204,12 @@ def _request_journeys(self, from_coord, to_coord, period_extremity, instance_par
'p[to][latitude]': arr_lat,
'p[to][longitude]': arr_lon,
'signature': 'toto',
'timestamp': period_extremity.datetime,
'timestamp': request_dates.departure_datetime,
'p[outward][mindate]': timestamp_to_date_str(
period_extremity.datetime, timezone, _format=DATE_FORMAT
request_dates.departure_datetime, timezone, _format=DATE_FORMAT
),
'p[outward][maxdate]': timestamp_to_date_str(
period_extremity.datetime, timezone, _format=DATE_FORMAT
request_dates.departure_datetime, timezone, _format=DATE_FORMAT
),
}

Expand All @@ -230,7 +230,9 @@ def _request_journeys(self, from_coord, to_coord, period_extremity, instance_par
# EVEN THOUGH the timezone is previously set in g.
# We'd better retrieve the timezone from instance_params than relying on the g
timezone = get_timezone_or_paris() if instance_params is None else instance_params.timezone
r = self._make_response(resp.json(), period_extremity.datetime, from_coord, to_coord, timezone)
r = self._make_response(
resp.json(), request_dates.departure_datetime, from_coord, to_coord, timezone
)
self.record_additional_info('Received ridesharing offers', nb_ridesharing_offers=len(r))
logging.getLogger('stat.ridesharing.ouestgo').info(
'Received ridesharing offers : %s',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,15 @@ def _call_service(self, params, headers={}, verify=True):
raise RidesharingServiceError(str(e))

def request_journeys_with_feed_publisher(
self, from_coord, to_coord, period_extremity, instance_params, limit=None
self, from_coord, to_coord, request_dates, instance_params, limit=None
):
"""
This function shouldn't be overwritten!
:return: a list(mandatory) contains solutions and a feed_publisher
"""
try:
journeys = self._request_journeys(from_coord, to_coord, period_extremity, instance_params, limit)
journeys = self._request_journeys(from_coord, to_coord, request_dates, instance_params, limit)
feed_publisher = self._get_feed_publisher()

self.record_call('ok')
Expand All @@ -132,7 +132,7 @@ def request_journeys_with_feed_publisher(
return [], None

@abc.abstractmethod
def _request_journeys(self, from_coord, to_coord, period_extremity, instance_params, limit=None):
def _request_journeys(self, from_coord, to_coord, request_dates, instance_params, limit=None):
"""
:return: a list(mandatory) contains solutions
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
from jormungandr.utils import get_pt_object_coord, generate_id
from jormungandr.street_network.utils import crowfly_distance_between
from navitiacommon import response_pb2
from jormungandr.utils import PeriodExtremity
from jormungandr.utils import RequestDates
from jormungandr.scenarios.journey_filter import to_be_deleted
from jormungandr.scenarios.helper_classes.helper_future import FutureManager
from importlib import import_module
Expand Down Expand Up @@ -198,27 +198,29 @@ def decorate_journeys_with_ridesharing_offers(self, response, request, instance)
for section_idx, section in enumerate(journey.sections):
if section.street_network.mode == response_pb2.Ridesharing:
section.additional_informations.append(response_pb2.HAS_DATETIME_ESTIMATED)
period_extremity = None
request_dates = None
if len(journey.sections) == 1: # direct path, we use the user input
period_extremity = PeriodExtremity(request['datetime'], request['clockwise'])
request_dates = RequestDates(
request['datetime'], request['datetime'], request['clockwise']
)
elif (
section_idx == 0
): # ridesharing on first section we want to arrive before the start of the pt
period_extremity = PeriodExtremity(section.end_date_time, False)
request_dates = RequestDates(section.begin_date_time, section.end_date_time, False)
else: # ridesharing at the end, we search for solution starting after the end of the pt sections
period_extremity = PeriodExtremity(section.begin_date_time, True)
request_dates = RequestDates(section.begin_date_time, section.end_date_time, True)
instance_params = self.InstanceParams.make_params(instance)
if greenlet_pool_actived:
futures[journey_idx][section_idx] = future_manager.create_future(
self.build_ridesharing_journeys,
section.origin,
section.destination,
period_extremity,
request_dates,
instance_params,
)
else:
pb_rsjs, pb_tickets, pb_fps = self.build_ridesharing_journeys(
section.origin, section.destination, period_extremity, instance_params
section.origin, section.destination, request_dates, instance_params
)
self.add_new_ridesharing_results(
pb_rsjs, pb_tickets, pb_fps, response, journey_idx, section_idx
Expand All @@ -242,7 +244,7 @@ def add_new_ridesharing_results(self, pb_rsjs, pb_tickets, pb_fps, response, jou
response.feed_publishers.extend((fp for fp in pb_fps if fp not in response.feed_publishers))

def get_ridesharing_journeys_with_feed_publishers(
self, from_coord, to_coord, period_extremity, instance_params, limit=None
self, from_coord, to_coord, request_dates, instance_params, limit=None
):
calls = []
res = []
Expand All @@ -252,7 +254,7 @@ def get_ridesharing_journeys_with_feed_publishers(

def _call(s=service):
return s.request_journeys_with_feed_publisher(
from_coord, to_coord, period_extremity, instance_params, limit
from_coord, to_coord, request_dates, instance_params, limit
)

calls.append(_call)
Expand All @@ -271,14 +273,14 @@ def _call(s=service):

return res, fps

def build_ridesharing_journeys(self, from_pt_obj, to_pt_obj, period_extremity, instance_params):
def build_ridesharing_journeys(self, from_pt_obj, to_pt_obj, request_dates, instance_params):
from_coord = get_pt_object_coord(from_pt_obj)
to_coord = get_pt_object_coord(to_pt_obj)
from_str = "{},{}".format(from_coord.lat, from_coord.lon)
to_str = "{},{}".format(to_coord.lat, to_coord.lon)
try:
rsjs, fps = self.get_ridesharing_journeys_with_feed_publishers(
from_str, to_str, period_extremity, instance_params
from_str, to_str, request_dates, instance_params
)
except Exception as e:
self.logger.exception(
Expand All @@ -304,7 +306,7 @@ def build_ridesharing_journeys(self, from_pt_obj, to_pt_obj, period_extremity, i
pickup_coord = get_pt_object_coord(pb_rsj_pickup)
dropoff_coord = get_pt_object_coord(pb_rsj_dropoff)

pb_rsj.requested_date_time = period_extremity.datetime
pb_rsj.requested_date_time = request_dates.departure_datetime
if rsj.departure_date_time:
pb_rsj.departure_date_time = rsj.departure_date_time
if rsj.arrival_date_time:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,15 @@ def blablalines_test():
from_coord = '47.28696,0.78981'
to_coord = '47.38642,0.69039'

period_extremity = utils.PeriodExtremity(
datetime=utils.str_to_time_stamp("20171225T060000"), represents_start=True
request_dates = utils.RequestDates(
departure_datetime=utils.str_to_time_stamp("20171225T060000"),
arrival_datetime=utils.str_to_time_stamp("20171225T060000"),
represents_start=True,
)
ridesharing_journeys, feed_publisher = blablalines.request_journeys_with_feed_publisher(
from_coord=from_coord,
to_coord=to_coord,
period_extremity=period_extremity,
request_dates=request_dates,
instance_params=DummyInstance(),
)

Expand Down Expand Up @@ -215,8 +217,10 @@ def test_request_journeys_should_raise_on_non_200():
blablalines._request_journeys(
'1.2,3.4',
'5.6,7.8',
utils.PeriodExtremity(
datetime=utils.str_to_time_stamp("20171225T060000"), represents_start=True
utils.RequestDates(
departure_datetime=utils.str_to_time_stamp("20171225T060000"),
arrival_datetime=utils.str_to_time_stamp("20171225T060000"),
represents_start=True,
),
DummyInstance(),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,13 +250,14 @@ def instant_system_test():
from_coord = '48.109377,-1.682103'
to_coord = '48.020335,-1.743929'

period_extremity = utils.PeriodExtremity(
datetime=utils.str_to_time_stamp("20171225T060000"), represents_start=True
request_dates = utils.RequestDates(
departure_datetime=utils.str_to_time_stamp("20171225T060000"),
arrival_datetime=utils.str_to_time_stamp("20171225T060000"),
represents_start=True,
)

params = RidesharingServiceManager.InstanceParams.make_params(DummyInstance())
ridesharing_journeys, feed_publisher = instant_system.request_journeys_with_feed_publisher(
from_coord=from_coord, to_coord=to_coord, period_extremity=period_extremity, instance_params=params
from_coord=from_coord, to_coord=to_coord, request_dates=request_dates, instance_params=params
)

assert len(ridesharing_journeys) == 2
Expand Down Expand Up @@ -347,8 +348,10 @@ def test_request_journeys_should_raise_on_non_200():
instant_system._request_journeys(
'1.2,3.4',
'5.6,7.8',
utils.PeriodExtremity(
datetime=utils.str_to_time_stamp("20171225T060000"), represents_start=True
utils.RequestDates(
departure_datetime=utils.str_to_time_stamp("20171225T060000"),
arrival_datetime=utils.str_to_time_stamp("20171225T060000"),
represents_start=True,
),
DummyInstance(),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,14 @@ def karos_service_test():
from_coord = '47.28696,0.78981'
to_coord = '47.38642,0.69039'

period_extremity = utils.PeriodExtremity(
datetime=utils.str_to_time_stamp("20171225T060000"), represents_start=True
request_dates = utils.RequestDates(
departure_datetime=utils.str_to_time_stamp("20171225T060000"),
arrival_datetime=utils.str_to_time_stamp("20171225T060000"),
represents_start=True,
)
params = RidesharingServiceManager.InstanceParams.make_params(DummyInstance)
ridesharing_journeys, feed_publisher = karos.request_journeys_with_feed_publisher(
from_coord=from_coord, to_coord=to_coord, period_extremity=period_extremity, instance_params=params
from_coord=from_coord, to_coord=to_coord, request_dates=request_dates, instance_params=params
)

assert len(ridesharing_journeys) == 2
Expand Down Expand Up @@ -334,8 +336,10 @@ def test_request_journeys_should_raise_on_non_200():
karos._request_journeys(
'1.2,3.4',
'5.6,7.8',
utils.PeriodExtremity(
datetime=utils.str_to_time_stamp("20171225T060000"), represents_start=True
utils.RequestDates(
departure_datetime=utils.str_to_time_stamp("20171225T060000"),
arrival_datetime=utils.str_to_time_stamp("20171225T060000"),
represents_start=True,
),
DummyInstance(),
)
Expand Down
Loading

0 comments on commit e3587c7

Please sign in to comment.