Skip to content

Commit

Permalink
update durations
Browse files Browse the repository at this point in the history
  • Loading branch information
ragibkl committed Sep 13, 2024
1 parent bece86a commit 2947a83
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/get_logs.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

<body>
<p>ip address: {{ip}}</p>
<p>active ips in last day: {{ active_ips_last_day }}</p>
<p>active ips (10 minutes): {{ active_ips }}</p>

<table>
<tr>
Expand Down
6 changes: 3 additions & 3 deletions src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct GetLogsApiOutput {
pub struct GetLogsOutput {
ip: String,
queries: Vec<QueryLog>,
active_ips_last_day: usize,
active_ips: usize,
}

#[derive(Clone)]
Expand Down Expand Up @@ -69,7 +69,7 @@ pub async fn get_logs(

let ip = get_ip(addr);
let queries = app_state.logs_store.get_logs_for_ip(&ip);
let active_ips_last_day = app_state.usage_stats.get_active_ips_in_last_day();
let active_ips = app_state.usage_stats.get_active_ips();

let reg = Handlebars::new();
let response = reg
Expand All @@ -78,7 +78,7 @@ pub async fn get_logs(
&GetLogsOutput {
ip,
queries,
active_ips_last_day,
active_ips,
},
)
.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/logs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +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();
self.usage_stats.remove_old_active_ips();
tracing::trace!("LogsStore logs_hash_map. DONE");
}
}
2 changes: 1 addition & 1 deletion src/logs/query_logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct QueryLogs {

impl QueryLogs {
pub fn remove_expired_logs(&self) {
let query_time_cutoff = Utc::now() - Duration::minutes(2);
let query_time_cutoff = Utc::now() - Duration::minutes(10);

let mut logs_store_guard = self.logs_store.lock().unwrap();
for query_logs in logs_store_guard.values_mut() {
Expand Down
16 changes: 8 additions & 8 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 {
active_ips_one_day: Arc<Mutex<HashMap<String, DateTime<Utc>>>>,
active_ips: 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.active_ips_one_day.lock().unwrap().clone();
let mut last_query_times = self.active_ips.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,16 +29,16 @@ impl UsageStats {
}
}

*self.active_ips_one_day.lock().unwrap() = last_query_times;
*self.active_ips.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();
pub fn remove_old_active_ips(&self) {
let time_cutoff = Utc::now() - Duration::minutes(10);
let mut active_ips_one_day = self.active_ips.lock().unwrap();
active_ips_one_day.retain(|_ip, qt| *qt > time_cutoff);
}

pub fn get_active_ips_in_last_day(&self) -> usize {
self.active_ips_one_day.lock().unwrap().len()
pub fn get_active_ips(&self) -> usize {
self.active_ips.lock().unwrap().len()
}
}

0 comments on commit 2947a83

Please sign in to comment.