1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145

mod date_filter;
mod status_filter;
mod str_filter;
mod method_filter;

pub use {
    date_filter::*,
    status_filter::*,
    str_filter::*,
    method_filter::*,
};

use {
    crate::*,
    anyhow::*,
    smallvec::*,
    std::{
        str::FromStr,
    },
};

pub enum Filter {
    Date(DateFilter),
    Ip(StrFilter),
    Method(MethodFilter),
    Path(StrFilter),
    Referer(StrFilter),
    Status(StatusFilter),
}

impl Filter {
    pub fn accepts(&self, line: &LogLine) -> bool {
        match self {
            Self::Date(f) => f.contains(line.date),
            Self::Ip(f) => f.accepts(&line.remote_addr),
            Self::Method(f) => f.contains(line.method),
            Self::Path(f) => f.accepts(&line.path),
            Self::Referer(f) => f.accepts(&line.referer),
            Self::Status(f) => f.accepts(line.status),
        }
    }
    pub fn field_name(&self) -> &'static str {
        match self {
            Self::Date(_) => "date",
            Self::Ip(_) => "remote address",
            Self::Method(_) => "method",
            Self::Path(_) => "path",
            Self::Referer(_) => "referer", // it looks like it's the usual orthograph
            Self::Status(_) => "status",
        }
    }
}

pub struct Filtering {
    pub pattern: String,
    pub filter: Filter,
    pub removed_count: usize,
}

impl Filtering {
    pub fn new(pattern: &str, filter: Filter) -> Self {
        Self {
            pattern: pattern.to_string(),
            filter,
            removed_count: 0,
        }
    }
}

pub struct Filterer {
    pub first_date: Date,
    pub filterings: SmallVec<[Filtering; 5]>,
}

impl Filterer {
    pub fn new(
        args: &args::Args,
        first_date: Date,
        last_date: Date,
    ) -> Result<Self> {
        let (default_year, default_month) = unique_year_month(first_date, last_date);
        let mut filterings = SmallVec::new();
        if let Some(s) = &args.date {
            filterings.push(Filtering::new(
                s,
                Filter::Date(DateFilter::new(s, default_year, default_month)?),
            ));
        }
        if let Some(s) = &args.ip {
            filterings.push(Filtering::new(
                s,
                Filter::Ip(StrFilter::new(s)?),
            ));
        }
        if let Some(s) = &args.method {
            filterings.push(Filtering::new(
                s,
                Filter::Method(MethodFilter::from_str(s)),
            ));
        }
        if let Some(s) = &args.path {
            filterings.push(Filtering::new(
                s,
                Filter::Path(StrFilter::new(s)?),
            ));
        }
        if let Some(s) = &args.referer {
            filterings.push(Filtering::new(
                s,
                Filter::Referer(StrFilter::new(s)?),
            ));
        }
        if let Some(s) = &args.status {
            filterings.push(Filtering::new(
                s,
                Filter::Status(StatusFilter::from_str(s)?),
            ));
        }
        Ok(Self { first_date, filterings })
    }
    pub fn date_filter(&self) -> Option<&DateFilter> {
        for i in 0..self.filterings.len() {
            match &self.filterings[i].filter {
                Filter::Date(f) => {
                    return Some(&f);
                }
                _ => {},
            }
        }
        None
    }
    pub fn accepts(&mut self, line: &LogLine) -> bool {
        for filtering in &mut self.filterings {
            if !filtering.filter.accepts(line) {
                filtering.removed_count += 1;
                return false;
            }
        }
        true
    }
    pub fn has_filters(&self) -> bool {
        !self.filterings.is_empty()
    }
}