Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support querying multiple time strings #55

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/bin/monocle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ enum Commands {
Time {
/// Time stamp or time string to convert
#[clap()]
time: Option<String>,
time: Vec<String>,

/// Simple output, only print the converted time
#[clap(short, long)]
Expand Down
2 changes: 1 addition & 1 deletion src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl MsgStore {
ElemType::ANNOUNCE => "A",
ElemType::WITHDRAW => "W",
};
let origin_string = elem.origin_asns.as_ref().map(|asns| asns.get(0).unwrap());
let origin_string = elem.origin_asns.as_ref().map(|asns| asns.first().unwrap());
format!(
"('{}','{}','{}','{}','{}', {},{},{},{},{},{},{},'{}',{},{})",
elem.timestamp as u32,
Expand Down
85 changes: 46 additions & 39 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod datasets;

use anyhow::{anyhow, Result};
use bgpkit_parser::BgpkitParser;
use chrono::{NaiveDateTime, TimeZone, Utc};
use chrono::{DateTime, TimeZone, Utc};
use chrono_humanize::HumanTime;
use itertools::Itertools;
use std::io::Read;
Expand All @@ -23,7 +23,7 @@ pub fn parser_with_filters(
prefix: &Option<String>,
include_super: &bool,
include_sub: &bool,
peer_ip: &Vec<IpAddr>,
peer_ip: &[IpAddr],
peer_asn: &Option<u32>,
elem_type: &Option<String>,
start_ts: &Option<String>,
Expand Down Expand Up @@ -101,54 +101,61 @@ pub fn string_to_time(time_string: &str) -> Result<i64> {
Ok(ts)
}

pub fn convert_time_string(time_string: &Option<String>) -> Result<String> {
let unix = match time_string {
None => Utc::now().to_rfc3339(),
Some(ts) => {
pub fn convert_time_string(time_vec: &[String]) -> Result<String> {
let time_strings = match time_vec.len() {
0 => vec![Utc::now().to_rfc3339()],
_ => {
// check if ts is a valid Unix timestamp
match ts.parse::<f64>() {
Ok(timestamp) => {
let dt = Utc.timestamp_opt(timestamp as i64, 0).unwrap();
dt.to_rfc3339()
}
Err(_) => {
// not a time stamp, check if it is a valid RFC3339 string,
// if so, return the unix timestamp as string; otherwise, return error
match chrono::DateTime::parse_from_rfc3339(ts) {
Ok(dt) => dt.timestamp().to_string(),
time_vec
.iter()
.map(|ts| {
match ts.parse::<f64>() {
Ok(timestamp) => {
let dt = Utc.timestamp_opt(timestamp as i64, 0).unwrap();
dt.to_rfc3339()
}
Err(_) => {
return Err(anyhow!(
"Input time must be either Unix timestamp or time string compliant with RFC3339"
))
// not a time stamp, check if it is a valid RFC3339 string,
// if so, return the unix timestamp as string; otherwise, return error
match chrono::DateTime::parse_from_rfc3339(ts) {
Ok(dt) => dt.timestamp().to_string(),
Err(_) => "".to_string(),
}
}
}
}
}
})
.collect()
}
};

Ok(unix)
Ok(time_strings.join("\n"))
}

pub fn time_to_table(time_string: &Option<String>) -> Result<String> {
pub fn time_to_table(time_vec: &[String]) -> Result<String> {
let now_ts = Utc::now().timestamp();
let unix = match time_string {
None => now_ts,
Some(ts) => string_to_time(ts.as_str())?,
let ts_vec = match time_vec.is_empty() {
true => vec![now_ts],
false => time_vec
.iter()
.map(|ts| string_to_time(ts.as_str()).unwrap_or_default())
.collect(),
};

let ht = HumanTime::from(chrono::Local::now() - chrono::Duration::seconds(now_ts - unix));
let human = ht.to_string();

let rfc3339 = Utc
.from_utc_datetime(&NaiveDateTime::from_timestamp_opt(unix, 0).unwrap())
.to_rfc3339();
let bgptime_vec = ts_vec
.into_iter()
.map(|ts| {
let ht = HumanTime::from(chrono::Local::now() - chrono::Duration::seconds(now_ts - ts));
let human = ht.to_string();
let rfc3339 = Utc
.from_utc_datetime(&DateTime::from_timestamp(ts, 0).unwrap().naive_utc())
.to_rfc3339();
BgpTime {
unix: ts,
rfc3339,
human,
}
})
.collect::<Vec<BgpTime>>();

Ok(Table::new(vec![BgpTime {
unix,
rfc3339,
human,
}])
.with(Style::rounded())
.to_string())
Ok(Table::new(bgptime_vec).with(Style::rounded()).to_string())
}