Skip to content

Commit

Permalink
feat: add message to patch event (#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanoltman authored Aug 30, 2024
1 parent e4e4c57 commit b2d5ece
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
12 changes: 11 additions & 1 deletion library/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,20 @@ pub struct PatchEvent {

/// When this event occurred as a Unix epoch timestamp in seconds.
pub timestamp: u64,

/// An optional message to be sent with the event.
/// Care should be taken that this field *never* contain PII or sensitive information.
pub message: Option<String>,
}

impl PatchEvent {
/// Creates a `PatchEvent` for the given `EventType` and patch number for reporting to the server.
pub fn new(config: &UpdateConfig, event_type: EventType, patch_number: usize) -> PatchEvent {
pub fn new(
config: &UpdateConfig,
event_type: EventType,
patch_number: usize,
message: Option<&str>,
) -> PatchEvent {
PatchEvent {
app_id: config.app_id.clone(),
arch: current_arch().to_string(),
Expand All @@ -82,6 +91,7 @@ impl PatchEvent {
platform: current_platform().to_string(),
release_version: config.release_version.clone(),
timestamp: time::unix_timestamp(),
message: message.map(|s| s.to_string()),
}
}
}
25 changes: 24 additions & 1 deletion library/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,33 @@ mod tests {
release_version: "release_version".to_string(),
identifier: EventType::PatchInstallSuccess,
timestamp: 1234,
message: None,
};
let request = super::CreatePatchEventRequest { event };
let json_string = serde_json::to_string(&request).unwrap();
assert_eq!(
json_string,
r#"{"event":{"app_id":"app_id","arch":"arch","type":"__patch_install__","patch_number":1,"platform":"platform","release_version":"release_version","timestamp":1234}}"#
r#"{"event":{"app_id":"app_id","arch":"arch","type":"__patch_install__","patch_number":1,"platform":"platform","release_version":"release_version","timestamp":1234,"message":null}}"#
)
}

#[test]
fn create_patch_install_event_request_serializes_with_message() {
let event = PatchEvent {
app_id: "app_id".to_string(),
arch: "arch".to_string(),
patch_number: 1,
platform: "platform".to_string(),
release_version: "release_version".to_string(),
identifier: EventType::PatchInstallSuccess,
timestamp: 1234,
message: Some("hello".to_string()),
};
let request = super::CreatePatchEventRequest { event };
let json_string = serde_json::to_string(&request).unwrap();
assert_eq!(
json_string,
r#"{"event":{"app_id":"app_id","arch":"arch","type":"__patch_install__","patch_number":1,"platform":"platform","release_version":"release_version","timestamp":1234,"message":"hello"}}"#
)
}

Expand Down Expand Up @@ -367,6 +388,7 @@ mod tests {
release_version: "release_version".to_string(),
identifier: EventType::PatchInstallSuccess,
timestamp: time::unix_timestamp(),
message: None,
};
let result = super::report_event_default(
// Make the request to a non-existent URL, which will trigger the
Expand Down Expand Up @@ -395,6 +417,7 @@ mod tests {
release_version: "release_version".to_string(),
identifier: EventType::PatchInstallSuccess,
timestamp: time::unix_timestamp(),
message: None,
},
},
);
Expand Down
24 changes: 22 additions & 2 deletions library/src/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,13 @@ pub fn handle_prior_boot_failure_if_necessary() -> Result<(), InitError> {
config,
EventType::PatchInstallFailure,
patch.number,
Some(
format!(
"Patch {} was marked currently_booting in init",
patch.number
)
.as_ref(),
),
))?;
}

Expand Down Expand Up @@ -420,7 +427,7 @@ fn update_internal(_: &UpdaterLockState) -> anyhow::Result<UpdateStatus> {
);

std::thread::spawn(move || {
let event = PatchEvent::new(&config, EventType::PatchDownload, patch.number);
let event = PatchEvent::new(&config, EventType::PatchDownload, patch.number, None);
let report_result = crate::network::send_patch_event(event, &config);
if let Err(err) = report_result {
error!("Failed to report patch download: {:?}", err);
Expand Down Expand Up @@ -562,7 +569,18 @@ pub fn report_launch_failure() -> anyhow::Result<()> {
if mark_result.is_err() {
error!("Failed to mark patch as bad: {:?}", mark_result);
}
let event = PatchEvent::new(config, EventType::PatchInstallFailure, patch.number);
let event = PatchEvent::new(
config,
EventType::PatchInstallFailure,
patch.number,
Some(
format!(
"Install failure reported from engine for patch {}",
patch.number
)
.as_ref(),
),
);
// Queue the failure event for later sending since right after this
// function returns the Flutter engine is likely to abort().
state.queue_event(event)
Expand Down Expand Up @@ -612,6 +630,7 @@ pub fn report_launch_success() -> anyhow::Result<()> {
&config_copy,
EventType::PatchInstallSuccess,
booting_patch.number,
None,
);
let report_result = crate::network::send_patch_event(event, &config_copy);
if let Err(err) = report_result {
Expand Down Expand Up @@ -1216,6 +1235,7 @@ mod tests {
platform: current_platform().to_string(),
release_version: config.release_version.clone(),
timestamp: time::unix_timestamp(),
message: Some("Install failure reported from engine for patch 1".to_string()),
};
// Queue 5 events.
assert!(state.queue_event(fail_event.clone()).is_ok());
Expand Down

0 comments on commit b2d5ece

Please sign in to comment.