Skip to content

Commit eac4adc

Browse files
committed
feat: report download percent
1 parent 57b7e85 commit eac4adc

File tree

4 files changed

+36
-61
lines changed

4 files changed

+36
-61
lines changed

example/gauge/Components/Pages/Auth/Auth.tsx

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { ComponentProps, DisplayComponent, FSComponent, VNode } from "@microsoft/msfs-sdk";
22
import {
3-
DownloadProgressPhase,
43
NavigationDataStatus,
54
NavigraphEventType,
65
NavigraphNavigationDataInterface,
@@ -29,22 +28,7 @@ export class AuthPage extends DisplayComponent<AuthPageProps> {
2928
super(props);
3029

3130
this.props.navigationDataInterface.onEvent(NavigraphEventType.DownloadProgress, data => {
32-
switch (data.phase) {
33-
case DownloadProgressPhase.Downloading:
34-
this.displayMessage("Downloading navigation data...");
35-
break;
36-
case DownloadProgressPhase.Cleaning:
37-
if (!data.deleted) return;
38-
this.displayMessage(`Cleaning destination directory. ${data.deleted} files deleted so far`);
39-
break;
40-
case DownloadProgressPhase.Extracting: {
41-
// Ensure non-null
42-
if (!data.unzipped || !data.total_to_unzip) return;
43-
const percent = Math.round((data.unzipped / data.total_to_unzip) * 100);
44-
this.displayMessage(`Unzipping files... ${percent}% complete`);
45-
break;
46-
}
47-
}
31+
this.displayMessage(`Downloaded ${data.downloaded_bytes}/${data.total_bytes} bytes`);
4832
});
4933
}
5034

src/ts/interface/NavigationDataInterfaceTypes.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,9 @@ export enum NavigraphEventType {
1111
DownloadProgress = "DownloadProgress",
1212
}
1313

14-
export enum DownloadProgressPhase {
15-
Downloading = "Downloading",
16-
Cleaning = "Cleaning",
17-
Extracting = "Extracting",
18-
}
19-
2014
export interface DownloadProgressData {
21-
phase: DownloadProgressPhase;
22-
deleted: number | null;
23-
total_to_unzip: number | null;
24-
unzipped: number | null;
15+
total_bytes: number;
16+
downloaded_bytes: number;
2517
}
2618

2719
export enum NavigraphFunction {

src/wasm/src/funcs.rs

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::{
1818
WORK_NAVIGATION_DATA_FOLDER,
1919
},
2020
futures::AsyncNetworkRequest,
21-
DownloadProgressEvent, DownloadProgressPhase, InterfaceEvent,
21+
DownloadProgressEvent, InterfaceEvent,
2222
};
2323

2424
/// The URL to get the latest available cycle number
@@ -66,12 +66,30 @@ impl Function for DownloadNavigationData {
6666
type ReturnType = ();
6767

6868
async fn run(&mut self) -> Result<Self::ReturnType> {
69-
// Send an initial progress event TODO: remove these in a breaking version, these are only here for backwards compatibility
69+
// Figure out total size of download (this request is acting like a HEAD since we don't have those in this environment. Nothing actually gets downloaded since we are constraining the range)
70+
let request = NetworkRequestBuilder::new(&self.url)
71+
.context("can't create new NetworkRequestBuilder")?
72+
.with_header(&format!("Range: bytes=0-0"))
73+
.context(".with_header() returned None")?
74+
.get()
75+
.context(".get() returned None")?;
76+
77+
request.wait_for_data().await?;
78+
79+
// Try parsing the content-range header
80+
let total_bytes = request
81+
.header_section("content-range")
82+
.context("no content-range header")?
83+
.trim()
84+
.split("/")
85+
.last()
86+
.ok_or(anyhow!("invalid content-range"))?
87+
.parse::<usize>()?;
88+
89+
// Report the amount
7090
InterfaceEvent::send_download_progress_event(DownloadProgressEvent {
71-
phase: DownloadProgressPhase::Downloading,
72-
deleted: None,
73-
total_to_unzip: None,
74-
unzipped: None,
91+
total_bytes,
92+
downloaded_bytes: 0,
7593
})?;
7694

7795
// We need to download the data in chunks of DOWNLOAD_CHUNK_SIZE_BYTES to avoid a timeout, so we need to keep track of a "working" accumulation of all responses
@@ -121,6 +139,12 @@ impl Function for DownloadNavigationData {
121139
}
122140

123141
current_byte_index += content_length;
142+
143+
// Send the current download amount
144+
InterfaceEvent::send_download_progress_event(DownloadProgressEvent {
145+
total_bytes,
146+
downloaded_bytes: current_byte_index,
147+
})?;
124148
}
125149

126150
// Only close connection if DATABASE_STATE has already been initialized - otherwise we end up unnecessarily copying the bundled data and instantly replacing it (due to initialization logic in database state)
@@ -132,21 +156,6 @@ impl Function for DownloadNavigationData {
132156
.close_connection()?;
133157
}
134158

135-
// Send the deleting and extraction events
136-
InterfaceEvent::send_download_progress_event(DownloadProgressEvent {
137-
phase: DownloadProgressPhase::Cleaning,
138-
deleted: Some(2),
139-
total_to_unzip: None,
140-
unzipped: None,
141-
})?;
142-
143-
InterfaceEvent::send_download_progress_event(DownloadProgressEvent {
144-
phase: DownloadProgressPhase::Extracting,
145-
deleted: None,
146-
total_to_unzip: Some(2),
147-
unzipped: None,
148-
})?;
149-
150159
// Load the zip archive
151160
let mut zip = ZipArchive::new(Cursor::new(bytes))?;
152161

src/wasm/src/lib.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,18 @@ mod sentry_gauge;
2121
/// Amount of MS between dispatches of the heartbeat commbus event
2222
const HEARTBEAT_INTERVAL_MS: u128 = 1000;
2323

24-
/// The current phase of downloading
25-
#[derive(Serialize)]
26-
pub enum DownloadProgressPhase {
27-
Downloading,
28-
Cleaning,
29-
Extracting,
30-
}
31-
3224
/// The data associated with the `DownloadProgress` event
3325
#[derive(Serialize)]
3426
pub struct DownloadProgressEvent {
35-
pub phase: DownloadProgressPhase,
36-
pub deleted: Option<usize>,
37-
pub total_to_unzip: Option<usize>,
38-
pub unzipped: Option<usize>,
27+
pub total_bytes: usize,
28+
pub downloaded_bytes: usize,
3929
}
4030

4131
/// The types of events that can be emitted from the interface
4232
#[derive(Serialize)]
4333
enum NavigraphEventType {
4434
Heartbeat,
45-
DownloadProgress, // TODO: remove in a future version. here for backwards compatibility
35+
DownloadProgress,
4636
}
4737

4838
/// The structure of an event message

0 commit comments

Comments
 (0)