diff options
| author | Daniel <kingdread@gmx.de> | 2018-10-15 17:32:22 +0200 | 
|---|---|---|
| committer | Daniel <kingdread@gmx.de> | 2018-10-15 17:32:22 +0200 | 
| commit | d50fdaa06f3509696d04cdd564ebb9b6265c46c4 (patch) | |
| tree | 5b8678bbc18eeaa2bdfd8cfa96a9bb74147f864a | |
| parent | 46e99ad65d41a9f03bcefb6bc17b3fa00bca6b79 (diff) | |
| download | raidgrep-d50fdaa06f3509696d04cdd564ebb9b6265c46c4.tar.gz raidgrep-d50fdaa06f3509696d04cdd564ebb9b6265c46c4.tar.bz2 raidgrep-d50fdaa06f3509696d04cdd564ebb9b6265c46c4.zip | |
add time based filtering
This accepts timestamps in the following formats:
* Human-readable, like "15d", taken relative to the current time.
* rfc3339-like "2018-03-14 13:13:00"
More formats might be added in the future.
| -rw-r--r-- | Cargo.lock | 10 | ||||
| -rw-r--r-- | Cargo.toml | 1 | ||||
| -rw-r--r-- | src/filters.rs | 14 | ||||
| -rw-r--r-- | src/main.rs | 39 | 
4 files changed, 62 insertions, 2 deletions
| @@ -194,6 +194,14 @@ dependencies = [  ]  [[package]] +name = "humantime" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]]  name = "lazy_static"  version = "1.1.0"  source = "registry+https://github.com/rust-lang/crates.io-index" @@ -334,6 +342,7 @@ dependencies = [   "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",   "colored 1.6.1 (registry+https://github.com/rust-lang/crates.io-index)",   "evtclib 0.1.0", + "humantime 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)",   "num-traits 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",   "quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)",   "rayon 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -592,6 +601,7 @@ dependencies = [  "checksum flate2 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "37847f133aae7acf82bb9577ccd8bda241df836787642654286e79679826a54b"  "checksum fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "2fad85553e09a6f881f739c29f0b00b0f01357c743266d478b68951ce23285f3"  "checksum getset 0.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "54c7f36a235738bb25904d6a2b3dbb28f6f5736cd3918c4bf80d6bb236200782" +"checksum humantime 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0484fda3e7007f2a4a0d9c3a703ca38c71c54c55602ce4660c419fd32e188c9e"  "checksum lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ca488b89a5657b0a2ecd45b95609b3e848cf1755da332a0da46e2b2b1cb371a7"  "checksum libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)" = "76e3a3ef172f1a0b9a9ff0dd1491ae5e6c948b94479a3021819ba7d860c8645d"  "checksum memchr 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a3b4142ab8738a78c51896f704f83c11df047ff1bda9a92a661aa6361552d93d" @@ -14,3 +14,4 @@ colored = "1"  chrono = "0.4"  rayon = "1"  num-traits = "0.2" +humantime = "1.1" diff --git a/src/filters.rs b/src/filters.rs index 1549c26..c6df6e8 100644 --- a/src/filters.rs +++ b/src/filters.rs @@ -31,3 +31,17 @@ pub fn filter_outcome(result: &LogResult, opt: &Opt) -> bool {          _ => false,      }  } + +/// Do filtering based on encounter time. +pub fn filter_time(result: &LogResult, opt: &Opt) -> bool { +    let after_ok = match opt.after { +        Some(time) => time <= result.time, +        None => true, +    }; +    let before_ok = match opt.before { +        Some(time) => time >= result.time, +        None => true, +    }; + +    after_ok && before_ok +} diff --git a/src/main.rs b/src/main.rs index d026776..c05a8b6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ extern crate quick_error;  extern crate chrono;  extern crate colored;  extern crate evtclib; +extern crate humantime;  extern crate num_traits;  extern crate rayon;  extern crate regex; @@ -14,7 +15,7 @@ use std::io::{self, BufReader};  use std::path::PathBuf;  use std::str::FromStr; -use chrono::NaiveDateTime; +use chrono::{Duration, NaiveDateTime};  use num_traits::cast::FromPrimitive;  use regex::Regex;  use structopt::StructOpt; @@ -85,6 +86,22 @@ pub struct Opt {      #[structopt(long = "no-color")]      no_color: bool, +    /// Only show logs that are younger than the given time. +    #[structopt( +        short = "a", +        long = "younger", +        parse(try_from_str = "parse_time_arg") +    )] +    after: Option<NaiveDateTime>, + +    /// Only show logs that are older than the given time. +    #[structopt( +        short = "b", +        long = "older", +        parse(try_from_str = "parse_time_arg") +    )] +    before: Option<NaiveDateTime>, +      /// Print more debugging information to stderr.      #[structopt(long = "debug")]      debug: bool, @@ -179,6 +196,22 @@ impl FromStr for FightOutcome {      }  } +fn parse_time_arg(input: &str) -> Result<NaiveDateTime, &'static str> { +    if let Ok(duration) = humantime::parse_duration(input) { +        let now = chrono::Local::now().naive_local(); +        let chrono_dur = Duration::from_std(duration).expect("Duration out of range!"); +        return Ok(now - chrono_dur); +    } +    if let Ok(time) = humantime::parse_rfc3339_weak(input) { +        let timestamp = time +            .duration_since(std::time::SystemTime::UNIX_EPOCH) +            .unwrap() +            .as_secs(); +        return Ok(NaiveDateTime::from_timestamp(timestamp as i64, 0)); +    } +    Err("unknown time format") +} +  fn main() {      let opt = Opt::from_args(); @@ -254,7 +287,9 @@ fn search_log(entry: &DirEntry, opt: &Opt) -> Result<Option<LogResult>, RuntimeE      let info = extract_info(entry, &log); -    let take_log = filters::filter_name(&log, opt) && filters::filter_outcome(&info, opt); +    let take_log = filters::filter_name(&log, opt) +        && filters::filter_outcome(&info, opt) +        && filters::filter_time(&info, opt);      if take_log {          Ok(Some(info)) | 
