Skip to content

Commit 5dadd20

Browse files
514sidFabianLars
andauthored
feat(upload): add progressTotal to event payload (#2033)
Co-authored-by: Fabian-Lars <[email protected]>
1 parent 3e15ace commit 5dadd20

File tree

4 files changed

+17
-4
lines changed

4 files changed

+17
-4
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"upload": "minor"
3+
"upload-js": "minor"
4+
---
5+
6+
Added a new field `progressTotal` to track the total amount of data transferred during the upload/download process.

plugins/upload/guest-js/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { invoke, Channel } from '@tauri-apps/api/core'
66

77
interface ProgressPayload {
88
progress: number
9+
progressTotal: number
910
total: number
1011
transferSpeed: number
1112
}

plugins/upload/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ impl Serialize for Error {
6161
#[serde(rename_all = "camelCase")]
6262
struct ProgressPayload {
6363
progress: u64,
64+
progress_total: u64,
6465
total: u64,
6566
transfer_speed: u64,
6667
}
@@ -99,6 +100,7 @@ async fn download(
99100
stats.record_chunk_transfer(chunk.len());
100101
let _ = on_progress.send(ProgressPayload {
101102
progress: chunk.len() as u64,
103+
progress_total: stats.total_transferred,
102104
total,
103105
transfer_speed: stats.transfer_speed,
104106
});
@@ -153,6 +155,7 @@ fn file_to_body(channel: Channel<ProgressPayload>, file: File) -> reqwest::Body
153155
stats.record_chunk_transfer(progress as usize);
154156
let _ = channel.send(ProgressPayload {
155157
progress,
158+
progress_total: stats.total_transferred,
156159
total,
157160
transfer_speed: stats.transfer_speed,
158161
});

plugins/upload/src/transfer_stats.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
use std::time::Instant;
66

7-
// The TransferStats struct is used to track and calculate the transfer speed of data chunks over time.
7+
// The TransferStats struct tracks both transfer speed and cumulative transfer progress.
88
pub struct TransferStats {
99
accumulated_chunk_len: usize, // Total length of chunks transferred in the current period
1010
accumulated_time: u128, // Total time taken for the transfers in the current period
1111
pub transfer_speed: u64, // Calculated transfer speed in bytes per second
12+
pub total_transferred: u64, // Cumulative total of all transferred data
1213
start_time: Instant, // Time when the current period started
1314
granularity: u32, // Time period (in milliseconds) over which the transfer speed is calculated
1415
}
@@ -20,18 +21,20 @@ impl TransferStats {
2021
accumulated_chunk_len: 0,
2122
accumulated_time: 0,
2223
transfer_speed: 0,
24+
total_transferred: 0,
2325
start_time: Instant::now(),
2426
granularity,
2527
}
2628
}
27-
// Records the transfer of a data chunk and updates the transfer speed if the granularity period has elapsed.
29+
// Records the transfer of a data chunk and updates both transfer speed and total progress.
2830
pub fn record_chunk_transfer(&mut self, chunk_len: usize) {
2931
let now = Instant::now();
3032
let it_took = now.duration_since(self.start_time).as_millis();
3133
self.accumulated_chunk_len += chunk_len;
34+
self.total_transferred += chunk_len as u64;
3235
self.accumulated_time += it_took;
3336

34-
// If the accumulated time exceeds the granularity, calculate the transfer speed.
37+
// Calculate transfer speed if accumulated time exceeds granularity.
3538
if self.accumulated_time >= self.granularity as u128 {
3639
self.transfer_speed =
3740
(self.accumulated_chunk_len as u128 / self.accumulated_time * 1024) as u64;
@@ -47,6 +50,6 @@ impl TransferStats {
4750
// Provides a default implementation for TransferStats with a granularity of 500 milliseconds.
4851
impl Default for TransferStats {
4952
fn default() -> Self {
50-
Self::start(500) // Default granularity is 500
53+
Self::start(500) // Default granularity is 500 ms
5154
}
5255
}

0 commit comments

Comments
 (0)