Skip to main content

manycastr/cli/writer/
trace_row.rs

1use crate::SINGLE_ORIGIN;
2use crate::cli::writer::format_rtt;
3use crate::custom_module::manycastr::TraceReply;
4use bimap::BiHashMap;
5
6/// Get traceroute row
7/// format: rx, hop_addr, ttl, tx, trace_dst, trace_ttl, rtt [, origin_id]
8pub fn get_trace_row(
9    reply: TraceReply,
10    rx_id: &u32,
11    worker_map: &BiHashMap<u32, String>,
12    origin_id: u32,
13) -> Vec<String> {
14    // convert the worker ID to hostname (no hop reply → no worker received it → '*')
15    let rx_hostname = if reply.hop_addr.is_some() {
16        worker_map
17            .get_by_left(rx_id)
18            .unwrap_or(&String::from("*"))
19            .to_string()
20    } else {
21        "*".to_string()
22    };
23
24    let tx_hostname = worker_map
25        .get_by_left(&reply.tx_id)
26        .unwrap_or(&String::from("*"))
27        .to_string();
28
29    // Set fields to '*' when it is an unresponsive hop
30    let hop_addr = if let Some(hop_addr) = reply.hop_addr {
31        hop_addr.to_string()
32    } else {
33        "*".to_string()
34    };
35
36    let rtt = if reply.hop_addr.is_some() {
37        format_rtt(reply.rtt)
38    } else {
39        "*".to_string()
40    };
41    let mut row = vec![
42        rx_hostname,
43        hop_addr,
44        reply.ttl.to_string(),
45        tx_hostname,
46        reply.trace_dst.unwrap().to_string(),
47        reply.hop_count.to_string(),
48        rtt,
49    ];
50
51    if origin_id != SINGLE_ORIGIN {
52        row.push(origin_id.to_string());
53    }
54
55    row
56}