Can't even keep track anymore
This commit is contained in:
parent
dccc1e439d
commit
3a3f899ea2
50 changed files with 1144 additions and 560 deletions
|
|
@ -2,20 +2,25 @@ use serde::{Deserialize, Serialize};
|
|||
use tracing::warn;
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct TravelTimeRequest {
|
||||
origins: Vec<[f64; 2]>,
|
||||
destination: [f64; 2],
|
||||
struct R5Request {
|
||||
origin: [f64; 2],
|
||||
destinations: Vec<[f64; 2]>,
|
||||
mode: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct TravelTimeResponse {
|
||||
travel_times: Vec<Option<f64>>,
|
||||
struct R5Response {
|
||||
travel_times: Vec<f64>,
|
||||
}
|
||||
|
||||
/// Call the R5 service to compute many-to-one travel times.
|
||||
/// Call the R5 Java service to compute one-to-many travel times.
|
||||
///
|
||||
/// Returns a Vec of travel times in minutes (one per origin), with None for unreachable origins.
|
||||
/// `origins` are hex centroids as `[lat, lon]`.
|
||||
/// `destination` is the user-chosen point as `[lat, lon]`.
|
||||
/// `mode` is one of "car", "bicycle", "walking", "transit".
|
||||
///
|
||||
/// R5 computes from destination to all origins (one-to-many from the user's chosen point).
|
||||
/// Returns a Vec of travel times in minutes (one per origin), with None for unreachable.
|
||||
pub async fn fetch_travel_times(
|
||||
client: &reqwest::Client,
|
||||
r5_url: &str,
|
||||
|
|
@ -23,36 +28,45 @@ pub async fn fetch_travel_times(
|
|||
destination: [f64; 2],
|
||||
mode: &str,
|
||||
) -> Result<Vec<Option<f64>>, String> {
|
||||
let url = format!("{}/travel-times", r5_url);
|
||||
if origins.is_empty() {
|
||||
return Ok(vec![]);
|
||||
}
|
||||
|
||||
let request_body = TravelTimeRequest {
|
||||
origins,
|
||||
destination,
|
||||
let body = R5Request {
|
||||
origin: destination,
|
||||
destinations: origins,
|
||||
mode: mode.to_string(),
|
||||
};
|
||||
|
||||
let resp = client
|
||||
.post(&url)
|
||||
.json(&request_body)
|
||||
.timeout(std::time::Duration::from_secs(60))
|
||||
.post(format!("{}/travel-times", r5_url))
|
||||
.json(&body)
|
||||
.timeout(std::time::Duration::from_secs(30))
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| {
|
||||
warn!("R5 request failed: {}", e);
|
||||
format!("R5 service error: {}", e)
|
||||
format!("R5 routing error: {}", e)
|
||||
})?;
|
||||
|
||||
if !resp.status().is_success() {
|
||||
let status = resp.status();
|
||||
let body = resp.text().await.unwrap_or_default();
|
||||
warn!("R5 returned {}: {}", status, body);
|
||||
return Err(format!("R5 service returned {}: {}", status, body));
|
||||
return Err(format!("R5 returned {}: {}", status, body));
|
||||
}
|
||||
|
||||
let body: TravelTimeResponse = resp.json().await.map_err(|e| {
|
||||
let r5_resp: R5Response = resp.json().await.map_err(|e| {
|
||||
warn!("Failed to parse R5 response: {}", e);
|
||||
format!("Failed to parse R5 response: {}", e)
|
||||
})?;
|
||||
|
||||
Ok(body.travel_times)
|
||||
// R5 returns -1 for unreachable destinations
|
||||
let travel_times: Vec<Option<f64>> = r5_resp
|
||||
.travel_times
|
||||
.into_iter()
|
||||
.map(|t| if t < 0.0 { None } else { Some(t) })
|
||||
.collect();
|
||||
|
||||
Ok(travel_times)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue