Move filter logic from database/mod.rs to filter.rs

This commit is contained in:
ppom 2024-10-03 12:00:00 +02:00
commit 116e75f81f
2 changed files with 17 additions and 14 deletions

View file

@ -12,7 +12,7 @@ use serde::Deserialize;
use super::{
messages::{Match, Time, MAT},
Action, ActionFilter, Pattern, Patterns,
Action, ActionFilter, LogEntry, Pattern, Patterns,
};
use crate::{daemon::ExecsManagerInput, utils::parse_duration};
@ -73,10 +73,6 @@ impl Filter {
self.retry_duration
}
pub fn longuest_action_duration(&self) -> TimeDelta {
self.longuest_action_duration
}
pub fn setup(
&mut self,
stream_name: &str,
@ -215,6 +211,14 @@ impl Filter {
tx.send(ExecsManagerInput::Exec(mat)).unwrap();
}
}
pub fn is_outdated(entry: &LogEntry, now: &Time) -> bool {
if entry.exec {
entry.t + entry.f.longuest_action_duration < *now
} else {
entry.t + entry.f.retry_duration.unwrap_or_default() < *now
}
}
}
impl Display for Filter {

View file

@ -235,15 +235,14 @@ fn __rotate_db(
}
// Store match & store in db
#[allow(clippy::unwrap_used)] // 0 second is obviously less seconds than i64::MAX
if (!entry.exec && entry.t + entry.f.retry_duration().unwrap_or_default() > now)
|| (entry.exec && entry.t + entry.f.longuest_action_duration() > now)
{
// We loose subsecond precision when storing times, so we add those fake
// milliseconds to make sure each time is unique
entry.t += TimeDelta::new(0, millisecond_disambiguation_counter).unwrap();
millisecond_disambiguation_counter += 1;
if !Filter::is_outdated(&entry, &now) {
#[allow(clippy::unwrap_used)] // 0 second is obviously less than i64::MAX
{
// We loose subsecond precision when storing times, so we add those fake
// milliseconds to make sure each time is unique
entry.t += TimeDelta::new(0, millisecond_disambiguation_counter).unwrap();
millisecond_disambiguation_counter += 1;
}
if let Some(tx) = matches_tx {
debug!("DB sending match from DB: {:?}", entry.m);