Skip to content

Commit

Permalink
reduce logs and ip stats
Browse files Browse the repository at this point in the history
  • Loading branch information
ragibkl committed Sep 13, 2024
1 parent e2c33e7 commit 0bc766e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/logs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
4 changes: 3 additions & 1 deletion src/logs/query_logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Vec<QueryLog>>) {
Expand Down
21 changes: 10 additions & 11 deletions src/logs/usage_stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ use super::QueryLog;

#[derive(Debug, Clone, Default)]
pub struct UsageStats {
last_query_times: Arc<Mutex<HashMap<String, DateTime<Utc>>>>,
active_ips_one_day: Arc<Mutex<HashMap<String, DateTime<Utc>>>>,
}

impl UsageStats {
pub fn merge_logs(&self, logs_hash_map: &HashMap<String, Vec<QueryLog>>) {
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 {
Expand All @@ -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()
}
}

0 comments on commit 0bc766e

Please sign in to comment.