feat(upload): add `progressTotal` to event payload (#2033)

Co-authored-by: Fabian-Lars <github@fabianlars.de>
pull/2035/head
sid 7 months ago committed by GitHub
parent 3e15acea9a
commit 5dadd205f5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,6 @@
---
"upload": "minor"
"upload-js": "minor"
---
Added a new field `progressTotal` to track the total amount of data transferred during the upload/download process.

@ -6,6 +6,7 @@ import { invoke, Channel } from '@tauri-apps/api/core'
interface ProgressPayload { interface ProgressPayload {
progress: number progress: number
progressTotal: number
total: number total: number
transferSpeed: number transferSpeed: number
} }

@ -61,6 +61,7 @@ impl Serialize for Error {
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
struct ProgressPayload { struct ProgressPayload {
progress: u64, progress: u64,
progress_total: u64,
total: u64, total: u64,
transfer_speed: u64, transfer_speed: u64,
} }
@ -99,6 +100,7 @@ async fn download(
stats.record_chunk_transfer(chunk.len()); stats.record_chunk_transfer(chunk.len());
let _ = on_progress.send(ProgressPayload { let _ = on_progress.send(ProgressPayload {
progress: chunk.len() as u64, progress: chunk.len() as u64,
progress_total: stats.total_transferred,
total, total,
transfer_speed: stats.transfer_speed, transfer_speed: stats.transfer_speed,
}); });
@ -153,6 +155,7 @@ fn file_to_body(channel: Channel<ProgressPayload>, file: File) -> reqwest::Body
stats.record_chunk_transfer(progress as usize); stats.record_chunk_transfer(progress as usize);
let _ = channel.send(ProgressPayload { let _ = channel.send(ProgressPayload {
progress, progress,
progress_total: stats.total_transferred,
total, total,
transfer_speed: stats.transfer_speed, transfer_speed: stats.transfer_speed,
}); });

@ -4,11 +4,12 @@
use std::time::Instant; 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 { pub struct TransferStats {
accumulated_chunk_len: usize, // Total length of chunks transferred in the current period 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 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 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 start_time: Instant, // Time when the current period started
granularity: u32, // Time period (in milliseconds) over which the transfer speed is calculated granularity: u32, // Time period (in milliseconds) over which the transfer speed is calculated
} }
@ -20,18 +21,20 @@ impl TransferStats {
accumulated_chunk_len: 0, accumulated_chunk_len: 0,
accumulated_time: 0, accumulated_time: 0,
transfer_speed: 0, transfer_speed: 0,
total_transferred: 0,
start_time: Instant::now(), start_time: Instant::now(),
granularity, 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) { pub fn record_chunk_transfer(&mut self, chunk_len: usize) {
let now = Instant::now(); let now = Instant::now();
let it_took = now.duration_since(self.start_time).as_millis(); let it_took = now.duration_since(self.start_time).as_millis();
self.accumulated_chunk_len += chunk_len; self.accumulated_chunk_len += chunk_len;
self.total_transferred += chunk_len as u64;
self.accumulated_time += it_took; 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 { if self.accumulated_time >= self.granularity as u128 {
self.transfer_speed = self.transfer_speed =
(self.accumulated_chunk_len as u128 / self.accumulated_time * 1024) as u64; (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. // Provides a default implementation for TransferStats with a granularity of 500 milliseconds.
impl Default for TransferStats { impl Default for TransferStats {
fn default() -> Self { fn default() -> Self {
Self::start(500) // Default granularity is 500 Self::start(500) // Default granularity is 500 ms
} }
} }

Loading…
Cancel
Save