From 370a7172eba7a3a033cea31fc5940ee9224a6f3d Mon Sep 17 00:00:00 2001 From: Zefanja Jobse Date: Wed, 24 Jan 2024 21:56:37 +0100 Subject: [PATCH] only run once every 10 minutes --- src/connectors/mongo.rs | 1 - src/main.rs | 291 ++++++++++++++++++++++------------------ 2 files changed, 158 insertions(+), 134 deletions(-) diff --git a/src/connectors/mongo.rs b/src/connectors/mongo.rs index 46af39e..172488e 100644 --- a/src/connectors/mongo.rs +++ b/src/connectors/mongo.rs @@ -3,7 +3,6 @@ use crate::structs::results; use bf_sparta::cookie::Cookie; use bson::Document; use chrono::{DateTime, Utc}; -use futures::TryStreamExt; use mongodb::error::Result; use mongodb::{options::ReplaceOptions, results::UpdateResult, Client, Collection, Database}; use serde::{Deserialize, Serialize}; diff --git a/src/main.rs b/src/main.rs index ba6ffb5..79df212 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,8 +12,10 @@ use std::{ collections::HashMap, ops::Add, sync::{atomic, Arc}, + time::Duration, }; use structs::results; +use tokio::time::sleep; use warp::Filter; #[tokio::main] @@ -87,171 +89,194 @@ async fn main() -> anyhow::Result<()> { let empty_game_hash: HashMap = HashMap::new(); let mut sessions: HashMap> = HashMap::new(); let mut last_ran_detailed = chrono::Utc::now(); + let mut last_ran = chrono::Utc::now(); log::info!("Started"); loop { - match mongo_client.gather_managerinfo().await { - Ok(result) => { - match gatherer::server_manager::save_server_manager_info(&influx_client, result) - .await + let run = last_ran.add(chrono::Duration::minutes(10)) >= chrono::Utc::now(); + if run { + log::info!("Starting new run"); + last_ran = chrono::Utc::now(); + + match mongo_client.gather_managerinfo().await { + Ok(result) => { + match gatherer::server_manager::save_server_manager_info(&influx_client, result) + .await + { + Ok(_) => {} + Err(e) => { + log::error!("Failed to send new manager info to influxdb {:#?}", e) + } + }; + } + Err(e) => log::error!("Failed to send new manager info {:#?}", e), + }; + log::info!("manager done"); + + let mut game_results: HashMap = HashMap::new(); + let mut failed_games: Vec<&str> = vec![]; + + let old_games = HashMap::from([ + ("bf2-playbf2", "playbf2"), + ("bf2-bf2hub", "bf2hub"), + ("bfield1942-bf1942org", "bfield1942"), + ("bf2142-openspy", "bf2142"), + ("bf2142-play2142", "play2142"), + ("bfbc2", "bfbc2"), + ("bfvietnam-qtracker", "bfvietnam"), + ("bfvietnam-openspy", "openspy"), + ]); + for (key, value) in old_games.into_iter() { + match old_games::push_old_games( + &pool, + &influx_client, + &mut mongo_client, + key, + value, + ) + .await { - Ok(_) => {} - Err(e) => log::error!("Failed to send new manager info to influxdb {:#?}", e), + Ok(game_result) => { + game_results.insert(key.to_string(), game_result); + } + Err(e) => { + log::error!("Failed oldgame: {}, with reason: {:#?}", key, e); + failed_games.push(key); + } }; } - Err(e) => log::error!("Failed to send new manager info {:#?}", e), - }; - log::info!("manager done"); - - let mut game_results: HashMap = HashMap::new(); - let mut failed_games: Vec<&str> = vec![]; + log::info!("oldgames done"); - let old_games = HashMap::from([ - ("bf2-playbf2", "playbf2"), - ("bf2-bf2hub", "bf2hub"), - ("bfield1942-bf1942org", "bfield1942"), - ("bf2142-openspy", "bf2142"), - ("bf2142-play2142", "play2142"), - ("bfbc2", "bfbc2"), - ("bfvietnam-qtracker", "bfvietnam"), - ("bfvietnam-openspy", "openspy"), - ]); - for (key, value) in old_games.into_iter() { - match old_games::push_old_games(&pool, &influx_client, &mut mongo_client, key, value) + let sparta_games = + HashMap::from([("tunguska", "bf1"), ("casablanca", "bfv"), ("bf4", "bf4")]); + for (key, value) in sparta_games.into_iter() { + match companion::gather_companion( + &pool, + &influx_client, + sessions.get(key).unwrap_or(&empty_game_hash).to_owned(), + cookie.clone(), + key, + value, + ) .await - { - Ok(game_result) => { - game_results.insert(key.to_string(), game_result); - } - Err(e) => { - log::error!("Failed oldgame: {}, with reason: {:#?}", key, e); - failed_games.push(key); - } - }; - } - log::info!("oldgames done"); + { + Ok((session, platform_result)) => { + sessions.insert(key.to_string(), session); + game_results.insert(key.to_string(), platform_result); + } + Err(e) => { + log::error!("Failed sparta_game: {}, with reason: {:#?}", key, e); + failed_games.push(key); + } + }; + } + log::info!("sparta done"); + + // pc only! + let battlelog_games = HashMap::from([ + ( + "bf3", + "https://battlelog.battlefield.com/bf3/servers/getAutoBrowseServers/", + ), + ( + "bf4", + "https://battlelog.battlefield.com/bf4/servers/getServers/pc/", + ), + ( + "bfh", + "https://battlelog.battlefield.com/bfh/servers/getServers/pc/", + ), + ]); + for (key, value) in battlelog_games { + match battlelog::gather_battlelog(&pool, &influx_client, key, value).await { + Ok(game_result) => { + game_results.insert(key.to_string(), game_result); + } + Err(e) => { + log::error!("Failed battlelog_game: {}, with reason: {:#?}", key, e); + failed_games.push(key); + } + }; + } + log::info!("battlelog done"); - let sparta_games = - HashMap::from([("tunguska", "bf1"), ("casablanca", "bfv"), ("bf4", "bf4")]); - for (key, value) in sparta_games.into_iter() { - match companion::gather_companion( + let run_detailed = + last_ran_detailed.add(chrono::Duration::hours(12)) <= chrono::Utc::now(); + if run_detailed { + log::info!("Running grpc detailed"); + last_ran_detailed = chrono::Utc::now(); + } + + match battlefield_grpc::gather_grpc( &pool, &influx_client, - sessions.get(key).unwrap_or(&empty_game_hash).to_owned(), + sessions + .get("kingston") + .unwrap_or(&empty_game_hash) + .to_owned(), cookie.clone(), - key, - value, + run_detailed, ) .await { - Ok((session, platform_result)) => { - sessions.insert(key.to_string(), session); - game_results.insert(key.to_string(), platform_result); + Ok((session, game_result)) => { + sessions.insert("kingston".to_string(), session); + game_results.insert("kingston".to_string(), game_result); } Err(e) => { - log::error!("Failed sparta_game: {}, with reason: {:#?}", key, e); - failed_games.push(key); + log::error!("Failed kingston_grpc, with reason: {:#?}", e); + failed_games.push("kingston"); } }; - } - log::info!("sparta done"); + log::info!("grpc done"); - // pc only! - let battlelog_games = HashMap::from([ - ( - "bf3", - "https://battlelog.battlefield.com/bf3/servers/getAutoBrowseServers/", - ), - ( - "bf4", - "https://battlelog.battlefield.com/bf4/servers/getServers/pc/", - ), - ( - "bfh", - "https://battlelog.battlefield.com/bfh/servers/getServers/pc/", - ), - ]); - for (key, value) in battlelog_games { - match battlelog::gather_battlelog(&pool, &influx_client, key, value).await { + match bf1_marne::push_bf1_marne(&pool, &influx_client).await { Ok(game_result) => { - game_results.insert(key.to_string(), game_result); + game_results.insert("bf1_marne".to_string(), game_result); } Err(e) => { - log::error!("Failed battlelog_game: {}, with reason: {:#?}", key, e); - failed_games.push(key); + log::error!("BF1 Marne failed with reason: {:#?}", e); } }; - } - log::info!("battlelog done"); + log::info!("BF1 Marne done"); - let run_detailed = last_ran_detailed.add(chrono::Duration::hours(12)) <= chrono::Utc::now(); - if run_detailed { - log::info!("Running grpc detailed"); - last_ran_detailed = chrono::Utc::now(); - } + // if no games failed, make global array + if failed_games.iter().any(|&value| { + vec!["bf3", "bf4", "bfh", "tunguska", "casablanca", "kingston"].contains(&value) + }) { + log::error!("1 of the important games failed to gather, skipping global array..."); + } else { + let global_result = + results::combine_region_players("global", "global", &game_results).await; - match battlefield_grpc::gather_grpc( - &pool, - &influx_client, - sessions - .get("kingston") - .unwrap_or(&empty_game_hash) - .to_owned(), - cookie.clone(), - run_detailed, - ) - .await - { - Ok((session, game_result)) => { - sessions.insert("kingston".to_string(), session); - game_results.insert("kingston".to_string(), game_result); - } - Err(e) => { - log::error!("Failed kingston_grpc, with reason: {:#?}", e); - failed_games.push("kingston"); + // influx + match influx_db::push_totals(&influx_client, &global_result).await { + Ok(_) => log::info!("successfully made global array"), + Err(e) => log::error!("Failed to push global games array: {:#?}", e), + }; } - }; - log::info!("grpc done"); + log::info!("global done"); - match bf1_marne::push_bf1_marne(&pool, &influx_client).await { - Ok(game_result) => { - game_results.insert("bf1_marne".to_string(), game_result); - } - Err(e) => { - log::error!("BF1 Marne failed with reason: {:#?}", e); - } - }; - log::info!("BF1 Marne done"); + match battlebit::push_battlebit(&pool, &influx_client).await { + Ok(_) => {} + Err(e) => { + log::error!("Battlebit failed with reason: {:#?}", e); + } + }; + log::info!("Battlebit done"); - // if no games failed, make global array - if failed_games.iter().any(|&value| { - vec!["bf3", "bf4", "bfh", "tunguska", "casablanca", "kingston"].contains(&value) - }) { - log::error!("1 of the important games failed to gather, skipping global array..."); + last_update.store( + chrono::Utc::now().timestamp() / 60, + atomic::Ordering::Relaxed, + ); } else { - let global_result = - results::combine_region_players("global", "global", &game_results).await; - - // influx - match influx_db::push_totals(&influx_client, &global_result).await { - Ok(_) => log::info!("successfully made global array"), - Err(e) => log::error!("Failed to push global games array: {:#?}", e), - }; + let ten_mins = last_ran + chrono::Duration::minutes(10); + log::info!( + "Waiting {:#?} minutes before running again", + (ten_mins - last_ran).num_minutes() + ); + sleep(Duration::from_secs(30)).await; } - log::info!("global done"); - - match battlebit::push_battlebit(&pool, &influx_client).await { - Ok(_) => {} - Err(e) => { - log::error!("Battlebit failed with reason: {:#?}", e); - } - }; - log::info!("Battlebit done"); - - last_update.store( - chrono::Utc::now().timestamp() / 60, - atomic::Ordering::Relaxed, - ); } }