From 7b3eb2d843ecc4cef56406d6dcd055f57c5f8be0 Mon Sep 17 00:00:00 2001 From: Michael Kutzner <174690291+MichaelKutzner@users.noreply.github.com> Date: Thu, 21 Nov 2024 11:30:49 +0100 Subject: [PATCH 1/3] Use 'maxTravelTime' to filter results --- openapi.yaml | 4 +--- src/endpoints/routing.cc | 24 +++++++++++++++++++++--- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/openapi.yaml b/openapi.yaml index d07f5e8e1..bc4718dc1 100644 --- a/openapi.yaml +++ b/openapi.yaml @@ -359,7 +359,7 @@ paths: schema: type: integer - - name: maxHours + - name: maxTravelTime in: query required: false description: | @@ -371,8 +371,6 @@ paths: 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 diff --git a/src/endpoints/routing.cc b/src/endpoints/routing.cc index f964fd4b2..cc336d32f 100644 --- a/src/endpoints/routing.cc +++ b/src/endpoints/routing.cc @@ -1,5 +1,7 @@ #include "motis/endpoints/routing.h" +#include + #include "boost/thread/tss.hpp" #include "utl/erase_duplicates.h" @@ -291,6 +293,23 @@ stats_map_t join(auto&&... maps) { return ret; } +// Get cutoff to remove journeys with slower or equal duration +std::optional calculate_search_cutoff_time( + n::duration_t const fastest_direct, + std::optional 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{std::lround(60 * *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; @@ -473,9 +492,8 @@ api::plan_response routing::operator()(boost::urls::url_view const& url) const { .factor_ = static_cast(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); From 707e0191217c4075c3a153dcdeabb06e3c6812fb Mon Sep 17 00:00:00 2001 From: Michael Kutzner <174690291+MichaelKutzner@users.noreply.github.com> Date: Thu, 21 Nov 2024 11:50:26 +0100 Subject: [PATCH 2/3] Change unit to minutes --- openapi.yaml | 4 ++-- src/endpoints/routing.cc | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/openapi.yaml b/openapi.yaml index bc4718dc1..58d71c181 100644 --- a/openapi.yaml +++ b/openapi.yaml @@ -363,7 +363,7 @@ paths: 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. @@ -372,7 +372,7 @@ paths: If this value is too low to reach the destination at all, it can lead to slow routing performance. schema: - type: number + type: integer - name: minTransferTime in: query diff --git a/src/endpoints/routing.cc b/src/endpoints/routing.cc index cc336d32f..9906581d2 100644 --- a/src/endpoints/routing.cc +++ b/src/endpoints/routing.cc @@ -1,6 +1,6 @@ #include "motis/endpoints/routing.h" -#include +#include #include "boost/thread/tss.hpp" @@ -296,11 +296,11 @@ stats_map_t join(auto&&... maps) { // Get cutoff to remove journeys with slower or equal duration std::optional calculate_search_cutoff_time( n::duration_t const fastest_direct, - std::optional const& max_travel_time) { + std::optional 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{std::lround(60 * *max_travel_time) + 1}; + n::duration_t{*max_travel_time + 1}; return fastest_direct == kInfinityDuration ? max_travel_duration : std::min(fastest_direct, max_travel_duration); From 95eb07f777d39b6ee5090386ad124ba85d0bb601 Mon Sep 17 00:00:00 2001 From: Michael Kutzner <174690291+MichaelKutzner@users.noreply.github.com> Date: Thu, 21 Nov 2024 11:51:28 +0100 Subject: [PATCH 3/3] Fix formatting --- src/endpoints/routing.cc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/endpoints/routing.cc b/src/endpoints/routing.cc index 9906581d2..a6f7b4457 100644 --- a/src/endpoints/routing.cc +++ b/src/endpoints/routing.cc @@ -299,8 +299,7 @@ std::optional calculate_search_cutoff_time( std::optional 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}; + 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);