diff options
author | Daniel <kingdread@gmx.de> | 2020-06-12 00:48:18 +0200 |
---|---|---|
committer | Daniel <kingdread@gmx.de> | 2020-06-12 00:48:18 +0200 |
commit | d4a24eef7fd410c147de201d776089e0601317d5 (patch) | |
tree | 8311b261c20d6d5f2d817f63e2cf4a3b213cdd34 /src/filters/values.rs | |
parent | 1fb3d3259d23410f8bf9879f64de880a11e4f876 (diff) | |
download | raidgrep-d4a24eef7fd410c147de201d776089e0601317d5.tar.gz raidgrep-d4a24eef7fd410c147de201d776089e0601317d5.tar.bz2 raidgrep-d4a24eef7fd410c147de201d776089e0601317d5.zip |
initial work on comparison based filters
This enables filters such as
-time > 2020-01-01
-time < 2020-02-03
...
for time and duration, and later possibly also for more things (such as
a COUNT(...) construct).
This work tries to integrate them into the existing filter system as
seamless as possible, by providing a Comparator which implements
LogFilter.
The "type checking" is done at parse time, so nonsensical comparisons
like -time > 12s flat out give a parse error. This however might be
changed to a more dynamic system with run-time type checking, in which
case we could do away with the type parameter on Producer and simply
work with a generic Value. The comparator would then return an error if
two non-identical types would be compared.
Note that the system does not support arithmetic expressions, only
simple comparisons to constant values.
Diffstat (limited to 'src/filters/values.rs')
-rw-r--r-- | src/filters/values.rs | 144 |
1 files changed, 144 insertions, 0 deletions
diff --git a/src/filters/values.rs b/src/filters/values.rs new file mode 100644 index 0000000..543b59c --- /dev/null +++ b/src/filters/values.rs @@ -0,0 +1,144 @@ +use std::{ + cmp::Ordering, + fmt::{self, Debug}, +}; + +use chrono::{DateTime, Duration, Utc}; + +use super::{log::LogFilter, Filter}; +use crate::{EarlyLogResult, LogResult}; + +pub trait Producer: Send + Sync + Debug { + type Output; + + fn produce_early(&self, _early_log: &EarlyLogResult) -> Option<Self::Output> { + None + } + + fn produce(&self, log: &LogResult) -> Self::Output; +} + +#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] +pub enum CompOp { + Less, + LessEqual, + Equal, + GreaterEqual, + Greater, +} + +impl fmt::Display for CompOp { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let symbol = match self { + CompOp::Less => "<", + CompOp::LessEqual => "<=", + CompOp::Equal => "=", + CompOp::GreaterEqual => ">=", + CompOp::Greater => ">", + }; + f.pad(symbol) + } +} + +impl CompOp { + pub fn matches(self, cmp: Ordering) -> bool { + match cmp { + Ordering::Less => self == CompOp::Less || self == CompOp::LessEqual, + Ordering::Equal => { + self == CompOp::LessEqual || self == CompOp::Equal || self == CompOp::GreaterEqual + } + Ordering::Greater => self == CompOp::Greater || self == CompOp::GreaterEqual, + } + } +} + +struct Comparator<V>( + Box<dyn Producer<Output = V>>, + CompOp, + Box<dyn Producer<Output = V>>, +); + +impl<V> Debug for Comparator<V> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "({:?} {} {:?})", self.0, self.1, self.2) + } +} + +impl<V> Filter<EarlyLogResult, LogResult> for Comparator<V> +where + V: Ord, +{ + fn filter(&self, log: &LogResult) -> bool { + let lhs = self.0.produce(log); + let rhs = self.2.produce(log); + self.1.matches(lhs.cmp(&rhs)) + } +} + +pub fn comparison<V: 'static>( + lhs: Box<dyn Producer<Output = V>>, + op: CompOp, + rhs: Box<dyn Producer<Output = V>>, +) -> Box<dyn LogFilter> +where + V: Ord, +{ + Box::new(Comparator(lhs, op, rhs)) +} + +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +struct ConstantProducer<V>(V); + +impl<V: Send + Sync + Debug + Clone> Producer for ConstantProducer<V> { + type Output = V; + fn produce_early(&self, _: &EarlyLogResult) -> Option<Self::Output> { + Some(self.0.clone()) + } + + fn produce(&self, _: &LogResult) -> Self::Output { + self.0.clone() + } +} + +pub fn constant<V: Send + Sync + Debug + Clone + 'static>( + value: V, +) -> Box<dyn Producer<Output = V>> { + Box::new(ConstantProducer(value)) +} + +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] +struct TimeProducer; + +impl Producer for TimeProducer { + type Output = DateTime<Utc>; + + fn produce_early(&self, early_log: &EarlyLogResult) -> Option<Self::Output> { + early_log + .log_file + .file_name() + .and_then(super::log::datetime_from_filename) + } + + fn produce(&self, log: &LogResult) -> Self::Output { + log.time + } +} + +pub fn time() -> Box<dyn Producer<Output = DateTime<Utc>>> { + Box::new(TimeProducer) +} + +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] +struct DurationProducer; + +impl Producer for DurationProducer { + type Output = Duration; + + fn produce(&self, log: &LogResult) -> Self::Output { + log.duration + } +} + +pub fn duration() -> Box<dyn Producer<Output = Duration>> { + Box::new(DurationProducer) +} |