Skip to main content

manycastr/cli/writer/
csv_writer.rs

1use crate::ALL_WORKERS;
2use crate::cli::writer::MetadataArgs;
3use crate::custom_module::Separated;
4use bimap::BiHashMap;
5
6/// Returns a vector of lines containing the metadata of the measurement
7///
8/// # Arguments
9/// * `args` - Contains measurement arguments to be written into the metadata
10/// * `worker_map` - Convert IDs to hostnames
11pub fn get_csv_metadata(
12    args: MetadataArgs<'_>,
13    worker_map: &BiHashMap<u32, String>,
14) -> Vec<String> {
15    let mut md_file = Vec::new();
16    md_file.push(format!("# Measurement type: {}", args.m_type));
17    if args.is_responsive {
18        md_file.push("# Responsiveness mode enabled".to_string());
19    }
20
21    // Print configurations used
22    for configuration in args.configurations {
23        let origin = configuration.origin.unwrap();
24        let src = origin.src.expect("Invalid source address");
25        let hostname = if configuration.worker_id == ALL_WORKERS {
26            format!("ALL {}", worker_map.len())
27        } else {
28            worker_map
29                .get_by_left(&configuration.worker_id)
30                .unwrap_or(&String::from("Unknown"))
31                .to_string()
32        };
33        md_file.push(format!(
34            "# Configuration (Origin ID: {}) - Worker: {hostname:<2}, src IP: {src}, src port: {}, dst port: {}, protocol: {}",
35            origin.origin_id, origin.sport, origin.dport, origin.p_type()
36        ));
37    }
38    md_file.push(format!(
39        "# Hitlist{}: {}",
40        if args.is_shuffle { " (shuffled)" } else { "" },
41        args.hitlist
42    ));
43    md_file.push(format!(
44        "# Probing rate: {}",
45        args.probing_rate.with_separator()
46    ));
47    md_file.push(format!("# Worker interval: {}", args.interval));
48    let hostnames: Vec<_> = args.all_workers.iter().map(|(_, h)| h.as_str()).collect();
49    md_file.push(format!(
50        "# Connected workers ({}): {}",
51        hostnames.len(),
52        hostnames.join(", ")
53    ));
54
55    md_file
56}