-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
205 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
mod query_log; | ||
mod query_logs; | ||
mod usage_stats; | ||
|
||
pub use query_log::*; | ||
pub use query_logs::*; | ||
pub use usage_stats::*; | ||
|
||
use crate::tasks::dnstap::read_dnstap_logs; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct LogsConsumer { | ||
logs_store: QueryLogs, | ||
usage_stats: UsageStats, | ||
} | ||
|
||
impl LogsConsumer { | ||
pub fn new(logs_store: QueryLogs, usage_stats: UsageStats) -> Self { | ||
Self { | ||
logs_store, | ||
usage_stats, | ||
} | ||
} | ||
|
||
pub async fn ingest_logs_from_file(&self) { | ||
tracing::trace!("LogsStore remove_expired_logs"); | ||
self.logs_store.remove_expired_logs(); | ||
tracing::trace!("LogsStore remove_expired_logs. DONE"); | ||
|
||
tracing::trace!("LogsStore read_dnstap_logs"); | ||
let content = read_dnstap_logs().await; | ||
let content_len = content.len(); | ||
tracing::trace!("LogsStore read_dnstap_logs. DONE, content_len={content_len}"); | ||
|
||
tracing::trace!("LogsStore extract_query_logs"); | ||
let logs_hash_map = extract_query_logs(&content); | ||
let logs_hash_map_len = logs_hash_map.len(); | ||
tracing::trace!( | ||
"LogsStore extract_query_logs. DONE, logs_hash_map_len={logs_hash_map_len}" | ||
); | ||
|
||
tracing::trace!("LogsStore logs_hash_map"); | ||
self.logs_store.merge_logs(&logs_hash_map); | ||
self.logs_store.remove_expired_logs(); | ||
|
||
self.usage_stats.merge_logs(&logs_hash_map); | ||
tracing::trace!("LogsStore logs_hash_map. DONE"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use std::{ | ||
collections::HashMap, | ||
sync::{Arc, Mutex}, | ||
}; | ||
|
||
use chrono::{Duration, Utc}; | ||
|
||
use super::QueryLog; | ||
|
||
#[derive(Debug, Clone, Default)] | ||
pub struct QueryLogs { | ||
logs_store: Arc<Mutex<HashMap<String, Vec<QueryLog>>>>, | ||
} | ||
|
||
impl QueryLogs { | ||
pub fn remove_expired_logs(&self) { | ||
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() { | ||
query_logs.retain(|q| q.query_time > query_time_cutoff); | ||
} | ||
} | ||
|
||
pub fn merge_logs(&self, logs_hash_map: &HashMap<String, Vec<QueryLog>>) { | ||
let mut logs_store_guard = self.logs_store.lock().unwrap(); | ||
for (ip, logs) in logs_hash_map.iter() { | ||
match logs_store_guard.get_mut(ip) { | ||
Some(existing_logs) => { | ||
existing_logs.extend(logs.clone()); | ||
} | ||
None => { | ||
logs_store_guard.insert(ip.to_string(), logs.clone()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub fn get_logs_for_ip(&self, ip: &str) -> Vec<QueryLog> { | ||
match self.logs_store.lock().unwrap().get(ip).cloned() { | ||
Some(logs) => logs, | ||
None => Vec::new(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use std::{ | ||
collections::HashMap, | ||
sync::{Arc, Mutex}, | ||
}; | ||
|
||
use chrono::{DateTime, Duration, Utc}; | ||
|
||
use super::QueryLog; | ||
|
||
#[derive(Debug, Clone, Default)] | ||
pub struct UsageStats { | ||
last_query_times: 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(); | ||
|
||
for (ip, queries) in logs_hash_map.iter() { | ||
let Some(last_qt) = queries.last().map(|q| q.query_time) else { | ||
continue; | ||
}; | ||
|
||
match last_query_times.get_mut(ip) { | ||
Some(qt) => *qt = last_qt, | ||
None => { | ||
last_query_times.insert(ip.to_string(), last_qt); | ||
} | ||
} | ||
} | ||
|
||
*self.last_query_times.lock().unwrap() = last_query_times; | ||
} | ||
|
||
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() | ||
} | ||
} |
Oops, something went wrong.