| 1 | use crate::{collection::network::NetworkHarvest, utils::data_units::convert_bytes}; |
| 2 | |
| 3 | pub mod cpu_basic; |
| 4 | pub mod cpu_graph; |
| 5 | pub mod disk_io_graph; |
| 6 | pub mod disk_table; |
| 7 | pub mod mem_basic; |
| 8 | pub mod mem_graph; |
| 9 | pub mod network_basic; |
| 10 | pub mod network_graph; |
| 11 | pub mod process_table; |
| 12 | pub mod temperature_graph; |
| 13 | pub mod temperature_table; |
| 14 | |
| 15 | #[cfg(feature = "battery")] |
| 16 | pub mod battery_display; |
| 17 | |
| 18 | /// Helper struct to hold packet-related data |
| 19 | pub(super) struct PacketInfo { |
| 20 | /// Current received packet rate. |
| 21 | pub(super) rx_packet_rate: u64, |
| 22 | |
| 23 | /// Current transmitted packet rate. |
| 24 | pub(super) tx_packet_rate: u64, |
| 25 | |
| 26 | /// Average received packet size in bytes, converted to the nearest unit. |
| 27 | pub(super) avg_rx_packet_size: (f64, &'static str), |
| 28 | |
| 29 | /// Average transmitted packet size in bytes, converted to the nearest unit. |
| 30 | pub(super) avg_tx_packet_size: (f64, &'static str), |
| 31 | } |
| 32 | |
| 33 | /// Calculate packet information from network data. |
| 34 | pub(super) fn calculate_packet_info( |
| 35 | network_latest_data: &NetworkHarvest, use_binary_prefix: bool, |
| 36 | ) -> PacketInfo { |
| 37 | let rx_packet_rate = network_latest_data.rx_packets; |
| 38 | let tx_packet_rate = network_latest_data.tx_packets; |
| 39 | |
| 40 | // Calculate average packet size (bytes per packet) |
| 41 | let avg_rx_packet_size = if network_latest_data.rx_packets > 0 { |
| 42 | (network_latest_data.rx as f64 / 8.0) / network_latest_data.rx_packets as f64 // Convert bits to bytes |
| 43 | } else { |
| 44 | 0.0 |
| 45 | }; |
| 46 | |
| 47 | let avg_tx_packet_size = if network_latest_data.tx_packets > 0 { |
| 48 | (network_latest_data.tx as f64 / 8.0) / network_latest_data.tx_packets as f64 // Convert bits to bytes |
| 49 | } else { |
| 50 | 0.0 |
| 51 | }; |
| 52 | |
| 53 | let avg_rx_packet_size = convert_bytes(avg_rx_packet_size.round() as u64, use_binary_prefix); |
| 54 | let avg_tx_packet_size = convert_bytes(avg_tx_packet_size.round() as u64, use_binary_prefix); |
| 55 | |
| 56 | PacketInfo { |
| 57 | rx_packet_rate, |
| 58 | tx_packet_rate, |
| 59 | avg_rx_packet_size, |
| 60 | avg_tx_packet_size, |
| 61 | } |
| 62 | } |