22
33use rayon:: prelude:: * ;
44use std:: collections:: HashMap ;
5- use tokio:: sync:: mpsc:: Sender ;
5+ use tokio:: sync:: mpsc:: { self , Sender } ;
66
77use super :: algo:: dijkstra;
88use super :: coord:: Coord ;
@@ -195,8 +195,29 @@ impl RoadNetwork {
195195 } )
196196 . collect ( ) ;
197197
198+ let row_progress = progress. map ( |tx| {
199+ let ( progress_tx, mut progress_rx) = mpsc:: unbounded_channel :: < usize > ( ) ;
200+ let tx = tx. clone ( ) ;
201+ let handle = tokio:: spawn ( async move {
202+ let mut completed_rows = 0usize ;
203+ while progress_rx. recv ( ) . await . is_some ( ) {
204+ completed_rows += 1 ;
205+ let percent = 55 + ( ( completed_rows * 44 ) / n. max ( 1 ) ) as u8 ;
206+ let _ = tx
207+ . send ( RoutingProgress :: ComputingMatrix {
208+ percent,
209+ row : completed_rows,
210+ total : n,
211+ } )
212+ . await ;
213+ }
214+ } ) ;
215+ ( progress_tx, handle)
216+ } ) ;
217+
198218 // Compute rows in parallel via rayon - each row runs Dijkstra from source endpoints
199219 let graph = & self . graph ;
220+ let progress_tx = row_progress. as_ref ( ) . map ( |( tx, _) | tx. clone ( ) ) ;
200221 let rows: Vec < Vec < i64 > > = ( 0 ..n)
201222 . into_par_iter ( )
202223 . map ( |i| {
@@ -208,6 +229,9 @@ impl RoadNetwork {
208229 * cell = UNREACHABLE ;
209230 }
210231 }
232+ if let Some ( tx) = & progress_tx {
233+ let _ = tx. send ( i) ;
234+ }
211235 return row;
212236 } ;
213237
@@ -230,18 +254,20 @@ impl RoadNetwork {
230254 } ;
231255 }
232256
257+ if let Some ( tx) = & progress_tx {
258+ let _ = tx. send ( i) ;
259+ }
233260 row
234261 } )
235262 . collect ( ) ;
236263
264+ if let Some ( ( progress_tx, handle) ) = row_progress {
265+ drop ( progress_tx) ;
266+ let _ = handle. await ;
267+ }
268+
237269 if let Some ( tx) = progress {
238- let _ = tx
239- . send ( RoutingProgress :: ComputingMatrix {
240- percent : 80 ,
241- row : n,
242- total : n,
243- } )
244- . await ;
270+ let _ = tx. send ( RoutingProgress :: Complete ) . await ;
245271 }
246272
247273 let data: Vec < i64 > = rows. into_iter ( ) . flatten ( ) . collect ( ) ;
@@ -283,6 +309,10 @@ impl RoadNetwork {
283309 }
284310 }
285311
312+ if let Some ( tx) = progress {
313+ let _ = tx. send ( RoutingProgress :: Complete ) . await ;
314+ }
315+
286316 geometries
287317 }
288318}
0 commit comments