Skip to content

Commit ba1fef2

Browse files
committed
[web] also return minutes in costs
1 parent ee012d1 commit ba1fef2

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

web/src/graph.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ impl Graph {
130130
end: usize,
131131
is_car: bool,
132132
use_distance: bool,
133-
) -> Option<(Vec<usize>, usize)> {
133+
) -> Option<(Vec<usize>, f32)> {
134134
let mut dist = vec![(usize::MAX, None); self.nodes.len()];
135135

136136
let mut heap = BinaryHeap::new();
@@ -152,7 +152,7 @@ impl Graph {
152152
}
153153
path.reverse();
154154
// println!("zero-counter {:?}", counter);
155-
return Some((path, cost / COST_MULTIPLICATOR));
155+
return Some((path, cost as f32 / COST_MULTIPLICATOR as f32));
156156
}
157157

158158
if cost > dist[node].0 {

web/src/main.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ use actix_web::{middleware, web, App, HttpServer};
1111
use bincode::deserialize_from;
1212
use graph::Graph;
1313
use serde::{Deserialize, Serialize};
14+
use std::collections::HashMap;
1415
use std::fs::File;
1516
use std::io::BufReader;
1617
use std::path::Path;
1718
use std::time::Instant;
18-
use std::collections::HashMap;
1919

2020
#[derive(Copy, Clone, Deserialize, Debug)]
2121
pub struct Way {
@@ -37,7 +37,7 @@ struct Input {
3737
nodes: Vec<Node>,
3838
ways: Vec<Way>,
3939
offset: Vec<usize>,
40-
grid: HashMap<(usize, usize), Vec<usize>>
40+
grid: HashMap<(usize, usize), Vec<usize>>,
4141
}
4242

4343
#[derive(Debug, Deserialize, Serialize)]
@@ -78,14 +78,23 @@ fn query(request: web::Json<Query>, dijkstra: web::Data<Graph>) -> web::Json<Res
7878
println!("### duration for find_path(): {:?}", timing.elapsed());
7979

8080
let result: Vec<Node>;
81-
let mut cost: String;
81+
let mut cost: String = "".to_string();
8282
match tmp {
8383
Some((path, path_cost)) => {
8484
result = dijkstra.get_coordinates(path);
85-
cost = path_cost.to_string();
8685
match by_distance {
87-
false => cost.push_str(" hour"),
88-
true => cost.push_str(" km"),
86+
false => {
87+
if path_cost.trunc() >= 1.0 {
88+
cost = path_cost.trunc().to_string();
89+
cost.push_str("h ");
90+
}
91+
cost.push_str(&format!("{:.0}", path_cost.fract() * 60.0));
92+
cost.push_str("min");
93+
}
94+
true => {
95+
cost = format!("{:.2}", path_cost);
96+
cost.push_str("km");
97+
}
8998
};
9099
}
91100
None => {

0 commit comments

Comments
 (0)