diff --git a/src/database.rs b/src/database.rs index f6ac299..7be44aa 100644 --- a/src/database.rs +++ b/src/database.rs @@ -381,6 +381,22 @@ pub async fn create_system_jump( .await } +pub async fn graph_exists(graph: &Arc, graph_name: String) -> Result { + let list_of_graphs_query = "CALL gds.graph.list"; + let mut result = graph + .execute(query(list_of_graphs_query)) + .await + .expect("Failed to get a list of gds graphs"); + + while let Some(row) = result.next().await? { + if row.get::("graphName").unwrap() == graph_name { + return Ok(true); + } + } + + Ok(false) +} + pub async fn drop_system_jump_graph(graph: &Arc) -> Result { let drop_graph = "CALL gds.graph.drop('system-map')"; let mut result = graph @@ -396,7 +412,7 @@ pub async fn drop_system_jump_graph(graph: &Arc) -> Result row.get::("graphName").map_err(DeserializationError) } -pub async fn drop_jump_risk_graph(graph: Arc) -> Result { +pub async fn drop_jump_risk_graph(graph: &Arc) -> Result { let drop_graph = "CALL gds.graph.drop('jump-risk')"; let mut result = graph .execute(query(drop_graph)) @@ -466,14 +482,18 @@ pub async fn drop_system_connections(graph: &Arc, system_name: &str) -> R .await } -pub async fn rebuild_jump_cost_graph(graph: Arc) -> Result<(), Error> { - drop_system_jump_graph(&graph).await?; +pub async fn refresh_jump_cost_graph(graph: Arc) -> Result<(), Error> { + if graph_exists(&graph, String::from("system-map")).await? { + drop_system_jump_graph(&graph).await?; + } let _ = build_system_jump_graph(graph).await?; Ok(()) } -pub async fn rebuild_jump_risk_graph(graph: Arc) -> Result<(), Error> { - drop_jump_risk_graph(graph.clone()).await?; +pub async fn refresh_jump_risk_graph(graph: Arc) -> Result<(), Error> { + if graph_exists(&graph, String::from("jump-risk")).await? { + drop_jump_risk_graph(&graph).await?; + } let _ = build_jump_risk_graph(graph.clone()).await?; Ok(()) } diff --git a/src/main.rs b/src/main.rs index 7c0a829..f535845 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,7 +26,7 @@ mod eve_scout; async fn main() { println!("Starting eve-graph"); let client = Client::new(); - let graph = get_graph_client_with_retry(5).await.unwrap(); + let graph = get_graph_client_with_retry(10).await.unwrap(); let systems_refresh = warp::path!("systems" / "refresh"); let systems_risk = warp::path!("systems" / "risk"); @@ -128,13 +128,13 @@ async fn wormholes_refresh_handler( refresh_eve_scout_system_relations(client, graph.clone()) .await .unwrap(); - rebuild_jump_cost_graph(graph).await.unwrap(); + refresh_jump_cost_graph(graph).await.unwrap(); Ok(reply()) } async fn systems_risk_handler(client: Client, graph: Arc) -> Result { - pull_system_risk(client, graph.clone()).await.unwrap(); - rebuild_jump_risk_graph(graph).await.unwrap(); + refresh_jump_risks(client, graph.clone()).await?; + refresh_jump_risk_graph(graph).await.unwrap(); Ok(reply()) } @@ -150,7 +150,8 @@ async fn stargates_refresh_handler( client: Client, graph: Arc, ) -> Result { - pull_all_stargates(client, graph).await?; + pull_all_stargates(client, graph.clone()).await?; + refresh_jump_cost_graph(graph).await.unwrap(); Ok(reply()) } @@ -358,7 +359,10 @@ async fn pull_system_kills(client: Client, graph: Arc) -> Result) -> Result { +async fn pull_last_hour_of_jumps( + client: Client, + graph: Arc, +) -> Result { let response = get_system_jumps(&client).await?; let galaxy_jumps: i32 = response.system_jumps.iter().map(|s| s.ship_jumps).sum(); @@ -379,9 +383,9 @@ async fn pull_system_jumps(client: Client, graph: Arc) -> Result) -> Result<(), ReplicationError> { +async fn refresh_jump_risks(client: Client, graph: Arc) -> Result<(), ReplicationError> { let galaxy_kills = pull_system_kills(client.clone(), graph.clone()).await?; - let galaxy_jumps = pull_system_jumps(client.clone(), graph.clone()).await?; + let galaxy_jumps = pull_last_hour_of_jumps(client.clone(), graph.clone()).await?; let system_ids = get_all_system_ids(graph.clone()).await?; let mut set = JoinSet::new(); @@ -436,7 +440,9 @@ mod tests { use crate::database::{get_graph_client_with_retry, save_system, System}; use crate::esi::get_system_details; - use crate::{pull_all_stargates, pull_system_jumps, pull_system_kills, pull_system_stargates}; + use crate::{ + pull_all_stargates, pull_last_hour_of_jumps, pull_system_kills, pull_system_stargates, + }; #[tokio::test] #[ignore] @@ -487,7 +493,7 @@ mod tests { let client = Client::new(); let graph = get_graph_client_with_retry(1).await.unwrap(); - let total_jumps = pull_system_jumps(client, graph).await.unwrap(); + let total_jumps = pull_last_hour_of_jumps(client, graph).await.unwrap(); assert!(total_jumps > 0) }