|
| 1 | +extern crate flate2; |
| 2 | +extern crate serde; |
| 3 | +extern crate serde_json; |
| 4 | +extern crate time; |
| 5 | + |
| 6 | +#[macro_use] |
| 7 | +extern crate serde_derive; |
| 8 | +extern crate nom; |
| 9 | + |
| 10 | +mod user_agent_parser; |
| 11 | + |
| 12 | +#[derive(Deserialize, Debug)] |
| 13 | +struct Request { |
| 14 | + timestamp: String, |
| 15 | + // time_elapsed: u8, |
| 16 | + // client_ip: String, |
| 17 | + // client_continent: String, |
| 18 | + // client_country: String, |
| 19 | + // client_region: String, |
| 20 | + // client_city: String, |
| 21 | + // client_latitude: String, |
| 22 | + // client_longitude: String, |
| 23 | + client_timezone: String, |
| 24 | + // client_connection: String, |
| 25 | + // request: String, |
| 26 | + // request_host: String, |
| 27 | + request_path: String, |
| 28 | + // request_query: String, |
| 29 | + // request_bytes: u16, |
| 30 | + user_agent: String, |
| 31 | + http2: bool, |
| 32 | + tls: bool, |
| 33 | + tls_version: String, |
| 34 | + tls_cipher: String, |
| 35 | + // response_status: String, |
| 36 | + // response_text: String, |
| 37 | + // response_bytes: u16, |
| 38 | + // response_cache: String, |
| 39 | + // cache_state: String, |
| 40 | + // cache_lastuse: f32, |
| 41 | + // cache_hits: u16, |
| 42 | + // server_region: String, |
| 43 | + // server_datacenter: String, |
| 44 | +} |
| 45 | + |
| 46 | +use flate2::read::GzDecoder; |
| 47 | +use std::error::Error; |
| 48 | +use std::fs::File; |
| 49 | +use std::io::BufRead; |
| 50 | +use std::io::BufReader; |
| 51 | +use std::path::Path; |
| 52 | + |
| 53 | +fn read_file() -> BufReader<GzDecoder<File>> { |
| 54 | + // Create a path to the desired file |
| 55 | + let path = Path::new("test/sample_10.log.gz"); |
| 56 | + let display = path.display(); |
| 57 | + |
| 58 | + // Open the path in read-only mode, returns `io::Result<File>` |
| 59 | + let file = match File::open(&path) { |
| 60 | + // The `description` method of `io::Error` returns a string that |
| 61 | + // describes the error |
| 62 | + Err(why) => panic!("couldn't open {}: {}", display, why.description()), |
| 63 | + Ok(file) => file, |
| 64 | + }; |
| 65 | + |
| 66 | + let d = GzDecoder::new(file); |
| 67 | + BufReader::new(d) |
| 68 | +} |
| 69 | + |
| 70 | +fn main() { |
| 71 | + let file = read_file(); |
| 72 | + for line in file.lines() { |
| 73 | + let r: Request = serde_json::from_str(&line.unwrap()).unwrap(); |
| 74 | + let t = time::strptime(&r.timestamp, "%F %T").unwrap(); |
| 75 | + print!("{}: {:?}\n\n", t.rfc3339(), r.user_agent) |
| 76 | + } |
| 77 | +} |
0 commit comments