use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
use std::fmt::{Display, Formatter};
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, Hash)]
#[cfg_attr(feature = "cli", derive(tabled::Tabled))]
pub struct BrokerItem {
pub ts_start: chrono::NaiveDateTime,
pub ts_end: chrono::NaiveDateTime,
pub collector_id: String,
pub data_type: String,
pub url: String,
pub rough_size: i64,
pub exact_size: i64,
}
impl BrokerItem {
pub fn is_rib(&self) -> bool {
self.data_type.as_str() == "rib"
}
}
#[allow(clippy::unwrap_used)]
impl Display for BrokerItem {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", serde_json::to_string(self).unwrap())
}
}
impl PartialOrd for BrokerItem {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for BrokerItem {
fn cmp(&self, other: &Self) -> Ordering {
self.ts_start
.cmp(&other.ts_start) .then(self.data_type.cmp(&other.data_type)) .then(self.collector_id.cmp(&other.collector_id)) }
}
#[cfg(test)]
mod tests {
use super::*;
use chrono::DateTime;
#[test]
fn test_sorting() {
let mut items = vec![
BrokerItem {
ts_start: DateTime::from_timestamp(10, 0).unwrap().naive_utc(),
ts_end: Default::default(),
collector_id: "rrc00".to_string(),
data_type: "updates".to_string(),
url: "".to_string(),
rough_size: 0,
exact_size: 0,
},
BrokerItem {
ts_start: DateTime::from_timestamp(9, 0).unwrap().naive_utc(),
ts_end: Default::default(),
collector_id: "rrc00".to_string(),
data_type: "updates".to_string(),
url: "".to_string(),
rough_size: 0,
exact_size: 0,
},
BrokerItem {
ts_start: DateTime::from_timestamp(10, 0).unwrap().naive_utc(),
ts_end: Default::default(),
collector_id: "rrc00".to_string(),
data_type: "rib".to_string(),
url: "".to_string(),
rough_size: 0,
exact_size: 0,
},
BrokerItem {
ts_start: DateTime::from_timestamp(10, 0).unwrap().naive_utc(),
ts_end: Default::default(),
collector_id: "route-views2".to_string(),
data_type: "rib".to_string(),
url: "".to_string(),
rough_size: 0,
exact_size: 0,
},
];
let correct_items = vec![
BrokerItem {
ts_start: DateTime::from_timestamp(9, 0).unwrap().naive_utc(),
ts_end: Default::default(),
collector_id: "rrc00".to_string(),
data_type: "updates".to_string(),
url: "".to_string(),
rough_size: 0,
exact_size: 0,
},
BrokerItem {
ts_start: DateTime::from_timestamp(10, 0).unwrap().naive_utc(),
ts_end: Default::default(),
collector_id: "route-views2".to_string(),
data_type: "rib".to_string(),
url: "".to_string(),
rough_size: 0,
exact_size: 0,
},
BrokerItem {
ts_start: DateTime::from_timestamp(10, 0).unwrap().naive_utc(),
ts_end: Default::default(),
collector_id: "rrc00".to_string(),
data_type: "rib".to_string(),
url: "".to_string(),
rough_size: 0,
exact_size: 0,
},
BrokerItem {
ts_start: DateTime::from_timestamp(10, 0).unwrap().naive_utc(),
ts_end: Default::default(),
collector_id: "rrc00".to_string(),
data_type: "updates".to_string(),
url: "".to_string(),
rough_size: 0,
exact_size: 0,
},
];
assert_ne!(items, correct_items);
items.sort();
assert_eq!(items, correct_items);
}
}