Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use maxTravelTime to filter results #668

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -359,22 +359,20 @@ paths:
schema:
type: integer

- name: maxHours
- name: maxTravelTime
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe change unit to minutes to give users more fine grained control?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I can change that. Using floats works too, but that might not be intuitive for everyone.

in: query
required: false
description: |
The maximum travel time in hours.
The maximum travel time in minutes.
If not provided, the routing to uses the value
hardcoded in the server which is usually quite high.

*Warning*: Use with care. Setting this too low can lead to
optimal (e.g. the least transfers) journeys not being found.
If this value is too low to reach the destination at all,
it can lead to slow routing performance.

TODO: pass parameter to nigiri
schema:
type: number
type: integer

- name: minTransferTime
in: query
Expand Down
23 changes: 20 additions & 3 deletions src/endpoints/routing.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "motis/endpoints/routing.h"

#include <algorithm>

#include "boost/thread/tss.hpp"

#include "utl/erase_duplicates.h"
Expand Down Expand Up @@ -291,6 +293,22 @@ stats_map_t join(auto&&... maps) {
return ret;
}

// Get cutoff to remove journeys with slower or equal duration
std::optional<n::duration_t> calculate_search_cutoff_time(
n::duration_t const fastest_direct,
std::optional<int> const& max_travel_time) {
if (max_travel_time.has_value()) {
// Add +1 to keep journeys with equal duration
auto const max_travel_duration = n::duration_t{*max_travel_time + 1};
return fastest_direct == kInfinityDuration
? max_travel_duration
: std::min(fastest_direct, max_travel_duration);
} else {
return fastest_direct == kInfinityDuration ? std::nullopt
: std::optional{fastest_direct};
}
}

void remove_slower_than_fastest_direct(n::routing::query& q) {
if (!q.fastest_direct_) {
return;
Expand Down Expand Up @@ -473,9 +491,8 @@ api::plan_response routing::operator()(boost::urls::url_view const& url) const {
.factor_ = static_cast<float>(query.transferTimeFactor_)},
.via_stops_ =
get_via_stops(*tt_, *tags_, query.via_, query.viaMinimumStay_),
.fastest_direct_ = fastest_direct == kInfinityDuration
? std::nullopt
: std::optional{fastest_direct}};
.fastest_direct_ =
calculate_search_cutoff_time(fastest_direct, query.maxTravelTime_)};
remove_slower_than_fastest_direct(q);
UTL_STOP_TIMING(query_preparation);

Expand Down
Loading