chore(downloads): Progress on write speeds & added debug statements

This commit is contained in:
Louis van Liefland
2024-11-23 23:32:56 +11:00
parent 76b0975bcc
commit b065e101e6
7 changed files with 67 additions and 17 deletions

View File

@ -6,7 +6,9 @@ use std::{
}; };
use directories::BaseDirs; use directories::BaseDirs;
use log::debug;
use rustbreak::{deser::Bincode, PathDatabase}; use rustbreak::{deser::Bincode, PathDatabase};
use rustix::path::Arg;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use url::Url; use url::Url;
@ -61,7 +63,9 @@ impl DatabaseImpls for DatabaseInterface {
let db_path = data_root_dir.join("drop.db"); let db_path = data_root_dir.join("drop.db");
let games_base_dir = data_root_dir.join("games"); 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(); create_dir_all(data_root_dir.clone()).unwrap();
debug!("Creating games directory");
create_dir_all(games_base_dir.clone()).unwrap(); create_dir_all(games_base_dir.clone()).unwrap();
let default = Database { let default = Database {
@ -72,10 +76,15 @@ impl DatabaseImpls for DatabaseInterface {
games_statuses: HashMap::new(), games_statuses: HashMap::new(),
}, },
}; };
#[allow(clippy::let_and_return)] #[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"), 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 db

View File

@ -92,6 +92,7 @@ impl GameDownloadAgent {
// Blocking // Blocking
pub fn download(&self) -> Result<(), GameDownloadError> { pub fn download(&self) -> Result<(), GameDownloadError> {
self.setup_download()?; self.setup_download()?;
self.set_progress_object_params();
self.run().map_err(|_| GameDownloadError::DownloadError)?; self.run().map_err(|_| GameDownloadError::DownloadError)?;
Ok(()) Ok(())
@ -133,18 +134,6 @@ impl GameDownloadAgent {
} }
let manifest_download = response.json::<DropManifest>().unwrap(); let manifest_download = response.json::<DropManifest>().unwrap();
let length = manifest_download
.values()
.map(|chunk| {
return chunk.lengths.iter().sum::<usize>();
})
.sum::<usize>();
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() { if let Ok(mut manifest) = self.manifest.lock() {
*manifest = Some(manifest_download); *manifest = Some(manifest_download);
@ -154,6 +143,22 @@ impl GameDownloadAgent {
Err(GameDownloadError::Lock) 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> { pub fn generate_contexts(&self) -> Result<(), GameDownloadError> {
let db_lock = DB.borrow_data().unwrap(); let db_lock = DB.borrow_data().unwrap();
let data_base_dir = db_lock.games.install_dirs[self.target_download_dir].clone(); 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(), game_id: game_id.to_string(),
path: path.clone(), path: path.clone(),
checksum: chunk.checksums[i].clone(), checksum: chunk.checksums[i].clone(),
length: *length
}); });
running_offset += *length as u64; running_offset += *length as u64;
} }

View File

@ -46,6 +46,14 @@ pub fn stop_game_download(
.download_manager .download_manager
.cancel_download(game_id); .cancel_download(game_id);
} }
#[tauri::command]
pub fn get_current_write_speed(
state: tauri::State<'_, Mutex<AppState>>,
) {
}
/* /*
fn use_download_agent( fn use_download_agent(
state: tauri::State<'_, Mutex<AppState>>, state: tauri::State<'_, Mutex<AppState>>,

View File

@ -20,4 +20,5 @@ pub struct DropDownloadContext {
pub game_id: String, pub game_id: String,
pub path: PathBuf, pub path: PathBuf,
pub checksum: String, pub checksum: String,
pub length: usize
} }

View File

@ -1,12 +1,13 @@
use std::sync::{ use std::{sync::{
atomic::{AtomicUsize, Ordering}, atomic::{AtomicUsize, Ordering},
Arc, Mutex, Arc, Mutex,
}; }, time::Instant};
#[derive(Clone)] #[derive(Clone)]
pub struct ProgressObject { pub struct ProgressObject {
max: Arc<Mutex<usize>>, max: Arc<Mutex<usize>>,
progress_instances: Arc<Mutex<Vec<Arc<AtomicUsize>>>>, progress_instances: Arc<Mutex<Vec<Arc<AtomicUsize>>>>,
start: Arc<Mutex<Instant>>
} }
impl ProgressObject { impl ProgressObject {
@ -15,8 +16,12 @@ impl ProgressObject {
Self { Self {
max: Arc::new(Mutex::new(max)), max: Arc::new(Mutex::new(max)),
progress_instances: Arc::new(arr), 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 { pub fn sum(&self) -> usize {
self.progress_instances self.progress_instances
.lock() .lock()

View File

@ -17,7 +17,7 @@ use downloads::download_manager_interface::DownloadManager;
use env_logger::Env; use env_logger::Env;
use http::{header::*, response::Builder as ResponseBuilder}; use http::{header::*, response::Builder as ResponseBuilder};
use library::{fetch_game, fetch_library, Game}; use library::{fetch_game, fetch_library, Game};
use log::info; use log::{debug, info};
use remote::{gen_drop_url, use_remote, RemoteAccessError}; use remote::{gen_drop_url, use_remote, RemoteAccessError};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::sync::Arc; use std::sync::Arc;
@ -73,11 +73,13 @@ fn fetch_state(state: tauri::State<'_, Mutex<AppState>>) -> Result<AppState, App
} }
fn setup() -> AppState { fn setup() -> AppState {
debug!("Starting env");
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init(); env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
let games = HashMap::new(); let games = HashMap::new();
let download_manager = Arc::new(DownloadManagerBuilder::build()); let download_manager = Arc::new(DownloadManagerBuilder::build());
debug!("Checking if database is set up");
let is_set_up = DB.database_is_set_up(); let is_set_up = DB.database_is_set_up();
if !is_set_up { if !is_set_up {
return AppState { return AppState {
@ -88,6 +90,8 @@ fn setup() -> AppState {
}; };
} }
debug!("Database is set up");
let (app_status, user) = auth::setup().unwrap(); let (app_status, user) = auth::setup().unwrap();
AppState { AppState {
status: app_status, status: app_status,

View File

@ -0,0 +1,17 @@
use crate::{auth::generate_authorization_header, db::DatabaseImpls, remote::RemoteAccessError, DB};
pub async fn register() -> Result<String, RemoteAccessError> {
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())
}