use std::collections::HashSet;
pub fn aggregate_interface_bytes(
ifaces: &[(String, u64, u64)],
uplinks: Option<&HashSet<String>>,
) -> (u64, u64, String) {
let mut total_recv: u64 = 0;
let mut total_sent: u64 = 0;
let mut iface_name = String::from("No Interface");
let mut best_traffic: u64 = 0;
for (name, r, s) in ifaces {
let counted = match uplinks {
Some(set) => set.contains(name),
None => !is_filter_shadow(name, ifaces),
};
if !counted {
continue;
}
total_recv += r;
total_sent += s;
if r + s > best_traffic {
best_traffic = r + s;
iface_name = name.clone();
}
}
(total_recv, total_sent, iface_name)
}
fn is_filter_shadow(name: &str, ifaces: &[(String, u64, u64)]) -> bool {
has_filter_instance_suffix(name)
&& ifaces.iter().any(|(base, _, _)| {
base != name && name.starts_with(base.as_str()) && name[base.len()..].starts_with('-')
})
}
fn has_filter_instance_suffix(name: &str) -> bool {
match name.rsplit_once('-') {
Some((_, tail)) => tail.len() >= 4 && tail.chars().all(|c| c.is_ascii_digit()),
None => false,
}
}