Skip to content

Commit

Permalink
change way of doing things
Browse files Browse the repository at this point in the history
  • Loading branch information
princefr committed Nov 28, 2024
1 parent a936795 commit aa31fba
Show file tree
Hide file tree
Showing 21 changed files with 179 additions and 271 deletions.
4 changes: 2 additions & 2 deletions source/jormungandr/jormungandr/default_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
} # 1000ms

# path of the configuration file for each instances
INSTANCES_DIR = os.getenv('JORMUNGANDR_INSTANCES_DIR', '/etc/jormungandr.d')
INSTANCES_DIR = os.getenv('JORMUNGANDR_INSTANCES_DIR', './jormung_conf')


INSTANCES_TIMEOUT = float(os.getenv('JORMUNGANDR_INSTANCES_TIMEOUT_S', 10))
Expand All @@ -32,7 +32,7 @@
'JORMUNGANDR_SQLALCHEMY_DATABASE_URI', 'postgresql://navitia:navitia@localhost/jormungandr'
)

DISABLE_DATABASE = boolean(os.getenv('JORMUNGANDR_DISABLE_DATABASE', False))
DISABLE_DATABASE = boolean(os.getenv('JORMUNGANDR_DISABLE_DATABASE', True))

# Active the asynchronous ridesharing mode
ASYNCHRONOUS_RIDESHARING = boolean(os.getenv('JORMUNGANDR_ASYNCHRONOUS_RIDESHARING', False))
Expand Down
6 changes: 3 additions & 3 deletions source/jormungandr/jormungandr/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -820,9 +820,7 @@ def additional_parameters(self):
additional_time_after_first_section_taxi = _make_property_getter('additional_time_after_first_section_taxi')
additional_time_before_last_section_taxi = _make_property_getter('additional_time_before_last_section_taxi')

additional_time_after_first_section_bike = _make_property_getter('additional_time_after_first_section_bike')
additional_time_before_last_section_bike = _make_property_getter('additional_time_before_last_section_bike')

on_street_bike_parking_duration = _make_property_getter('on_street_bike_parking_duration')

max_walking_direct_path_duration = _make_property_getter('max_walking_direct_path_duration')
max_bike_direct_path_duration = _make_property_getter('max_bike_direct_path_duration')
Expand Down Expand Up @@ -1041,6 +1039,7 @@ def _get_street_network(self, mode, request):
:param request: This parameter in required only for file configuration.
:return: street_network backend connector for the mode
"""

if app.config[str('DISABLE_DATABASE')]:
return self._streetnetwork_backend_manager.get_street_network_legacy(self, mode, request)
else:
Expand All @@ -1061,6 +1060,7 @@ def get_street_network(self, mode, request):
if mode != fallback_modes.FallbackModes.car.name:
return self._get_street_network(mode, request)


walking_service = self._get_street_network(fallback_modes.FallbackModes.walking.name, request)
car_service = self._get_street_network(fallback_modes.FallbackModes.car.name, request)
return street_network.CarWithPark(
Expand Down
28 changes: 14 additions & 14 deletions source/jormungandr/jormungandr/interfaces/v1/Journeys.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@
from copy import deepcopy
from jormungandr.travelers_profile import TravelerProfile
from navitiacommon.constants import ENUM_LANGUAGE
import urllib.parse
import base64


Expand Down Expand Up @@ -143,6 +142,9 @@ def wrapper(*args, **kwargs):
args['from'] = journey['from']['id']
args['rel'] = 'journeys'
journey['links'] = [create_external_link('v1.journeys', **args)]



elif allowed_ids and 'public_transport' in (s['type'] for s in journey['sections']):
# exactly one first_section_mode
if any(s['type'].startswith('bss') for s in journey['sections'][:2]):
Expand Down Expand Up @@ -436,15 +438,15 @@ def wrapper(*args, **kwargs):
for j in response.get('journeys', []):
if 'sections' not in j:
continue
logging.debug(
'for journey changing origin: {old_o} to {new_o}'
', destination to {old_d} to {new_d}'.format(
old_o=j.get('sections', [{}])[0].get('from').get('id'),
new_o=(g.origin_detail or {}).get('id'),
old_d=j.get('sections', [{}])[-1].get('to').get('id'),
new_d=(g.destination_detail or {}).get('id'),
)
)
# logging.debug(
# 'for journey changing origin: {old_o} to {new_o}'
# ', destination to {old_d} to {new_d}'.format(
# old_o=j.get('sections', [{}])[0].get('from').get('id'),
# new_o=(g.origin_detail or {}).get('id'),
# old_d=j.get('sections', [{}])[-1].get('to').get('id'),
# new_d=(g.destination_detail or {}).get('id'),
# )
# )
if g.origin_detail:
self.clean_global_origin_destination_detail(g.origin_detail)
j['sections'][0]['from'] = g.origin_detail
Expand Down Expand Up @@ -826,10 +828,8 @@ def _set_specific_params(mod):
if args.get('additional_time_before_last_section_taxi') is None:
args['additional_time_before_last_section_taxi'] = mod.additional_time_before_last_section_taxi

if args.get('additional_time_after_first_section_bike') is None:
args['additional_time_after_first_section_bike'] = mod.additional_time_after_first_section_bike
if args.get('additional_time_before_last_section_bike') is None:
args['additional_time_before_last_section_bike'] = mod.additional_time_before_last_section_bike
if args.get("on_street_bike_parking_duration") is None:
args["on_street_bike_parking_duration"] = mod.on_street_bike_parking_duration

if args.get('_stop_points_nearby_duration') is None:
args['_stop_points_nearby_duration'] = mod.stop_points_nearby_duration
Expand Down
24 changes: 14 additions & 10 deletions source/jormungandr/jormungandr/interfaces/v1/journey_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
# www.navitia.io

from __future__ import absolute_import, print_function, unicode_literals, division
from jormungandr import i_manager, fallback_modes, partner_services, app, park_modes
from jormungandr import i_manager, fallback_modes, park_modes, partner_services, app
from jormungandr.interfaces.v1.ResourceUri import ResourceUri
from datetime import datetime
from jormungandr.resources_utils import ResourceUtc
Expand Down Expand Up @@ -232,6 +232,17 @@ def __init__(self, output_type_serializer):
'first_section_mode[]=bike&last_section_mode[]=walking&'
'last_section_mode[]=bss&last_section_mode[]=bike`',
)

parser_get.add_argument(
"park_mode[]",
type=OptionValue(park_modes.all_park_modes),
dest="park_mode",
action="append",
help='If you want to use park modes in your journey. '
'Note: the park_modes[] concern only the bike objects. '
)


parser_get.add_argument(
"last_section_mode[]",
type=OptionValue(fallback_modes.all_fallback_modes),
Expand Down Expand Up @@ -515,17 +526,10 @@ def __init__(self, output_type_serializer):
help="the additional time added to the taxi section, right before riding the taxi but after hopping off the public transit",
)


parser_get.add_argument(
"additional_time_after_first_section_bike",
type=int,
help="the additional time added to the bike section, right after riding the bike but before hopping on the public transit",
)

parser_get.add_argument(
"additional_time_before_last_section_bike",
"on_street_bike_parking_duration",
type=int,
help="the additional time added to the bike section, right before riding the bike but after hopping off the public transit",
help="the additional time added to the bike section before and after parking the bike",
)

parser_get.add_argument(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ class ParametersSerializer(serpy.Serializer):
max_extra_second_pass = Field(schema_type=int)
additional_time_after_first_section_taxi = Field(schema_type=int)
additional_time_before_last_section_taxi = Field(schema_type=int)
additional_time_after_first_section_bike = Field(schema_type=int)
additional_time_before_last_section_bike = Field(schema_type=int)
on_street_bike_parking_duration = Field(schema_type=int)
max_walking_direct_path_duration = Field(schema_type=int)
max_bike_direct_path_duration = Field(schema_type=int)
max_bss_direct_path_duration = Field(schema_type=int)
Expand Down
11 changes: 5 additions & 6 deletions source/jormungandr/jormungandr/park_modes.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@
from navitiacommon import response_pb2



class ParkModes(Enum):
none = response_pb2.None
without_park = response_pb2.WithoutPark
with_park = response_pb2.WithPark
class ParkMode(Enum):
none = response_pb2.NONE
on_street = response_pb2.OnStreet
park_and_ride = response_pb2.ParkAndRide


@classmethod
Expand Down Expand Up @@ -72,4 +71,4 @@ def get_allowed_combinations_str(cls):
return set(six.moves.map(lambda modes: (modes[0].name, modes[1].name), allowed_combinations_enum))


all_park_modes = ParkModes.modes_str()
all_park_modes = ParkMode.modes_str()
15 changes: 7 additions & 8 deletions source/jormungandr/jormungandr/scenarios/distributed.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ def finalise_journeys(self, future_manager, request, responses, context, instanc
for response in responses:
pt_journey_fare_pool.async_compute_fare(response, request_id)


wait_and_complete_pt_journey(
requested_orig_obj=context.requested_orig_obj,
requested_dest_obj=context.requested_dest_obj,
Expand All @@ -356,6 +357,8 @@ def finalise_journeys(self, future_manager, request, responses, context, instanc
journeys=journeys_to_complete,
request_id="{}_complete_pt_journey".format(request_id),
)


if request['_loki_compute_pt_journey_fare'] is True and request['_pt_planner'] == "loki":
wait_and_complete_pt_journey_fare(
pt_elements=journeys_to_complete, pt_journey_fare_pool=pt_journey_fare_pool
Expand Down Expand Up @@ -589,15 +592,11 @@ def graphical_isochrones(self, request, instance):
'additional_time_before_last_section_taxi'
] = instance.additional_time_after_first_section_taxi

if request.get('additional_time_after_first_section_bike') is None:
request[
'additional_time_after_first_section_bike'
] = instance.additional_time_after_first_section_bike

if request.get('additional_time_before_last_section_bike') is None:
if request.get('on_street_bike_parking_duration') is None:
request[
'additional_time_before_last_section_bike'
] = instance.additional_time_before_last_section_bike
'on_street_bike_parking_duration'
] = instance.on_street_bike_parking_duration


krakens_call = set({(request["origin_mode"][0], request["destination_mode"][0], "indifferent")})
pt_object_origin = None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,31 @@ def _make_ending_car_park_sections(
car_park_to_sp_section.duration = car_park_crowfly_duration


def _extend_with_bike_park(begin_date_time, duration):
bike_park_section = response_pb2.Section()
bike_park_section.id = "Section_1"
bike_park_section.duration = duration
bike_park_section.type = response_pb2.PARK
bike_park_section.begin_date_time = begin_date_time
bike_park_section.end_date_time = bike_park_section.begin_date_time + duration
return bike_park_section


def _extend_with_bike_park_street_network(origin, begin_date_time, destination):
bike_park_to_sp_section = response_pb2.Section()
bike_park_to_sp_section.id = "Section_2"
bike_park_to_sp_section.origin.CopyFrom(origin)
bike_park_to_sp_section.destination.CopyFrom(destination)
bike_park_to_sp_section.type = response_pb2.STREET_NETWORK
bike_park_to_sp_section.street_network.mode = response_pb2.Walking
bike_park_to_sp_section.begin_date_time = begin_date_time
# bike_park_to_sp_section.end_date_time = begin_date_time + duration
return bike_park_to_sp_section





def _extend_with_car_park(
fallback_dp, pt_journey, fallback_type, walking_speed, car_park, car_park_duration, car_park_crowfly_duration
):
Expand Down Expand Up @@ -390,6 +415,55 @@ def add_poi_access_point_in_sections(fallback_type, via_poi_access, sections):
poi_access.is_exit = True
poi_access.is_entrance = True

def _update_journey(journey, park_section, street_mode_section, to_replace, new_fallbacks):
journey.duration += park_section.duration + street_mode_section.duration
journey.durations.total += park_section.duration + street_mode_section.duration
journey.arrival_date_time += park_section.duration + street_mode_section.duration
journey.sections.remove(to_replace)
journey.sections.extend([street_mode_section, park_section])
journey.sections.extend(new_fallbacks)
journey.nb_sections += 1

def _update_fallback_with_bike_mode(journey, fallback_dp, fallback_period_extremity, fallback_type, via_pt_access, via_poi_access,**kwargs):
"""
Replace journey's fallback sections with the given fallback_dp.
Note: the replacement is done in place of the journey
"""
aligned_fallback = _align_fallback_direct_path_datetime(fallback_dp, fallback_period_extremity)
fallback_sections = aligned_fallback.journeys[0].sections

# update the 'id' which isn't set
_rename_fallback_sections_ids(fallback_sections)

# We have to create the link between the fallback and the pt part manually here
if fallback_type == StreetNetworkPathType.BEGINNING_FALLBACK and kwargs["origin_mode"] == ["bike"]:
for s in journey.sections:
s.begin_date_time += kwargs["additional_time"]
s.end_date_time += kwargs["additional_time"]
park_section = _extend_with_bike_park(fallback_sections[-1].end_date_time, kwargs["additional_time"])
street_mode_section = _extend_with_bike_park_street_network(fallback_sections[-1].destination, park_section.end_date_time, journey.sections[0].destination)
_update_journey(journey, park_section, street_mode_section, journey.sections[0], fallback_sections)

elif fallback_type == StreetNetworkPathType.ENDING_FALLBACK and kwargs["destination_mode"] == ["bike"]:
street_mode_section = _extend_with_bike_park_street_network(fallback_sections[0].destination, fallback_sections[0].begin_date_time, journey.sections[-1].destination)
park_section = _extend_with_bike_park(street_mode_section.begin_date_time, kwargs["additional_time"])
fallback_sections[0].begin_date_time += kwargs["additional_time"]
_update_journey(journey, park_section, street_mode_section, journey.sections[-1], fallback_sections)


add_poi_access_point_in_sections(fallback_type, via_poi_access, fallback_sections)

if isinstance(via_pt_access, type_pb2.PtObject) and via_pt_access.embedded_type == type_pb2.ACCESS_POINT:
if fallback_type == StreetNetworkPathType.BEGINNING_FALLBACK:
journey.sections[-1].vias.add().CopyFrom(via_pt_access.access_point)
else:
journey.sections[0].vias.add().CopyFrom(via_pt_access.access_point)
journey.sections.sort(key=cmp_to_key(SectionSorter()))





def _update_fallback_sections(
journey, fallback_dp, fallback_period_extremity, fallback_type, via_pt_access, via_poi_access
Expand Down Expand Up @@ -638,14 +712,28 @@ def _build_fallback(
fallback_dp_copy, fallback_type, requested_obj, via_poi_access, language
)

_update_fallback_sections(
pt_journey,
fallback_dp_copy,
fallback_period_extremity,
fallback_type,
via_pt_access,
via_poi_access,
)
if request["park_mode"] == ["on_street"] and (request["origin_mode"] == ["bike"] or request["destination_mode"] == ["bike"]):
_update_fallback_with_bike_mode(
pt_journey,
fallback_dp_copy,
fallback_period_extremity,
fallback_type,
via_pt_access,
via_poi_access,
park_mode = request["park_mode"],
origin_mode = request["origin_mode"],
destination_mode = request["destination_mode"],
additional_time = request["on_street_bike_parking_duration"]
)
else:
_update_fallback_sections(
pt_journey,
fallback_dp_copy,
fallback_period_extremity,
fallback_type,
via_pt_access,
via_poi_access,
)

# update distances and durations by mode if it's a proper computed streetnetwork fallback
if fallback_dp_copy and fallback_dp_copy.journeys:
Expand Down Expand Up @@ -693,9 +781,13 @@ def get_max_fallback_duration(request, mode, dp_future, direct_path_timeout):
:return: max_fallback_duration
"""
# 30 minutes by default
print("request", request)
max_duration = request.get('max_{}_duration_to_pt'.format(mode), 1800)
print("max_duration", max_duration)
print(str(dp_future))
dp = dp_future.wait_and_get(timeout=direct_path_timeout) if dp_future else None
dp_duration = dp.journeys[0].durations.total if getattr(dp, 'journeys', None) else max_duration

return min(max_duration, dp_duration)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,6 @@ def _async_request(self):
self._futures.append(self._future_manager.create_future(self._do_request, origin, destination))

def wait_and_get(self, timeout=None):

# timeout=None -> wait forever...
timer = gevent.timeout.Timeout(timeout, exception=False)

Expand Down
6 changes: 6 additions & 0 deletions source/jormungandr/jormungandr/scenarios/journey_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
from jormungandr.scenarios.utils import compare, get_or_default
from navitiacommon import response_pb2
from jormungandr.utils import (
get_pt_object_from_json,
json_address_from_uri,
pb_del_if,
ComposedFilter,
portable_min,
Expand Down Expand Up @@ -693,6 +695,10 @@ def apply_final_journey_filters(response_list, instance, request):
filter_non_car_tagged_journey(journeys, request)






def is_direct_path_walking(j):
if not j:
return False
Expand Down
3 changes: 3 additions & 0 deletions source/jormungandr/jormungandr/scenarios/new_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -1449,6 +1449,9 @@ def fill_journeys(self, request_type, api_request, instance):
api_request, responses, distributed_context, instance, api_request['debug'], request_id
)




# We need to reapply some filter after 'finalise_journeys' because
# journeys with crow_fly bss and crow_fly walking couldn't be compared
# but can results in street_network walking in both case
Expand Down
1 change: 0 additions & 1 deletion source/jormungandr/jormungandr/street_network/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,3 @@
from jormungandr.street_network.car_with_park import CarWithPark
from jormungandr.street_network.handimap import Handimap
from jormungandr.street_network.andyamo import Andyamo
from jormungandr.street_network.bike import Bike
Loading

0 comments on commit aa31fba

Please sign in to comment.