Skip to content

Commit

Permalink
health check improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Sammers21 committed Feb 13, 2024
1 parent 79723ee commit 963fce4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
50 changes: 35 additions & 15 deletions health-check/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use std::{error::Error};
use std::error::Error;

use chrono::{NaiveDateTime};
use chrono::NaiveDateTime;
use clap::Parser;
use serde_json::Value;

use clap::Parser;
use region::region_name;

mod region;

#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
Expand All @@ -21,15 +24,28 @@ async fn main() -> Result<(), Box<dyn Error>> {
let chat_id = args.chat_id;
let token = args.token;
let no_update_limit_minutes = 3 * 60 + 30;
let date = last_updated().await?;
let now = chrono::Utc::now().naive_utc();
let diff = (now - date).num_minutes();
println!("Last updated: {} minutes ago", diff);
if diff > no_update_limit_minutes {
println!("{} diff > {} limit", diff, no_update_limit_minutes);
send_tg_notification(format!("No updates for `{} hours` and `{} minutes` in EU shuffles: [EU activity in shuffle](https://pvpq.net/eu/activity/shuffle)", diff / 60, diff % 60).as_str(), &chat_id, &token).await?;
} else {
println!("Everything is fine, not sending any notifications");
let regions = vec!["en-gb", "en-us"];
let brackets = vec!["shuffle", "2v2", "3v3", "rbg"];
let mut combinations: Vec<(&str, &str)> = vec![];
for region in regions.iter() {
for bracket in brackets.iter() {
combinations.push((region, bracket));
}
}
println!("Combinations: {:?}", combinations);
for (region, bracket) in combinations.iter() {
let date = last_updated(region, bracket).await?;
let now = chrono::Utc::now().naive_utc();
let diff = (now - date).num_minutes();
println!("Last updated: {} minutes ago", diff);
if diff > no_update_limit_minutes {
println!("{} diff > {} limit", diff, no_update_limit_minutes);
let region_name = region_name(region);
send_tg_notification(format!("No updates for `{} hours` and `{} minutes` in {} {} on pvpq\\.net: [{} activity in {}](https://pvpq.net/{}/activity/{})",
diff / 60, diff % 60, region_name, bracket, region_name, bracket, region_name, bracket).as_str(), &chat_id, &token).await?;
} else {
println!("Everything is fine, not sending any notifications");
}
}
Ok(())
}
Expand All @@ -42,17 +58,21 @@ async fn send_tg_notification(text: &str, chat_id: &str, token: &str) -> Result<
if resp.status().is_success() {
Ok(())
} else {
println!("Error sending notification {:?}", resp.text().await?);
Err("Error sending notification".into())
}
}

async fn last_updated() -> Result<NaiveDateTime, Box<dyn Error>> {
let resp = reqwest::get("https://pvpq.net/api/en-gb/activity/shuffle?page=1")
async fn last_updated(region: &str, bracket: &str) -> Result<NaiveDateTime, Box<dyn Error>> {
let url = format!("https://pvpq.net/api/{}/activity/{}?page=1", region, bracket);
let resp = reqwest::get(url)
.await?
.text()
.await?;
let parsed = serde_json::from_str::<Value>(&resp);
let timestamp = parsed.unwrap()["timestamp"].as_i64().unwrap();
let date = chrono::NaiveDateTime::from_timestamp_millis(timestamp).unwrap();
Ok(date)
}
}


7 changes: 7 additions & 0 deletions health-check/src/region.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pub(crate) fn region_name(region: &str) -> String {
match region {
"en-gb" => "EU",
"en-us" => "US",
_ => "Unknown",
}.to_string()
}

0 comments on commit 963fce4

Please sign in to comment.