From 32a81b35619f4aa46c32491d1904603955adb07e Mon Sep 17 00:00:00 2001 From: Johannes Erwerle Date: Thu, 15 Sep 2022 12:43:52 +0200 Subject: [PATCH] cleaned up the web UI code --- src/bin/task6-rocket.rs | 88 ++++++++++++++++++++++++----------------- 1 file changed, 51 insertions(+), 37 deletions(-) diff --git a/src/bin/task6-rocket.rs b/src/bin/task6-rocket.rs index 29fd886..5460806 100644 --- a/src/bin/task6-rocket.rs +++ b/src/bin/task6-rocket.rs @@ -1,23 +1,24 @@ -#[macro_use] extern crate rocket; +#[macro_use] +extern crate rocket; use clap::Parser; -use std::process::exit; -use std::fs::File; -use fapra_osm_2::gridgraph::{GridGraph, Route, NodeId}; -use fapra_osm_2::coordinates::{RadianCoordinate, DegreeCoordinate}; -use fapra_osm_2::astar::{AStar, estimate_haversine}; -use fapra_osm_2::alt::{LandmarkSet, LandmarkBestSet}; +use fapra_osm_2::alt::{LandmarkBestSet, LandmarkSet}; +use fapra_osm_2::astar::{estimate_haversine, AStar}; +use fapra_osm_2::coordinates::{DegreeCoordinate, RadianCoordinate}; +use fapra_osm_2::gridgraph::{GridGraph, NodeId, Route}; use rand::seq::SliceRandom; use serde::Serialize; +use std::fs::File; use std::io::BufReader; -use fapra_osm_2::utils::EARTH_RADIUS; +use std::process::exit; +use std::time::Instant; -use rocket::State; -use rocket::response::status::BadRequest; use rocket::form::Form; use rocket::fs::FileServer; +use rocket::response::status::BadRequest; use rocket::serde::json::Json; -use rocket_dyn_templates::{Template, context, Engines}; +use rocket::State; +use rocket_dyn_templates::{context, Engines, Template}; #[derive(Parser, Debug)] #[clap(author, version, about, long_about=None)] @@ -58,8 +59,7 @@ struct RouteQuery<'r> { r#algorithm: &'r str, } -#[derive(Debug)] -#[derive(Serialize)] +#[derive(Debug, Serialize)] pub struct RouteResponse { success: bool, route: Option, @@ -67,64 +67,74 @@ pub struct RouteResponse { } #[post("/route", data = "")] -fn route(routequery: Form>, graphwrapper: &State) -> Result, BadRequest> { - +fn route( + routequery: Form>, + graphwrapper: &State, +) -> Result, BadRequest> { let from = match DegreeCoordinate::from_string_tuple(routequery.from) { Ok(from) => RadianCoordinate::from(from), - Err(e) => {return Result::Err(BadRequest(Some(format!("Error while parsing from field: {:?}", e))));} + Err(e) => { + return Result::Err(BadRequest(Some(format!( + "Error while parsing from field: {:?}", + e + )))); + } }; let to = match DegreeCoordinate::from_string_tuple(routequery.to) { Ok(to) => RadianCoordinate::from(to), - Err(e) => {return Result::Err(BadRequest(Some(format!("Error while parsing to field: {:?}", e))));} + Err(e) => { + return Result::Err(BadRequest(Some(format!( + "Error while parsing to field: {:?}", + e + )))); + } }; let from = graphwrapper.graph.get_nearest_node(from).unwrap(); let to = graphwrapper.graph.get_nearest_node(to).unwrap(); - println!("working on route from {:?} to {:?}", from, to); - let direct_distance = from.position.distance_to(&to.position) * EARTH_RADIUS; - println!("haversine distance: {}", direct_distance); - let graph_distance = graphwrapper.graph.shortest_path(from, to).unwrap().cost; - println!("graph distance is: {}", graph_distance); + println!("Working on a route from {:?} to {:?}", from, to); + + let astar = AStar { + graph: &graphwrapper.graph, + }; let mut algorithm = routequery.algorithm; + let start = Instant::now(); + let route = if algorithm == "astar-haversine" { println!("running A* with haversine distance"); - let astar = AStar{graph: &graphwrapper.graph}; - astar.shortest_path(from, to, estimate_haversine) } else if algorithm == "alt" { println!("running ALT"); - let mut best_landmarks = LandmarkBestSet::new(4, &graphwrapper.landmarks); - let astar = AStar{graph: &graphwrapper.graph}; best_landmarks.select_best(from.index as usize, to.index as usize); - - println!("initial estimate: {:?}", best_landmarks.estimate(from.index as NodeId, to.index as NodeId)); - astar.shortest_path(from, to, |src, dest| { best_landmarks.estimate(src.index as NodeId, dest.index as NodeId) }) - - } else { println!("running dijkstra"); algorithm = "dijkstra"; graphwrapper.graph.shortest_path(from, to) }; + let time = start.elapsed(); - println!("from: {:?}, to: {:?}", from, to); + println!("The query took {} ms", time.as_millis()); - let response = RouteResponse{success: route.is_some(), route, algorithm: algorithm.to_string()}; + let response = RouteResponse { + success: route.is_some(), + route, + algorithm: algorithm.to_string(), + }; Ok(Json(response)) } struct GraphWrapper { graph: GridGraph, - landmarks: LandmarkSet, + landmarks: LandmarkSet, } #[launch] @@ -164,11 +174,15 @@ fn rocket() -> _ { let landmarks: LandmarkSet = bincode::deserialize_from(BufReader::new(landmarks)).unwrap(); rocket::build() - .manage(GraphWrapper{graph: *graph, landmarks}) + .manage(GraphWrapper { + graph: *graph, + landmarks, + }) .mount("/", routes![index]) .mount("/", routes![random_route]) .mount("/", routes![route]) .mount("/static", FileServer::from("static")) - .attach(Template::custom(|engines: &mut Engines| {engines.handlebars.set_dev_mode(true);})) - + .attach(Template::custom(|engines: &mut Engines| { + engines.handlebars.set_dev_mode(true); + })) }