Skip to main content

manycastr/cli/writer/
laces_row.rs

1use crate::SINGLE_ORIGIN;
2use crate::cli::writer::format_rtt;
3use crate::custom_module::manycastr::MeasurementReply;
4use bimap::BiHashMap;
5
6/// Get the result (csv row) from a Reply message
7///
8/// # Arguments
9/// * `reply` - The Reply that is being written to this row
10/// * `rx_worker_id` - The worker ID of the receiver
11/// * `worker_map` - A map of worker IDs to hostnames, used to convert worker IDs to hostnames in the results
12/// * `origin_id` - Associated origin ID of the reply
13///
14/// # Returns
15/// A vector of strings representing the row in the CSV file
16///
17/// # Note
18/// The `rtt` column carries the signed offset `rx_time - tx_time` (milliseconds), computed on
19/// the worker. Under anycast the sender (`tx`) and receiver (`rx`) may be different PoPs, so it
20/// may be negative.
21pub fn get_laces_row(
22    reply: MeasurementReply,
23    rx_worker_id: &u32,
24    worker_map: &BiHashMap<u32, String>,
25    origin_id: u32,
26) -> Vec<String> {
27    // convert the worker ID to hostname
28    let rx_hostname = worker_map
29        .get_by_left(rx_worker_id)
30        .unwrap_or(&String::from("Unknown"))
31        .to_string();
32
33    let tx_hostname = worker_map
34        .get_by_left(&reply.tx_id)
35        .unwrap_or(&String::from("Unknown"))
36        .to_string();
37
38    let mut row = vec![
39        rx_hostname,
40        reply.src.unwrap().to_string(),
41        reply.ttl.to_string(),
42        tx_hostname,
43    ];
44
45    // CHAOS replies carry no RTT
46    match reply.chaos {
47        Some(chaos) => row.push(chaos),
48        None => row.push(format_rtt(reply.rtt)),
49    }
50
51    if origin_id != SINGLE_ORIGIN {
52        row.push(origin_id.to_string());
53    }
54
55    row
56}