Skip to content

Commit

Permalink
determine wait from periods
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerboa-app committed May 17, 2024
1 parent 4123981 commit cd86242
Showing 1 changed file with 40 additions and 48 deletions.
88 changes: 40 additions & 48 deletions src/server/stats/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use std::{cmp::min, sync::Arc};

use chrono::{Datelike, TimeZone};
use tokio::sync::Mutex;

use crate::{config::{read_config, CONFIG_PATH}, integrations::discord::post::post};

use self::{digest::{digest_message, process_hits}, hits::{archive, save, HitStats}};
use self::{digest::{digest_message, process_hits}, hits::{save, HitStats}};

pub mod hits;
pub mod digest;
Expand All @@ -15,61 +14,54 @@ pub async fn stats_thread(state: Arc<Mutex<HitStats>>)
loop
{

let t = chrono::offset::Utc::now();

{
let mut stats = state.lock().await;

let config = match read_config(CONFIG_PATH)
{
Some(c) => c,
None =>
{
std::process::exit(1)
}
};
let time: chrono::prelude::DateTime<chrono::prelude::Utc> = chrono::offset::Utc::now();

let stats_config = config.stats;
let mut stats = state.lock().await;

if (t - stats.last_save).num_seconds() > stats_config.save_period_seconds as i64
let config = match read_config(CONFIG_PATH)
{
Some(c) => c,
None =>
{
save(&mut stats);
std::process::exit(1)
}
};

if (t - stats.last_digest).num_seconds() > stats_config.digest_period_seconds as i64
{
stats.summary = process_hits(stats_config.path.clone(), Some(stats.last_digest), None, stats_config.top_n_digest, Some(stats.to_owned()));
let msg = digest_message(stats.summary.clone(), Some(stats.last_digest), None);
match config.notification_endpoint
{
Some(endpoint) => match post(&endpoint, msg).await
{
Ok(_s) => (),
Err(e) => {crate::debug(format!("Error posting to discord\n{}", e), None);}
},
None => ()
}
stats.last_digest = t;
}
let stats_config = config.stats;

let time_until_save = (time - stats.last_save).num_seconds() - stats_config.save_period_seconds as i64;
let time_until_digest = (time - stats.last_digest).num_seconds() - stats_config.digest_period_seconds as i64;

if (t - stats.last_clear).num_seconds() > stats_config.log_files_clear_period_seconds as i64
if time_until_save <= 0
{
save(&mut stats);
}

if time_until_digest <= 0
{
stats.summary = process_hits(stats_config.path.clone(), Some(stats.last_digest), None, stats_config.top_n_digest, Some(stats.to_owned()));
let msg = digest_message(stats.summary.clone(), Some(stats.last_digest), None);
match config.notification_endpoint
{
archive();
stats.last_clear = t;
Some(endpoint) => match post(&endpoint, msg).await
{
Ok(_s) => (),
Err(e) => {crate::debug(format!("Error posting to discord\n{}", e), None);}
},
None => ()
}
stats.last_digest = time;
}

let wait = min(3600, (chrono::Utc::with_ymd_and_hms
(
&chrono::Utc,
t.year(),
t.month(),
t.day(),
1,
0,
0
).unwrap() + chrono::Duration::days(1) - t).num_seconds()) as u64;
crate::debug(format!("Sleeping for {}", wait), Some("Statistics".to_string()));
tokio::time::sleep(std::time::Duration::from_secs(wait)).await;
let time_left = min(time_until_digest, time_until_save);

let wait_seconds = match time_left > 0
{
true => time_left as u64,
false => min(stats_config.save_period_seconds, stats_config.digest_period_seconds)
};

crate::debug(format!("Sleeping for {}", wait_seconds), Some("Statistics".to_string()));
tokio::time::sleep(std::time::Duration::from_secs(wait_seconds)).await;
}
}

0 comments on commit cd86242

Please sign in to comment.