def fill_journeys(self, pb_req, request, instance): """ call kraken to get the requested number of journeys It more journeys are wanted, we ask for the next (or previous if not clockwise) departures """ resp = response_pb2.Response() next_request = copy.deepcopy(pb_req) nb_typed_journeys = 0 cpt_attempt = 0 max_attempts = 2 if not request[ "min_nb_journeys"] else request["min_nb_journeys"] * 2 at_least_one_journey_found = False forbidden_uris = [] #call to kraken must be done with fallback mode in the right order: #walking, then bss, then bike, and finally car #in some case a car journey can be equivalent to a walking journey, typically when crowfly are used #and only the first one is kept, and we want the walking one to be kept! self.origin_modes.sort(fallback_mode_comparator) self.destination_modes.sort(fallback_mode_comparator) while ((request["min_nb_journeys"] and request["min_nb_journeys"] > nb_typed_journeys) or\ (not request["min_nb_journeys"] and nb_typed_journeys == 0)) and cpt_attempt < max_attempts: tmp_resp = self.call_kraken(next_request, instance) if len(tmp_resp.journeys) == 0: # if we do not yet have journeys, we get the tmp_resp to have the error if there are some if len(resp.journeys) == 0: resp = tmp_resp break at_least_one_journey_found = True if request['clockwise']: new_datetime = self.next_journey_datetime( tmp_resp.journeys, request['clockwise']) else: new_datetime = self.previous_journey_datetime( tmp_resp.journeys, request['clockwise']) if new_datetime is None: break next_request, forbidden_uris = self.change_request( pb_req, tmp_resp, forbidden_uris) next_request.journeys.datetimes[0] = new_datetime del next_request.journeys.datetimes[1:] # we tag the journeys as 'next' or 'prev' journey if len(resp.journeys): for j in tmp_resp.journeys: j.tags.append("next" if request['clockwise'] else "prev") self.merge_response(resp, tmp_resp) #we delete after the merge, else we will have duplicate non_pt journey in the count self.delete_journeys(resp, request, final_filter=False) nb_typed_journeys = count_typed_journeys(resp.journeys) cpt_attempt += 1 if not request['debug']: self._remove_not_long_enough_fallback(resp, instance) self._remove_not_long_enough_tc_with_fallback(resp, instance) self._delete_too_long_journey(resp, instance, request['clockwise']) self.sort_journeys(resp, instance.journey_order, request['clockwise']) self.choose_best(resp) self.delete_journeys( resp, request, final_filter=True ) # filter one last time to remove similar journeys if len(resp.journeys ) == 0 and at_least_one_journey_found and not resp.HasField( str("error")): error = resp.error error.id = response_pb2.Error.no_solution error.message = "No journey found, all were filtered" self._compute_pagination_links(resp, instance, request['clockwise']) return resp
def fill_journeys(self, pb_req, request, instance): """ call kraken to get the requested number of journeys It more journeys are wanted, we ask for the next (or previous if not clockwise) departures """ resp = response_pb2.Response() next_request = copy.deepcopy(pb_req) nb_typed_journeys = 0 cpt_attempt = 0 max_attempts = 2 if not request["min_nb_journeys"] else request["min_nb_journeys"]*2 at_least_one_journey_found = False forbidden_uris = [] while ((request["min_nb_journeys"] and request["min_nb_journeys"] > nb_typed_journeys) or\ (not request["min_nb_journeys"] and nb_typed_journeys == 0)) and cpt_attempt < max_attempts: tmp_resp = self.call_kraken(next_request, instance) if len(tmp_resp.journeys) == 0: # if we do not yet have journeys, we get the tmp_resp to have the error if there are some if len(resp.journeys) == 0: resp = tmp_resp break at_least_one_journey_found = True last_best = next((j for j in tmp_resp.journeys if j.type == 'rapid'), None) new_datetime = None one_minute = 60 if request['clockwise']: #since dates are now posix time stamp, we only have to add the additional seconds if not last_best: last_best = min(tmp_resp.journeys, key=lambda j: j.departure_date_time) #In this case there is no journeys, so we stop if not last_best: break new_datetime = last_best.departure_date_time + one_minute else: if not last_best: last_best = max(tmp_resp.journeys, key=lambda j: j.arrival_date_time) #In this case there is no journeys, so we stop if not last_best: break new_datetime = last_best.arrival_date_time - one_minute next_request, forbidden_uris = self.change_request(pb_req, tmp_resp, forbidden_uris) next_request.journeys.datetimes[0] = new_datetime del next_request.journeys.datetimes[1:] # we tag the journeys as 'next' or 'prev' journey if len(resp.journeys): for j in tmp_resp.journeys: j.tags.append("next" if request['clockwise'] else "prev") self.merge_response(resp, tmp_resp) #we delete after the merge, else we will have duplicate non_pt journey in the count self.delete_journeys(resp, request, final_filter=False) if not request['debug']: self._delete_non_optimal_journey(resp.journeys) nb_typed_journeys = count_typed_journeys(resp.journeys) cpt_attempt += 1 if not request['debug']: self._remove_not_long_enough_fallback(resp.journeys, instance) self._remove_not_long_enough_tc_with_fallback(resp.journeys, instance) self._delete_too_long_journey(resp.journeys, instance, request['clockwise']) self.sort_journeys(resp, instance.journey_order, request['clockwise']) self.choose_best(resp) self.delete_journeys(resp, request, final_filter=True) # filter one last time to remove similar journeys if len(resp.journeys) == 0 and at_least_one_journey_found and not resp.HasField("error"): error = resp.error error.id = response_pb2.Error.no_solution error.message = "No journey found, all were filtered" return resp