From b31d5c286a5a07b64932fdc3ec6c2b084b20e613 Mon Sep 17 00:00:00 2001 From: Louis van Liefland <116044207+quexeky@users.noreply.github.com> Date: Sat, 23 Nov 2024 23:32:56 +1100 Subject: [PATCH] chore(downloads): Progress on write speeds & added debug statements --- desktop/src-tauri/src/db.rs | 13 ++++++-- .../src-tauri/src/downloads/download_agent.rs | 30 +++++++++++-------- .../src/downloads/download_commands.rs | 8 +++++ desktop/src-tauri/src/downloads/manifest.rs | 1 + .../src/downloads/progress_object.rs | 9 ++++-- desktop/src-tauri/src/lib.rs | 6 +++- desktop/src-tauri/src/p2p/registration.rs | 17 +++++++++++ 7 files changed, 67 insertions(+), 17 deletions(-) create mode 100644 desktop/src-tauri/src/p2p/registration.rs diff --git a/desktop/src-tauri/src/db.rs b/desktop/src-tauri/src/db.rs index f5e5ea4d..4daf9844 100644 --- a/desktop/src-tauri/src/db.rs +++ b/desktop/src-tauri/src/db.rs @@ -6,7 +6,9 @@ use std::{ }; use directories::BaseDirs; +use log::debug; use rustbreak::{deser::Bincode, PathDatabase}; +use rustix::path::Arg; use serde::{Deserialize, Serialize}; use url::Url; @@ -61,7 +63,9 @@ impl DatabaseImpls for DatabaseInterface { let db_path = data_root_dir.join("drop.db"); let games_base_dir = data_root_dir.join("games"); + debug!("Creating data directory at {:?}", data_root_dir); create_dir_all(data_root_dir.clone()).unwrap(); + debug!("Creating games directory"); create_dir_all(games_base_dir.clone()).unwrap(); let default = Database { @@ -72,10 +76,15 @@ impl DatabaseImpls for DatabaseInterface { games_statuses: HashMap::new(), }, }; + #[allow(clippy::let_and_return)] - let db = match fs::exists(db_path.clone()).unwrap() { + let exists = fs::exists(db_path.clone()).unwrap(); + let db = match exists { true => PathDatabase::load_from_path(db_path).expect("Database loading failed"), - false => PathDatabase::create_at_path(db_path, default).unwrap(), + false => { + debug!("Creating database at path {}", db_path.as_str().unwrap()); + PathDatabase::create_at_path(db_path, default).expect("Database could not be created") + }, }; db diff --git a/desktop/src-tauri/src/downloads/download_agent.rs b/desktop/src-tauri/src/downloads/download_agent.rs index cf5192b5..0bb3b71c 100644 --- a/desktop/src-tauri/src/downloads/download_agent.rs +++ b/desktop/src-tauri/src/downloads/download_agent.rs @@ -92,6 +92,7 @@ impl GameDownloadAgent { // Blocking pub fn download(&self) -> Result<(), GameDownloadError> { self.setup_download()?; + self.set_progress_object_params(); self.run().map_err(|_| GameDownloadError::DownloadError)?; Ok(()) @@ -133,18 +134,6 @@ impl GameDownloadAgent { } let manifest_download = response.json::().unwrap(); - let length = manifest_download - .values() - .map(|chunk| { - return chunk.lengths.iter().sum::(); - }) - .sum::(); - let chunk_count = manifest_download - .values() - .map(|chunk| chunk.lengths.len()) - .sum(); - self.progress.set_max(length); - self.progress.set_size(chunk_count); if let Ok(mut manifest) = self.manifest.lock() { *manifest = Some(manifest_download); @@ -154,6 +143,22 @@ impl GameDownloadAgent { Err(GameDownloadError::Lock) } + fn set_progress_object_params(&self) { + let lock = self.contexts.lock().unwrap(); + let length = lock.len(); + + let chunk_count = lock.iter() + .map(|chunk| chunk.length) + .sum(); + + debug!("Setting ProgressObject max to {}", chunk_count); + self.progress.set_max(chunk_count); + debug!("Setting ProgressObject size to {}", length); + self.progress.set_size(length); + debug!("Setting ProgressObject time to now"); + self.progress.set_time_now(); + } + pub fn generate_contexts(&self) -> Result<(), GameDownloadError> { let db_lock = DB.borrow_data().unwrap(); let data_base_dir = db_lock.games.install_dirs[self.target_download_dir].clone(); @@ -187,6 +192,7 @@ impl GameDownloadAgent { game_id: game_id.to_string(), path: path.clone(), checksum: chunk.checksums[i].clone(), + length: *length }); running_offset += *length as u64; } diff --git a/desktop/src-tauri/src/downloads/download_commands.rs b/desktop/src-tauri/src/downloads/download_commands.rs index 88838367..5b429dfb 100644 --- a/desktop/src-tauri/src/downloads/download_commands.rs +++ b/desktop/src-tauri/src/downloads/download_commands.rs @@ -46,6 +46,14 @@ pub fn stop_game_download( .download_manager .cancel_download(game_id); } +#[tauri::command] +pub fn get_current_write_speed( + state: tauri::State<'_, Mutex>, +) { + +} + + /* fn use_download_agent( state: tauri::State<'_, Mutex>, diff --git a/desktop/src-tauri/src/downloads/manifest.rs b/desktop/src-tauri/src/downloads/manifest.rs index 2bb15520..2c28ea5a 100644 --- a/desktop/src-tauri/src/downloads/manifest.rs +++ b/desktop/src-tauri/src/downloads/manifest.rs @@ -20,4 +20,5 @@ pub struct DropDownloadContext { pub game_id: String, pub path: PathBuf, pub checksum: String, + pub length: usize } diff --git a/desktop/src-tauri/src/downloads/progress_object.rs b/desktop/src-tauri/src/downloads/progress_object.rs index fb8a4dd2..0848caf1 100644 --- a/desktop/src-tauri/src/downloads/progress_object.rs +++ b/desktop/src-tauri/src/downloads/progress_object.rs @@ -1,12 +1,13 @@ -use std::sync::{ +use std::{sync::{ atomic::{AtomicUsize, Ordering}, Arc, Mutex, -}; +}, time::Instant}; #[derive(Clone)] pub struct ProgressObject { max: Arc>, progress_instances: Arc>>>, + start: Arc> } impl ProgressObject { @@ -15,8 +16,12 @@ impl ProgressObject { Self { max: Arc::new(Mutex::new(max)), progress_instances: Arc::new(arr), + start: Arc::new(Mutex::new(Instant::now())), } } + pub fn set_time_now(&self) { + *self.start.lock().unwrap() = Instant::now(); + } pub fn sum(&self) -> usize { self.progress_instances .lock() diff --git a/desktop/src-tauri/src/lib.rs b/desktop/src-tauri/src/lib.rs index bd05f636..beb1aeb7 100644 --- a/desktop/src-tauri/src/lib.rs +++ b/desktop/src-tauri/src/lib.rs @@ -17,7 +17,7 @@ use downloads::download_manager_interface::DownloadManager; use env_logger::Env; use http::{header::*, response::Builder as ResponseBuilder}; use library::{fetch_game, fetch_library, Game}; -use log::info; +use log::{debug, info}; use remote::{gen_drop_url, use_remote, RemoteAccessError}; use serde::{Deserialize, Serialize}; use std::sync::Arc; @@ -73,11 +73,13 @@ fn fetch_state(state: tauri::State<'_, Mutex>) -> Result AppState { + debug!("Starting env"); env_logger::Builder::from_env(Env::default().default_filter_or("info")).init(); let games = HashMap::new(); let download_manager = Arc::new(DownloadManagerBuilder::build()); + debug!("Checking if database is set up"); let is_set_up = DB.database_is_set_up(); if !is_set_up { return AppState { @@ -88,6 +90,8 @@ fn setup() -> AppState { }; } + debug!("Database is set up"); + let (app_status, user) = auth::setup().unwrap(); AppState { status: app_status, diff --git a/desktop/src-tauri/src/p2p/registration.rs b/desktop/src-tauri/src/p2p/registration.rs new file mode 100644 index 00000000..0926515e --- /dev/null +++ b/desktop/src-tauri/src/p2p/registration.rs @@ -0,0 +1,17 @@ +use crate::{auth::generate_authorization_header, db::DatabaseImpls, remote::RemoteAccessError, DB}; + + +pub async fn register() -> Result { + let base_url = DB.fetch_base_url(); + let registration_url = base_url.join("/api/v1/client/capability").unwrap(); + let header = generate_authorization_header(); + + + let client = reqwest::blocking::Client::new(); + client + .post(registration_url) + .header("Authorization", header) + .send()?; + + return Ok(String::new()) +} \ No newline at end of file