feat: upload history/retry UI and low-disk recording guardrails
This commit is contained in:
1 parent
a2800c3664
commit
9c221cb101
7 files changed
+341
-17
No files matched your search
+29
-1
@@ -1,4 +1,4 @@
|
||||
use crate::models::{Clip, ClipSource, GameEventType, UploadProvider};
|
||||
use crate::models::{Clip, ClipSource, GameEventType, UploadHistoryEntry, UploadProvider};
|
||||
use rusqlite::{params, Connection};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||
@@ -176,6 +176,31 @@ impl ClipDatabase {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn load_upload_history(&self) -> rusqlite::Result<Vec<UploadHistoryEntry>> {
|
||||
let mut statement = self.connection.prepare(
|
||||
"
|
||||
SELECT id, clip_id, provider, status, url, error, attempted_at_ms
|
||||
FROM upload_history
|
||||
ORDER BY attempted_at_ms DESC
|
||||
LIMIT 200
|
||||
",
|
||||
)?;
|
||||
|
||||
let rows = statement.query_map([], |row| {
|
||||
Ok(UploadHistoryEntry {
|
||||
id: row.get(0)?,
|
||||
clip_id: row.get(1)?,
|
||||
provider: row.get(2)?,
|
||||
status: row.get(3)?,
|
||||
url: row.get(4)?,
|
||||
error: row.get(5)?,
|
||||
attempted_at: millis_to_system_time(row.get(6)?),
|
||||
})
|
||||
})?;
|
||||
|
||||
rows.collect()
|
||||
}
|
||||
|
||||
fn column_exists(&self, table: &str, column: &str) -> rusqlite::Result<bool> {
|
||||
let mut statement = self
|
||||
.connection
|
||||
@@ -293,6 +318,9 @@ mod tests {
|
||||
SystemTime::now(),
|
||||
)
|
||||
.expect("upload history");
|
||||
let history = database.load_upload_history().expect("load upload history");
|
||||
assert_eq!(history.len(), 1);
|
||||
assert_eq!(history[0].clip_id, "clip-1");
|
||||
database.delete_clip("clip-1").expect("delete");
|
||||
assert!(database.load_clips().expect("load empty").is_empty());
|
||||
let _ = std::fs::remove_file(db_path);
|
||||
|
||||
@@ -111,6 +111,17 @@ pub struct Clip {
|
||||
pub upload_provider: Option<UploadProvider>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct UploadHistoryEntry {
|
||||
pub id: i64,
|
||||
pub clip_id: String,
|
||||
pub provider: String,
|
||||
pub status: String,
|
||||
pub url: Option<String>,
|
||||
pub error: Option<String>,
|
||||
pub attempted_at: SystemTime,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum RecordingStatus {
|
||||
WaitingForGame,
|
||||
|
||||
@@ -114,6 +114,69 @@ pub struct StorageCleanupReport {
|
||||
pub remaining_bytes: u64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum StorageLevel {
|
||||
Ok,
|
||||
Warning,
|
||||
Critical,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct StorageSpace {
|
||||
pub total_bytes: u64,
|
||||
pub available_bytes: u64,
|
||||
pub level: StorageLevel,
|
||||
}
|
||||
|
||||
impl StorageSpace {
|
||||
pub fn free_gb(&self) -> f64 {
|
||||
self.available_bytes as f64 / (1024.0 * 1024.0 * 1024.0)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn check_storage_space(root: &Path) -> StorageSpace {
|
||||
let disks = sysinfo::Disks::new_with_refreshed_list();
|
||||
let mount = disks
|
||||
.list()
|
||||
.iter()
|
||||
.find(|disk| path_is_on_mount(root, disk.mount_point()))
|
||||
.or_else(|| disks.list().first());
|
||||
let valid = |disk: Option<&sysinfo::Disk>| -> Option<(u64, u64)> {
|
||||
disk.map(|disk| (disk.total_space(), disk.available_space()))
|
||||
};
|
||||
let (total, available) = valid(mount).unwrap_or((0, 0));
|
||||
let warning_at = bytes_for_gb(5);
|
||||
let critical_at = bytes_for_gb(2);
|
||||
let level = if available < critical_at {
|
||||
StorageLevel::Critical
|
||||
} else if available < warning_at {
|
||||
StorageLevel::Warning
|
||||
} else {
|
||||
StorageLevel::Ok
|
||||
};
|
||||
StorageSpace {
|
||||
total_bytes: total,
|
||||
available_bytes: available,
|
||||
level,
|
||||
}
|
||||
}
|
||||
|
||||
fn bytes_for_gb(gb: u64) -> u64 {
|
||||
gb * 1024 * 1024 * 1024
|
||||
}
|
||||
|
||||
fn path_is_on_mount(path: &Path, mount: &Path) -> bool {
|
||||
let path = path
|
||||
.to_str()
|
||||
.map(|value| value.to_ascii_lowercase())
|
||||
.unwrap_or_default();
|
||||
let mount = mount
|
||||
.to_str()
|
||||
.map(|value| value.to_ascii_lowercase())
|
||||
.unwrap_or_default();
|
||||
path == mount || path.starts_with(&mount)
|
||||
}
|
||||
|
||||
pub fn cleanup_oldest_files_until_under_limit(
|
||||
root: &Path,
|
||||
max_bytes: u64,
|
||||
@@ -206,4 +269,27 @@ mod tests {
|
||||
assert_eq!(report.remaining_bytes, 10);
|
||||
let _ = fs::remove_dir_all(root);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn storage_space_reports_metrics_for_root() {
|
||||
let root = env::temp_dir();
|
||||
let space = check_storage_space(&root);
|
||||
assert!(space.total_bytes > 0);
|
||||
assert!(space.available_bytes <= space.total_bytes);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn path_is_on_mount_matches_drive_prefix() {
|
||||
let root = if cfg!(windows) {
|
||||
Path::new("C:/Users/ClipForge/Clips")
|
||||
} else {
|
||||
Path::new("/home/clipforge/clips")
|
||||
};
|
||||
let mount = if cfg!(windows) {
|
||||
Path::new("C:")
|
||||
} else {
|
||||
Path::new("/home")
|
||||
};
|
||||
assert!(path_is_on_mount(root, mount));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user