Skip to content

Commit 7455a45

Browse files
authored
Merge pull request #4230 from hove-io/keep_best_olympics_when_using_max_nb_journeys
[Jormungandr] Keep best_olympics
2 parents d839fdc + aa7f9ed commit 7455a45

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

source/jormungandr/jormungandr/scenarios/new_default.py

+11-9
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@
101101
from functools import cmp_to_key
102102

103103
SECTION_TYPES_TO_RETAIN = {response_pb2.PUBLIC_TRANSPORT, response_pb2.STREET_NETWORK}
104+
JOURNEY_TAGS_TO_RETAIN = ['best_olympics']
104105
JOURNEY_TYPES_TO_RETAIN = ['best', 'comfort', 'non_pt_walk', 'non_pt_bike', 'non_pt_bss']
106+
JOURNEY_TYPES_SCORE = {t: i for i, t in enumerate(JOURNEY_TYPES_TO_RETAIN)}
105107
STREET_NETWORK_MODE_TO_RETAIN = {response_pb2.Ridesharing, response_pb2.Car, response_pb2.Bike, response_pb2.Bss}
106108
TEMPLATE_MSG_UNKNOWN_OBJECT = "The entry point: {} is not valid"
107109

@@ -515,7 +517,7 @@ def _build_candidate_pool_and_sections_set(journeys):
515517
idx_of_jrny_must_keep = list()
516518

517519
for (i, jrny) in enumerate(journeys):
518-
if jrny.type in JOURNEY_TYPES_TO_RETAIN:
520+
if set(jrny.tags) & set(JOURNEY_TAGS_TO_RETAIN) or jrny.type in set(JOURNEY_TYPES_TO_RETAIN):
519521
idx_of_jrny_must_keep.append(i)
520522
sections_set |= set([_get_section_id(s) for s in jrny.sections if s.type in SECTION_TYPES_TO_RETAIN])
521523
candidates_pool.append(jrny)
@@ -750,16 +752,16 @@ def _inverse_selection(d, indexes):
750752
)
751753

752754
# At this point, resp.journeys should contain only must-have journeys
753-
list_dict = collections.defaultdict(list)
754-
for jrny in resp.journeys:
755-
if not journey_filter.to_be_deleted(jrny):
756-
list_dict[jrny.type].append(jrny)
755+
must_have = [j for j in resp.journeys if not journey_filter.to_be_deleted(j)]
757756

758-
sorted_by_type_journeys = []
759-
for t in JOURNEY_TYPES_TO_RETAIN:
760-
sorted_by_type_journeys.extend(list_dict.get(t, []))
757+
def journey_score(j):
758+
if set(j.tags) & set(JOURNEY_TAGS_TO_RETAIN):
759+
return -100
760+
return JOURNEY_TYPES_SCORE.get(j.type, float('inf'))
761761

762-
for jrny in sorted_by_type_journeys[max_nb_journeys:]:
762+
must_have.sort(key=journey_score)
763+
764+
for jrny in must_have[max_nb_journeys:]:
763765
journey_filter.mark_as_dead(jrny, is_debug, 'Filtered by max_nb_journeys')
764766

765767
journey_filter.delete_journeys((resp,), request)

source/jormungandr/jormungandr/scenarios/tests/new_default_tests.py

+30
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,36 @@ def culling_journeys_4_test():
282282
assert jrny.type in ('best', 'comfort', 'non_pt_walk')
283283

284284

285+
def culling_journeys_5_test():
286+
"""
287+
When max_nb_journeys == 3 and nb_must_have_journeys == 4 and one journey has best_olympics in its tags
288+
"""
289+
mocked_pb_response = build_mocked_response()
290+
# tag last journeys with
291+
mocked_pb_response.journeys[10].tags.append("best_olympics")
292+
mocked_request = {'max_nb_journeys': 6, 'debug': False, 'datetime': 1444903200}
293+
new_default.culling_journeys(mocked_pb_response, mocked_request)
294+
295+
best_olympic = next((j for j in mocked_pb_response.journeys if 'best_olympics' in j.tags))
296+
assert best_olympic
297+
298+
299+
def culling_journeys_6_test():
300+
"""
301+
When max_nb_journeys == 3 and nb_must_have_journeys == 4 and one journey has best_olympics in its tags
302+
"""
303+
mocked_pb_response = build_mocked_response()
304+
# tag last journeys with
305+
mocked_pb_response.journeys[5].tags.append("best_olympics")
306+
307+
mocked_request = {'max_nb_journeys': 3, 'debug': False, 'datetime': 1444903200}
308+
new_default.culling_journeys(mocked_pb_response, mocked_request)
309+
assert len(mocked_pb_response.journeys) == 3
310+
311+
best_olympic = next((j for j in mocked_pb_response.journeys if 'best_olympics' in j.tags))
312+
assert best_olympic
313+
314+
285315
def aggregate_journeys_test():
286316
mocked_pb_response = build_mocked_response()
287317
aggregated_journeys, remaining_journeys = new_default.aggregate_journeys(mocked_pb_response.journeys)

0 commit comments

Comments
 (0)