From 5b607c45ae38c1395a4cc2d73d9ecf24730fddc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20K=C3=A5re=20Alsaker?= Date: Fri, 9 Aug 2024 02:53:57 +0200 Subject: [PATCH] Save results in `crusader-results` by default --- android/src/lib.rs | 4 ++-- src/crusader-gui-lib/src/lib.rs | 14 ++++++++++---- src/crusader-lib/src/file_format.rs | 14 +++++++------- src/crusader-lib/src/plot.rs | 24 +++++++++++++++++------- src/crusader-lib/src/remote.rs | 6 +++--- src/crusader-lib/src/test.rs | 22 +++++++++++++++------- src/crusader/src/main.rs | 9 +++++++-- 7 files changed, 61 insertions(+), 32 deletions(-) diff --git a/android/src/lib.rs b/android/src/lib.rs index 4ce7ef6..4ea2bae 100644 --- a/android/src/lib.rs +++ b/android/src/lib.rs @@ -182,7 +182,7 @@ fn android_main(app: AndroidApp) { tester.file_loader = Some(Box::new(|_| load_file().unwrap())); tester.plot_saver = Some(Box::new(move |result| { let path = temp_plot.as_deref().unwrap(); - crusader_lib::plot::save_graph_to_path(path, &PlotConfig::default(), result); + crusader_lib::plot::save_graph_to_path(path, &PlotConfig::default(), result).unwrap(); let data = fs::read(path).unwrap(); fs::remove_file(path).unwrap(); let name = format!("{}.png", crusader_lib::test::timed("plot")); @@ -190,7 +190,7 @@ fn android_main(app: AndroidApp) { })); tester.raw_saver = Some(Box::new(|result| { let mut writer = Cursor::new(Vec::new()); - result.save_to_writer(&mut writer); + result.save_to_writer(&mut writer).unwrap(); let data = writer.into_inner(); let name = format!("{}.crr", crusader_lib::test::timed("data")); save_file(false, name, data).unwrap(); diff --git a/src/crusader-gui-lib/src/lib.rs b/src/crusader-gui-lib/src/lib.rs index 8a76582..4d85d84 100644 --- a/src/crusader-gui-lib/src/lib.rs +++ b/src/crusader-gui-lib/src/lib.rs @@ -843,11 +843,13 @@ impl Tester { saver(&self.result.as_ref().unwrap().result); } None => { - self.result_saved = Some(plot::save_graph( + self.result_saved = plot::save_graph( &PlotConfig::default(), &self.result.as_ref().unwrap().result, "plot", - )); + Path::new("crusader-results"), + ) + .ok(); } } } @@ -863,8 +865,12 @@ impl Tester { saver(self.raw_result.as_ref().unwrap()); } None => { - self.raw_result_saved = - Some(test::save_raw(self.raw_result.as_ref().unwrap(), "data")); + self.raw_result_saved = test::save_raw( + self.raw_result.as_ref().unwrap(), + "data", + Path::new("crusader-results"), + ) + .ok(); } } } diff --git a/src/crusader-lib/src/file_format.rs b/src/crusader-lib/src/file_format.rs index 4cefc65..14f4a71 100644 --- a/src/crusader-lib/src/file_format.rs +++ b/src/crusader-lib/src/file_format.rs @@ -219,20 +219,20 @@ impl RawResult { Self::load_from_reader(File::open(path).ok()?) } - pub fn save_to_writer(&self, writer: impl Write) { + pub fn save_to_writer(&self, writer: impl Write) -> Result<(), anyhow::Error> { let mut file = BufWriter::new(writer); - bincode::serialize_into(&mut file, &RawHeader::default()).unwrap(); + bincode::serialize_into(&mut file, &RawHeader::default())?; let mut compressor = snap::write::FrameEncoder::new(file); - self.serialize(&mut rmp_serde::Serializer::new(&mut compressor).with_struct_map()) - .unwrap(); + self.serialize(&mut rmp_serde::Serializer::new(&mut compressor).with_struct_map())?; - compressor.flush().unwrap(); + compressor.flush()?; + Ok(()) } - pub fn save(&self, name: &Path) { - self.save_to_writer(File::create(name).unwrap()) + pub fn save(&self, name: &Path) -> Result<(), anyhow::Error> { + self.save_to_writer(File::create(name)?) } } diff --git a/src/crusader-lib/src/plot.rs b/src/crusader-lib/src/plot.rs index e38f2c9..304f18b 100644 --- a/src/crusader-lib/src/plot.rs +++ b/src/crusader-lib/src/plot.rs @@ -1,4 +1,4 @@ -use anyhow::anyhow; +use anyhow::{anyhow, Context}; use image::{ImageBuffer, ImageFormat, Rgb}; use plotters::coord::types::RangedCoordf64; use plotters::coord::Shift; @@ -153,16 +153,26 @@ pub struct TestResult { pub stream_groups: Vec, } -pub fn save_graph(config: &PlotConfig, result: &TestResult, name: &str) -> String { +pub fn save_graph( + config: &PlotConfig, + result: &TestResult, + name: &str, + root_path: &Path, +) -> Result { + std::fs::create_dir_all(root_path)?; let file = unique(name, "png"); - save_graph_to_path(file.as_ref(), config, result); - file + save_graph_to_path(&root_path.join(&file), config, result)?; + Ok(file) } -pub fn save_graph_to_path(path: &Path, config: &PlotConfig, result: &TestResult) { - let img = save_graph_to_mem(config, result).expect("Unable to write plot to file"); +pub fn save_graph_to_path( + path: &Path, + config: &PlotConfig, + result: &TestResult, +) -> Result<(), anyhow::Error> { + let img = save_graph_to_mem(config, result).context("Unable to plot")?; img.save_with_format(&path, ImageFormat::Png) - .expect("Unable to write plot to file"); + .context("Unable to write plot to file") } pub(crate) fn save_graph_to_mem( diff --git a/src/crusader-lib/src/remote.rs b/src/crusader-lib/src/remote.rs index 4042230..e3f107c 100644 --- a/src/crusader-lib/src/remote.rs +++ b/src/crusader-lib/src/remote.rs @@ -147,10 +147,10 @@ async fn handle_client( let data = task::spawn_blocking(move || { let mut data = Vec::new(); - result.save_to_writer(&mut data); - data + result.save_to_writer(&mut data)?; + Ok::<_, anyhow::Error>(data) }) - .await?; + .await??; socket.send(Message::Binary(data)).await?; (state.msg)(&format!("Remote client running from {}", who.ip())); diff --git a/src/crusader-lib/src/test.rs b/src/crusader-lib/src/test.rs index 8da8129..30fe84c 100644 --- a/src/crusader-lib/src/test.rs +++ b/src/crusader-lib/src/test.rs @@ -598,10 +598,11 @@ pub(crate) async fn test_async( Ok(raw_result) } -pub fn save_raw(result: &RawResult, name: &str) -> String { +pub fn save_raw(result: &RawResult, name: &str, root_path: &Path) -> Result { + std::fs::create_dir_all(root_path)?; let name = unique(name, "crr"); - result.save(Path::new(&name)); - name + result.save(&root_path.join(&name))?; + Ok(name) } fn setup_loaders( @@ -894,10 +895,17 @@ pub fn test( } }; println!("{}", with_time("Writing data...")); - let raw = save_raw(&result, "data"); - println!("{}", with_time(&format!("Saved raw data as {}", raw))); - let file = save_graph(&plot, &result.to_test_result(), "plot"); - println!("{}", with_time(&format!("Saved plot as {}", file))); + let path = Path::new("crusader-results"); + let raw = save_raw(&result, "data", path)?; + println!( + "{}", + with_time(&format!("Saved raw data as {}", path.join(raw).display())) + ); + let plot = save_graph(&plot, &result.to_test_result(), "plot", path)?; + println!( + "{}", + with_time(&format!("Saved plot as {}", path.join(plot).display())) + ); Ok(()) } diff --git a/src/crusader/src/main.rs b/src/crusader/src/main.rs index 2098fb6..f824f80 100644 --- a/src/crusader/src/main.rs +++ b/src/crusader/src/main.rs @@ -9,9 +9,9 @@ use crusader_lib::{protocol, LIB_VERSION}; use crusader_lib::{with_time, Config}; #[cfg(feature = "client")] use std::path::PathBuf; -use std::process; #[cfg(feature = "client")] use std::time::Duration; +use std::{path::Path, process}; #[derive(Parser)] #[command(version = LIB_VERSION)] @@ -198,14 +198,19 @@ fn run() -> Result<(), anyhow::Error> { #[cfg(feature = "client")] Commands::Plot { data, plot } => { let result = RawResult::load(data).expect("Unable to load data"); + let root = data.parent().unwrap_or(Path::new("")); let file = crusader_lib::plot::save_graph( &plot.config(), &result.to_test_result(), data.file_stem() .and_then(|name| name.to_str()) .unwrap_or("plot"), + data.parent().unwrap_or(Path::new("")), + )?; + println!( + "{}", + with_time(&format!("Saved plot as {}", root.join(file).display())) ); - println!("{}", with_time(&format!("Saved plot as {}", file))); Ok(()) } }