Skip to content

Commit

Permalink
Check if gds graph exists before trying to drop when refreshing
Browse files Browse the repository at this point in the history
  • Loading branch information
madmikeross committed Dec 23, 2023
1 parent 390112a commit a821570
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 15 deletions.
30 changes: 25 additions & 5 deletions src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,22 @@ pub async fn create_system_jump(
.await
}

pub async fn graph_exists(graph: &Arc<Graph>, graph_name: String) -> Result<bool, Error> {
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::<String>("graphName").unwrap() == graph_name {
return Ok(true);
}
}

Ok(false)
}

pub async fn drop_system_jump_graph(graph: &Arc<Graph>) -> Result<String, Error> {
let drop_graph = "CALL gds.graph.drop('system-map')";
let mut result = graph
Expand All @@ -396,7 +412,7 @@ pub async fn drop_system_jump_graph(graph: &Arc<Graph>) -> Result<String, Error>
row.get::<String>("graphName").map_err(DeserializationError)
}

pub async fn drop_jump_risk_graph(graph: Arc<Graph>) -> Result<String, Error> {
pub async fn drop_jump_risk_graph(graph: &Arc<Graph>) -> Result<String, Error> {
let drop_graph = "CALL gds.graph.drop('jump-risk')";
let mut result = graph
.execute(query(drop_graph))
Expand Down Expand Up @@ -466,14 +482,18 @@ pub async fn drop_system_connections(graph: &Arc<Graph>, system_name: &str) -> R
.await
}

pub async fn rebuild_jump_cost_graph(graph: Arc<Graph>) -> Result<(), Error> {
drop_system_jump_graph(&graph).await?;
pub async fn refresh_jump_cost_graph(graph: Arc<Graph>) -> 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<Graph>) -> Result<(), Error> {
drop_jump_risk_graph(graph.clone()).await?;
pub async fn refresh_jump_risk_graph(graph: Arc<Graph>) -> 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(())
}
Expand Down
26 changes: 16 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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<Graph>) -> Result<impl Reply, Rejection> {
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())
}

Expand All @@ -150,7 +150,8 @@ async fn stargates_refresh_handler(
client: Client,
graph: Arc<Graph>,
) -> Result<impl Reply, Rejection> {
pull_all_stargates(client, graph).await?;
pull_all_stargates(client, graph.clone()).await?;
refresh_jump_cost_graph(graph).await.unwrap();
Ok(reply())
}

Expand Down Expand Up @@ -358,7 +359,10 @@ async fn pull_system_kills(client: Client, graph: Arc<Graph>) -> Result<i32, Rep
.map(|_| galaxy_kills)
}

async fn pull_system_jumps(client: Client, graph: Arc<Graph>) -> Result<i32, ReplicationError> {
async fn pull_last_hour_of_jumps(
client: Client,
graph: Arc<Graph>,
) -> Result<i32, ReplicationError> {
let response = get_system_jumps(&client).await?;
let galaxy_jumps: i32 = response.system_jumps.iter().map(|s| s.ship_jumps).sum();

Expand All @@ -379,9 +383,9 @@ async fn pull_system_jumps(client: Client, graph: Arc<Graph>) -> Result<i32, Rep
.map(|_| galaxy_jumps)
}

async fn pull_system_risk(client: Client, graph: Arc<Graph>) -> Result<(), ReplicationError> {
async fn refresh_jump_risks(client: Client, graph: Arc<Graph>) -> 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();

Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit a821570

Please sign in to comment.