feat: expose face artifact completion counts (trace_profile/key_frame/key_face) in scan + traces APIs

This commit is contained in:
Accusys
2026-08-16 23:09:12 +08:00
parent e2b9999cba
commit dda0b7cc7f
3 changed files with 124 additions and 0 deletions
+89
View File
@@ -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_<number>`
if !dir_name.starts_with("trace_") || !path.is_dir() {
continue;
}
if dir_name[6..].parse::<i64>().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");