From 139ba037f7fabbdb7479eae9c8d3bf2d900a0836 Mon Sep 17 00:00:00 2001 From: 514sid <140138716+514sid@users.noreply.github.com> Date: Mon, 11 Nov 2024 19:44:41 +0400 Subject: [PATCH] feat: add total_transferred field to track cumulative data transferred --- plugins/upload/src/transfer_stats.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/plugins/upload/src/transfer_stats.rs b/plugins/upload/src/transfer_stats.rs index 0ae32ec8..2f3a3946 100644 --- a/plugins/upload/src/transfer_stats.rs +++ b/plugins/upload/src/transfer_stats.rs @@ -4,11 +4,12 @@ use std::time::Instant; -// The TransferStats struct is used to track and calculate the transfer speed of data chunks over time. +// The TransferStats struct tracks both transfer speed and cumulative transfer progress. pub struct TransferStats { accumulated_chunk_len: usize, // Total length of chunks transferred in the current period accumulated_time: u128, // Total time taken for the transfers in the current period pub transfer_speed: u64, // Calculated transfer speed in bytes per second + pub total_transferred: u64, // Cumulative total of all transferred data start_time: Instant, // Time when the current period started granularity: u32, // Time period (in milliseconds) over which the transfer speed is calculated } @@ -20,18 +21,20 @@ impl TransferStats { accumulated_chunk_len: 0, accumulated_time: 0, transfer_speed: 0, + total_transferred: 0, start_time: Instant::now(), granularity, } } - // Records the transfer of a data chunk and updates the transfer speed if the granularity period has elapsed. + // Records the transfer of a data chunk and updates both transfer speed and total progress. pub fn record_chunk_transfer(&mut self, chunk_len: usize) { let now = Instant::now(); let it_took = now.duration_since(self.start_time).as_millis(); self.accumulated_chunk_len += chunk_len; + self.total_transferred += chunk_len as u64; self.accumulated_time += it_took; - // If the accumulated time exceeds the granularity, calculate the transfer speed. + // Calculate transfer speed if accumulated time exceeds granularity. if self.accumulated_time >= self.granularity as u128 { self.transfer_speed = (self.accumulated_chunk_len as u128 / self.accumulated_time * 1024) as u64; @@ -47,6 +50,6 @@ impl TransferStats { // Provides a default implementation for TransferStats with a granularity of 500 milliseconds. impl Default for TransferStats { fn default() -> Self { - Self::start(500) // Default granularity is 500 + Self::start(500) // Default granularity is 500 ms } }