diff --git a/src/api/scan.rs b/src/api/scan.rs index 66558167..f8624da6 100644 --- a/src/api/scan.rs +++ b/src/api/scan.rs @@ -9,6 +9,7 @@ use serde::{Deserialize, Serialize}; use super::types::AppState; use crate::core::db::schema; +use crate::core::file_profile::{count_face_artifacts, FaceArtifactsCounts}; /// Comprehensive file stats endpoint — provides all data sources for frontend transparency /// Combines: JSON file status + PostgreSQL counts + Qdrant collections + TKG stats + Identity Agent stats @@ -27,6 +28,8 @@ struct FileStatsResponse { tkg: TkgFileStats, // Face Dedup stats face_dedup: FaceDedupStats, + // On-disk face artifact counts (vs expected) + face_artifacts: FaceArtifactsCounts, } #[derive(Debug, Serialize)] @@ -1056,6 +1059,19 @@ async fn get_file_stats( consistent: (face_clusters as i64) == face_track_count, }; + // 7. On-disk face artifact counts (trace dirs + key_frame/key_face/profile) + let trace_profiles_table = schema::table_name("trace_profiles"); + let expected_traces: i64 = sqlx::query_scalar(&format!( + "SELECT COUNT(*) FROM {} WHERE file_uuid = $1", + trace_profiles_table + )) + .bind(&file_uuid) + .fetch_one(pool) + .await + .unwrap_or(0); + let mut face_artifacts = count_face_artifacts(output_dir, &file_uuid); + face_artifacts.expected_traces = expected_traces; + Ok(Json(FileStatsResponse { file_uuid: file_uuid_str, file_name, @@ -1065,6 +1081,7 @@ async fn get_file_stats( qdrant: qdrant_stats, tkg, face_dedup, + face_artifacts, })) } diff --git a/src/api/trace_agent_api.rs b/src/api/trace_agent_api.rs index 684550e7..67e840c3 100644 --- a/src/api/trace_agent_api.rs +++ b/src/api/trace_agent_api.rs @@ -11,6 +11,7 @@ use std::sync::Arc; use crate::core::db::schema::t; use crate::core::db::PostgresDb; +use crate::core::file_profile::{count_face_artifacts, FaceArtifactsCounts}; pub fn trace_agent_routes() -> Router { Router::new() @@ -112,6 +113,8 @@ struct TracesResponse { page: i64, page_size: i64, traces: Vec, + // On-disk face artifact counts (vs expected) for completion transparency + face_artifacts: FaceArtifactsCounts, } async fn list_traces_sorted( @@ -317,6 +320,20 @@ async fn list_traces_sorted( ) .collect(); + // On-disk face artifact counts (vs expected) for completion transparency + let trace_profiles_table = crate::core::db::schema::table_name("trace_profiles"); + let expected_traces: i64 = sqlx::query_scalar(&format!( + "SELECT COUNT(*) FROM {} WHERE file_uuid = $1", + trace_profiles_table + )) + .bind(&file_uuid) + .fetch_one(state.db.pool()) + .await + .unwrap_or(0); + let mut face_artifacts = + count_face_artifacts(crate::core::config::OUTPUT_DIR.as_str(), &file_uuid); + face_artifacts.expected_traces = expected_traces; + Ok(Json(TracesResponse { success: true, file_uuid, @@ -326,6 +343,7 @@ async fn list_traces_sorted( page, page_size, traces, + face_artifacts, })) } diff --git a/src/core/file_profile.rs b/src/core/file_profile.rs index 16904559..ef430100 100644 --- a/src/core/file_profile.rs +++ b/src/core/file_profile.rs @@ -529,10 +529,99 @@ impl FaceTraceProfile { } } +/// On-disk face artifact counts for a file, used for completion transparency. +/// `expected_traces` is the authoritative count from DB/Qdrant; the *_files +/// fields reflect what actually exists on disk (TKG build may still be running). +#[derive(Debug, Clone, Default, Serialize)] +pub struct FaceArtifactsCounts { + /// Authoritative number of traces for this file (DB trace_profiles count). + pub expected_traces: i64, + /// Number of `trace_N` directories present on disk. + pub trace_dirs: i64, + /// Number of `trace_profile.json` files present on disk. + pub trace_profiles: i64, + /// Number of `key_frame.jpg` files present on disk. + pub key_frames: i64, + /// Number of `key_face.jpg` files present on disk. + pub key_faces: i64, +} + +/// Scan `output/{file_uuid}/` once and count trace dirs + required artifacts. +/// Returns all-zero counts when the directory does not exist (not yet processed). +pub fn count_face_artifacts(output_dir: &str, file_uuid: &str) -> FaceArtifactsCounts { + let mut counts = FaceArtifactsCounts::default(); + let base = Path::new(output_dir).join(file_uuid); + let entries = match std::fs::read_dir(&base) { + Ok(e) => e, + Err(_) => return counts, + }; + + for entry in entries.flatten() { + let path = entry.path(); + let dir_name = match path.file_name().and_then(|n| n.to_str()) { + Some(n) => n, + None => continue, + }; + // Only count directories named `trace_` + if !dir_name.starts_with("trace_") || !path.is_dir() { + continue; + } + if dir_name[6..].parse::().is_err() { + continue; + } + counts.trace_dirs += 1; + if path.join("trace_profile.json").is_file() { + counts.trace_profiles += 1; + } + if path.join("key_frame.jpg").is_file() { + counts.key_frames += 1; + } + if path.join("key_face.jpg").is_file() { + counts.key_faces += 1; + } + } + counts +} + #[cfg(test)] mod tests { use super::*; + #[test] + fn test_count_face_artifacts() { + let tmp = std::env::temp_dir().join("momentry_count_face_artifacts_test"); + let base = tmp.join("fileabc"); + let t0 = base.join("trace_0"); + let t1 = base.join("trace_1"); + let t2 = base.join("trace_2"); + std::fs::create_dir_all(&t0).unwrap(); + std::fs::create_dir_all(&t1).unwrap(); + std::fs::create_dir_all(&t2).unwrap(); + // trace_0: full set + std::fs::write(t0.join("trace_profile.json"), "{}").unwrap(); + std::fs::write(t0.join("key_frame.jpg"), b"x").unwrap(); + std::fs::write(t0.join("key_face.jpg"), b"x").unwrap(); + // trace_1: profile only (key_frame/key_face missing) + std::fs::write(t1.join("trace_profile.json"), "{}").unwrap(); + // trace_2: empty dir + // non-trace dir should be ignored + std::fs::create_dir_all(base.join("not_a_trace")).unwrap(); + + let c = count_face_artifacts(tmp.to_str().unwrap(), "fileabc"); + assert_eq!(c.trace_dirs, 3); + assert_eq!(c.trace_profiles, 2); + assert_eq!(c.key_frames, 1); + assert_eq!(c.key_faces, 1); + assert_eq!(c.expected_traces, 0); + + // Missing file dir -> all zeros + let c0 = count_face_artifacts(tmp.to_str().unwrap(), "does_not_exist"); + assert_eq!(c0.trace_dirs, 0); + assert_eq!(c0.key_frames, 0); + + let _ = std::fs::remove_dir_all(&tmp); + } + #[test] fn test_profile_path() { let path = FileProfile::profile_path("/output", "abc123");