diff --git a/src/logs/mod.rs b/src/logs/mod.rs index a870c6d..1d8348a 100644 --- a/src/logs/mod.rs +++ b/src/logs/mod.rs @@ -44,6 +44,7 @@ impl LogsConsumer { self.logs_store.remove_expired_logs(); self.usage_stats.merge_logs(&logs_hash_map); + self.usage_stats.remove_old_query_times(); tracing::trace!("LogsStore logs_hash_map. DONE"); } } diff --git a/src/logs/query_logs.rs b/src/logs/query_logs.rs index ec1ca20..19ff07e 100644 --- a/src/logs/query_logs.rs +++ b/src/logs/query_logs.rs @@ -14,12 +14,14 @@ pub struct QueryLogs { impl QueryLogs { pub fn remove_expired_logs(&self) { - let query_time_cutoff = Utc::now() - Duration::minutes(10); + let query_time_cutoff = Utc::now() - Duration::minutes(2); let mut logs_store_guard = self.logs_store.lock().unwrap(); for query_logs in logs_store_guard.values_mut() { query_logs.retain(|q| q.query_time > query_time_cutoff); } + + logs_store_guard.retain(|_ip, queries| !queries.is_empty()); } pub fn merge_logs(&self, logs_hash_map: &HashMap>) { diff --git a/src/logs/usage_stats.rs b/src/logs/usage_stats.rs index 043b1da..745fb8c 100644 --- a/src/logs/usage_stats.rs +++ b/src/logs/usage_stats.rs @@ -9,12 +9,12 @@ use super::QueryLog; #[derive(Debug, Clone, Default)] pub struct UsageStats { - last_query_times: Arc>>>, + active_ips_one_day: Arc>>>, } impl UsageStats { pub fn merge_logs(&self, logs_hash_map: &HashMap>) { - let mut last_query_times = self.last_query_times.lock().unwrap().clone(); + let mut last_query_times = self.active_ips_one_day.lock().unwrap().clone(); for (ip, queries) in logs_hash_map.iter() { let Some(last_qt) = queries.last().map(|q| q.query_time) else { @@ -29,17 +29,16 @@ impl UsageStats { } } - *self.last_query_times.lock().unwrap() = last_query_times; + *self.active_ips_one_day.lock().unwrap() = last_query_times; + } + + pub fn remove_old_query_times(&self) { + let time_cutoff = Utc::now() - Duration::days(1); + let mut active_ips_one_day = self.active_ips_one_day.lock().unwrap(); + active_ips_one_day.retain(|_ip, qt| *qt > time_cutoff); } pub fn get_active_ips_in_last_day(&self) -> usize { - let one_day = Utc::now() - Duration::days(1); - - self.last_query_times - .lock() - .unwrap() - .iter() - .filter(|(_, qt)| **qt > one_day) - .count() + self.active_ips_one_day.lock().unwrap().len() } }