Skip to content

Commit

Permalink
Merge pull request #4051 from hove-io/additional_parameters_activatio…
Browse files Browse the repository at this point in the history
…n_period_in_config

additional_parameters_activation_period in jormun configuration
  • Loading branch information
Krishna Prasad ADHIKARI authored Jun 23, 2023
2 parents 8efabb9 + f623744 commit e97d2dc
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 15 deletions.
9 changes: 9 additions & 0 deletions source/jormungandr/jormungandr/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
make_origin_destination_key,
read_best_boarding_positions,
read_origin_destination_data,
str_to_time_stamp,
)
from jormungandr import pt_planners_manager, transient_socket
import os
Expand Down Expand Up @@ -156,6 +157,7 @@ def __init__(
instance_db=None,
best_boarding_positions_dir=None,
olympics_forbidden_uris=None,
additional_params_period=None,
):
super(Instance, self).__init__(
name=name,
Expand Down Expand Up @@ -253,6 +255,8 @@ def __init__(
self.od_additional_parameters = None
self.od_stop_areas = None
self.od_lines = None
self.additional_params_period_start = None
self.additional_params_period_end = None

# Read the best_boarding_positions files if any
if best_boarding_positions_dir is not None:
Expand All @@ -268,6 +272,11 @@ def __init__(
file_path = os.path.join(origin_destination_dir, "{}_od_additional_parameters.csv".format(self.name))
self.od_additional_parameters, _, _ = read_origin_destination_data(file_path)

# If configured initialize additional parameters activation period values
if additional_params_period:
self.additional_params_period_start = str_to_time_stamp(additional_params_period.get('start'))
self.additional_params_period_end = str_to_time_stamp(additional_params_period.get('end'))

def get_providers_from_db(self):
"""
:return: a callable query of equipment providers associated to the current instance in db
Expand Down
1 change: 1 addition & 0 deletions source/jormungandr/jormungandr/instance_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ def register_instance(self, config):
config.get('ghost_words', []),
best_boarding_positions_dir=app.config.get(str('BEST_BOARDING_POSITIONS_DIR'), None),
olympics_forbidden_uris=config.get('olympics_forbidden_uris', None),
additional_params_period=config.get('additional_parameters_activation_period', None),
)

self.instances[instance.name] = instance
Expand Down
28 changes: 16 additions & 12 deletions source/jormungandr/jormungandr/scenarios/new_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -1292,20 +1292,21 @@ def add_olympics_forbidden_uris(origin_detail, destination_detail, api_request,
api_request["forbidden_uris[]"] = instance.olympics_forbidden_uris.pt_object_olympics_forbidden_uris


def request_in_maintenance_period(datetime):
# Maintenance work is planned for 2023/08/13 00:00:00 to 2023/08/15 24:00:00 local time
start_datetime = str_to_time_stamp('20230812T220000') # 2023/08/13 00:00:00 local datetime
end_datetime = str_to_time_stamp('20230815T215959') # 2023/08/15 23:59:59 local datetime
return start_datetime <= datetime <= end_datetime
def is_origin_destination_rules_applicable(instance, datetime):
# At least od_allowed_ids or od_additional_parameters should be present
if not (instance.od_allowed_ids or instance.od_additional_parameters):
return False

# We don't apply the rule if additional_parameters_activation_period is absent or present with bad value
if not (instance.additional_params_period_start and instance.additional_params_period_end):
return False

# We apply the rule only if the request data_time is in within maintenance period configured
return instance.additional_params_period_start <= datetime <= instance.additional_params_period_end

def apply_origin_destination_rules(origin_uri, destination_uri, api_request, instance):
# Add parameter allowed_id[] in the request if instance.od_allowed_ids exists and allowed_ids found for a key
if not (instance.od_allowed_ids or instance.od_additional_parameters):
return

# We apply the rule only if the request data_time in within maintenance period
if not request_in_maintenance_period(api_request.get('datetime')):
def apply_origin_destination_rules(origin_uri, destination_uri, api_request, instance):
if not is_origin_destination_rules_applicable(instance, api_request.get('datetime')):
return

# We re-initialize allowed_id[] values ignoring already existing parameter values.
Expand Down Expand Up @@ -1517,8 +1518,11 @@ def fill_journeys(self, request_type, api_request, instance):
update_total_air_pollutants(pb_resp)
# Tag ecologic should be done at the end
tag_ecologic(pb_resp)

# Tag special_event
if instance.additional_parameters and request_in_maintenance_period(api_request.get('datetime')):
if instance.additional_parameters and is_origin_destination_rules_applicable(
instance, api_request.get('datetime')
):
tag_special_event(instance, pb_resp)
# Update best boarding positions in PT sections
update_best_boarding_positions(pb_resp, instance)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,8 @@ def test_od_allowed_ids_present_and_od_present():
destination_uri = "sa:10"
instance = FakeInstance()
instance.od_allowed_ids = make_od_allowed_ids()
instance.additional_params_period_start = str_to_time_stamp('20230812T000000')
instance.additional_params_period_end = str_to_time_stamp('20230814T215959')
apply_origin_destination_rules(origin_uri, destination_uri, api_request, instance)
assert "allowed_id[]" in api_request
assert api_request["allowed_id[]"][0] == "rer:1"
Expand Down
12 changes: 9 additions & 3 deletions source/jormungandr/jormungandr/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,21 @@ def navitia_utcfromtimestamp(timestamp):
return None


def str_to_time_stamp(str):
def str_to_time_stamp(datetime_str):
"""
convert a string to a posix timestamp
the string must be in the YYYYMMDDTHHMMSS format
like 20170534T124500
"""
date = datetime.strptime(str, DATETIME_FORMAT)
try:
date = datetime.strptime(datetime_str, DATETIME_FORMAT)

return date_to_timestamp(date)
return date_to_timestamp(date)
except Exception as e:
logging.getLogger(__name__).exception(
'Error while converting a string to a posix timestamp with exception: {}'.format(str(e))
)
return None


def str_to_dt(str):
Expand Down

0 comments on commit e97d2dc

Please sign in to comment.