Skip to content
Merged
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,9 @@ tokio::spawn(async move {
RoutingProgress::ComputingMatrix { percent, row, total } => {
println!("[{:3}%] Computing matrix row {}/{}", percent, row, total);
}
RoutingProgress::Complete => {
println!("[100%] Done");
}
_ => {}
}
}
Expand All @@ -437,7 +440,6 @@ let matrix = network.compute_matrix(&locations, Some(&tx)).await;
| `BuildingGraph { percent }` | Building routing graph |
| `ComputingMatrix { percent, row, total }` | Computing travel time matrix |
| `ComputingGeometries { percent, pair, total }` | Computing route geometries |
| `EncodingGeometries { percent }` | Encoding geometries to polylines |
| `Complete` | Operation finished |

---
Expand Down
8 changes: 1 addition & 7 deletions src/routing/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ out body;"#,
let _ = tx
.send(RoutingProgress::BuildingGraph { percent: 50 })
.await;
let _ = tx.send(RoutingProgress::Complete).await;
}

Ok(network)
Expand Down Expand Up @@ -557,13 +558,6 @@ fn is_retryable_error(error: &reqwest::Error) -> bool {
error.is_timeout() || error.is_connect() || error.is_request()
}

impl RoadNetwork {
#[doc(hidden)]
pub async fn load_or_fetch_simple(bbox: &BoundingBox) -> Result<NetworkRef, RoutingError> {
Self::load_or_fetch(bbox, &NetworkConfig::default(), None).await
}
}

async fn acquire_in_flight_slot(cache_key: &str) -> (Arc<Mutex<()>>, OwnedMutexGuard<()>) {
let slot = {
let mut in_flight = in_flight_loads().lock().await;
Expand Down
46 changes: 38 additions & 8 deletions src/routing/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use rayon::prelude::*;
use std::collections::HashMap;
use tokio::sync::mpsc::Sender;
use tokio::sync::mpsc::{self, Sender};

use super::algo::dijkstra;
use super::coord::Coord;
Expand Down Expand Up @@ -195,8 +195,29 @@ impl RoadNetwork {
})
.collect();

let row_progress = progress.map(|tx| {
let (progress_tx, mut progress_rx) = mpsc::unbounded_channel::<usize>();
let tx = tx.clone();
let handle = tokio::spawn(async move {
let mut completed_rows = 0usize;
while progress_rx.recv().await.is_some() {
completed_rows += 1;
let percent = 55 + ((completed_rows * 44) / n.max(1)) as u8;
let _ = tx
.send(RoutingProgress::ComputingMatrix {
percent,
row: completed_rows,
total: n,
})
.await;
}
});
(progress_tx, handle)
});

// Compute rows in parallel via rayon - each row runs Dijkstra from source endpoints
let graph = &self.graph;
let progress_tx = row_progress.as_ref().map(|(tx, _)| tx.clone());
let rows: Vec<Vec<i64>> = (0..n)
.into_par_iter()
.map(|i| {
Expand All @@ -208,6 +229,9 @@ impl RoadNetwork {
*cell = UNREACHABLE;
}
}
if let Some(tx) = &progress_tx {
let _ = tx.send(i);
}
return row;
};

Expand All @@ -230,18 +254,20 @@ impl RoadNetwork {
};
}

if let Some(tx) = &progress_tx {
let _ = tx.send(i);
}
row
})
.collect();

if let Some((progress_tx, handle)) = row_progress {
drop(progress_tx);
let _ = handle.await;
}

if let Some(tx) = progress {
let _ = tx
.send(RoutingProgress::ComputingMatrix {
percent: 80,
row: n,
total: n,
})
.await;
let _ = tx.send(RoutingProgress::Complete).await;
}

let data: Vec<i64> = rows.into_iter().flatten().collect();
Expand Down Expand Up @@ -283,6 +309,10 @@ impl RoadNetwork {
}
}

if let Some(tx) = progress {
let _ = tx.send(RoutingProgress::Complete).await;
}

geometries
}
}
5 changes: 0 additions & 5 deletions src/routing/progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ pub enum RoutingProgress {
pair: usize,
total: usize,
},
EncodingGeometries {
percent: u8,
},
Complete,
}

Expand All @@ -45,7 +42,6 @@ impl RoutingProgress {
Self::BuildingGraph { percent } => *percent,
Self::ComputingMatrix { percent, .. } => *percent,
Self::ComputingGeometries { percent, .. } => *percent,
Self::EncodingGeometries { percent } => *percent,
Self::Complete => 100,
}
}
Expand All @@ -58,7 +54,6 @@ impl RoutingProgress {
Self::BuildingGraph { .. } => ("building", "Building routing graph..."),
Self::ComputingMatrix { .. } => ("matrix", "Computing travel times..."),
Self::ComputingGeometries { .. } => ("geometry", "Computing route geometries..."),
Self::EncodingGeometries { .. } => ("encoding", "Encoding geometries..."),
Self::Complete => ("complete", "Ready!"),
}
}
Expand Down
Loading