Skip to content

Commit

Permalink
Add route for safest route between systems
Browse files Browse the repository at this point in the history
  • Loading branch information
madmikeross committed Dec 23, 2023
1 parent bb6da97 commit 37c927a
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
31 changes: 31 additions & 0 deletions src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,3 +528,34 @@ pub async fn find_shortest_route(
None => Ok(None),
}
}

pub async fn find_safest_route(
graph: Arc<Graph>,
from_system_name: String,
to_system_name: String,
) -> Result<Option<Vec<String>>, Error> {
let shortest_path_query = "\
MATCH (source:System {name: $from_system_name}), (target:System {name: $to_system_name})
CALL gds.shortestPath.dijkstra.stream('jump-risk', {
sourceNode: source,
targetNode: target,
relationshipWeightProperty: 'risk'
})
YIELD index, sourceNode, targetNode, totalCost, nodeIds, costs, path
RETURN
[nodeId IN nodeIds | gds.util.asNode(nodeId).name] AS nodeNames
";

let mut result = graph
.execute(
query(shortest_path_query)
.param("from_system_name", from_system_name)
.param("to_system_name", to_system_name),
)
.await?;

match result.next().await? {
Some(row) => Ok(row.get("nodeNames").ok()),
None => Ok(None),
}
}
36 changes: 32 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,16 @@ async fn main() {
.and(with_graph(graph.clone()))
.and_then(wormholes_refresh_handler);

let routes_to = warp::path!("routes" / String / "to" / String);
let routes_routes = routes_to
let shortest_route_to = warp::path!("shortest-route" / String / "to" / String);
let safest_route_to = warp::path!("safest-route" / String / "to" / String);
let routes_routes = shortest_route_to
.and(warp::get())
.and(with_graph(graph.clone()))
.and_then(routes_to_handler);
.and_then(shortest_route_to_handler)
.or(safest_route_to
.and(warp::get())
.and(with_graph(graph.clone()))
.and_then(safest_route_to_handler));

let service_routes = routes_routes
.or(wormholes_routes)
Expand Down Expand Up @@ -107,7 +112,7 @@ async fn handle_rejection(err: Rejection) -> Result<impl Reply, Infallible> {
))
}

async fn routes_to_handler(
async fn shortest_route_to_handler(
from_system_name: String,
to_system_name: String,
graph: Arc<Graph>,
Expand All @@ -121,6 +126,29 @@ async fn routes_to_handler(
}
}

async fn safest_route_to_handler(
from_system_name: String,
to_system_name: String,
graph: Arc<Graph>,
) -> Result<impl Reply, Rejection> {
let exists = graph_exists(&graph, String::from("jump-risk"))
.await
.map_err(TargetError);
if !exists.unwrap() {
let _ = build_jump_risk_graph(graph.clone())
.await
.map_err(TargetError);
}

match find_safest_route(graph, from_system_name, to_system_name)
.await
.unwrap()
{
None => Ok(json::<String>(&String::from(""))),
Some(route) => Ok(json::<Vec<_>>(&route)),
}
}

async fn wormholes_refresh_handler(
client: Client,
graph: Arc<Graph>,
Expand Down

0 comments on commit 37c927a

Please sign in to comment.