feat: permanent trace delete (mode:delete) + best-face fields in /traces
- delete_trace: add mode:'delete' → permanent removal (Qdrant points, trace_profiles, tkg_nodes, on-disk files), no undo/history written - list_unassigned_traces: exclude status=deleted (REQ-2), consistent with /traces endpoint - /traces: return real best_face_id/frame/confidence/bbox (highest confidence face per trace) instead of start_frame/avg_confidence - docs: Studio delete Core req + FS detail fix patch + handoff v2
This commit is contained in:
@@ -456,7 +456,12 @@ async fn list_unassigned_traces(
|
||||
if let Some(ref file_uuid) = query.file_uuid {
|
||||
filter_must.push(serde_json::json!({"key": "file_uuid", "match": {"value": file_uuid}}));
|
||||
}
|
||||
let scroll_filter = serde_json::json!({"must": filter_must});
|
||||
let scroll_filter = serde_json::json!({
|
||||
"must": filter_must,
|
||||
"must_not": [
|
||||
{"key": "status", "match": {"value": "deleted"}}
|
||||
]
|
||||
});
|
||||
|
||||
let all_points = qdrant
|
||||
.scroll_all_points("_faces", scroll_filter, 1000)
|
||||
|
||||
+190
-15
@@ -88,6 +88,10 @@ struct TraceInfo {
|
||||
end_time: f64,
|
||||
duration_sec: f64,
|
||||
avg_confidence: f64,
|
||||
best_face_id: i32,
|
||||
best_face_frame: i64,
|
||||
best_face_confidence: f64,
|
||||
best_face_bbox: Option<serde_json::Value>,
|
||||
sample_face_id: Option<String>,
|
||||
thumbnail_url: String,
|
||||
}
|
||||
@@ -112,8 +116,8 @@ async fn list_traces_sorted(
|
||||
let min_faces = req.min_faces.unwrap_or(1);
|
||||
let sort = req.sort_by.as_deref().unwrap_or("first_appearance");
|
||||
let page = req.page.unwrap_or(1).max(1);
|
||||
let page_size = req.page_size.unwrap_or(50).max(1).min(500);
|
||||
let hard_limit = req.limit.unwrap_or(500);
|
||||
let page_size = req.page_size.unwrap_or(50).max(1).min(3000);
|
||||
let hard_limit = req.limit.unwrap_or(500).min(3000);
|
||||
let effective_limit = hard_limit.min(page_size);
|
||||
let db_offset = (page - 1) * page_size;
|
||||
let min_confidence = req.min_confidence.unwrap_or(0.0);
|
||||
@@ -161,6 +165,10 @@ async fn list_traces_sorted(
|
||||
end_frame: i64,
|
||||
avg_confidence: f64,
|
||||
sum_confidence: f64,
|
||||
best_confidence: f64,
|
||||
best_point_id: i64,
|
||||
best_frame: i64,
|
||||
best_bbox: Option<serde_json::Value>,
|
||||
}
|
||||
|
||||
let mut trace_data: HashMap<i32, TraceAgg> = HashMap::new();
|
||||
@@ -169,6 +177,7 @@ async fn list_traces_sorted(
|
||||
let trace_id = payload["trace_id"].as_i64().unwrap_or(0) as i32;
|
||||
let frame = payload["frame"].as_i64().unwrap_or(0);
|
||||
let confidence = payload["confidence"].as_f64().unwrap_or(0.5);
|
||||
let point_id = point["id"].as_i64().unwrap_or(0);
|
||||
|
||||
if confidence < min_confidence || confidence > max_confidence {
|
||||
continue;
|
||||
@@ -180,15 +189,36 @@ async fn list_traces_sorted(
|
||||
end_frame: i64::MIN,
|
||||
avg_confidence: 0.0,
|
||||
sum_confidence: 0.0,
|
||||
best_confidence: 0.0,
|
||||
best_point_id: 0,
|
||||
best_frame: 0,
|
||||
best_bbox: None,
|
||||
});
|
||||
entry.face_count += 1;
|
||||
entry.start_frame = entry.start_frame.min(frame);
|
||||
entry.end_frame = entry.end_frame.max(frame);
|
||||
entry.sum_confidence += confidence;
|
||||
if confidence > entry.best_confidence {
|
||||
entry.best_confidence = confidence;
|
||||
entry.best_point_id = point_id;
|
||||
entry.best_frame = frame;
|
||||
entry.best_bbox = payload.get("bbox").cloned();
|
||||
}
|
||||
}
|
||||
|
||||
// Filter by min_faces and sort
|
||||
let mut traces_vec: Vec<(i32, i64, i64, i64, f64, f64)> = trace_data
|
||||
let mut traces_vec: Vec<(
|
||||
i32,
|
||||
i64,
|
||||
i64,
|
||||
i64,
|
||||
f64,
|
||||
f64,
|
||||
i32,
|
||||
i64,
|
||||
f64,
|
||||
Option<serde_json::Value>,
|
||||
)> = trace_data
|
||||
.into_iter()
|
||||
.filter(|(_, agg)| agg.face_count >= min_faces)
|
||||
.map(|(tid, agg)| {
|
||||
@@ -205,6 +235,10 @@ async fn list_traces_sorted(
|
||||
agg.end_frame,
|
||||
duration,
|
||||
avg_conf,
|
||||
agg.best_point_id as i32,
|
||||
agg.best_frame,
|
||||
agg.best_confidence,
|
||||
agg.best_bbox,
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
@@ -228,18 +262,24 @@ async fn list_traces_sorted(
|
||||
|
||||
let traces: Vec<TraceInfo> = traces_vec
|
||||
.into_iter()
|
||||
.map(|(tid, fc, sf, ef, dur, conf)| TraceInfo {
|
||||
trace_id: tid,
|
||||
face_count: fc,
|
||||
start_frame: sf,
|
||||
end_frame: ef,
|
||||
start_time: sf as f64 / fps,
|
||||
end_time: ef as f64 / fps,
|
||||
duration_sec: dur / fps,
|
||||
avg_confidence: conf,
|
||||
sample_face_id: None,
|
||||
thumbnail_url: format!("/api/v1/file/{}/trace/{}/thumbnail", file_uuid, tid),
|
||||
})
|
||||
.map(
|
||||
|(tid, fc, sf, ef, dur, conf, bfid, bff, bfc, bfb)| TraceInfo {
|
||||
trace_id: tid,
|
||||
face_count: fc,
|
||||
start_frame: sf,
|
||||
end_frame: ef,
|
||||
start_time: sf as f64 / fps,
|
||||
end_time: ef as f64 / fps,
|
||||
duration_sec: dur / fps,
|
||||
avg_confidence: conf,
|
||||
best_face_id: bfid,
|
||||
best_face_frame: bff,
|
||||
best_face_confidence: bfc,
|
||||
best_face_bbox: bfb,
|
||||
sample_face_id: None,
|
||||
thumbnail_url: format!("/api/v1/file/{}/trace/{}/thumbnail", file_uuid, tid),
|
||||
},
|
||||
)
|
||||
.collect();
|
||||
|
||||
Ok(Json(TracesResponse {
|
||||
@@ -1710,6 +1750,11 @@ async fn delete_trace(
|
||||
.get("hard_delete")
|
||||
.and_then(|v| v.as_bool())
|
||||
.unwrap_or(false);
|
||||
let mode = req.get("mode").and_then(|v| v.as_str()).unwrap_or("");
|
||||
|
||||
if mode == "delete" {
|
||||
return delete_trace_permanent(state, &file_uuid, trace_id).await;
|
||||
}
|
||||
|
||||
let qdrant = crate::core::db::qdrant_db::QdrantDb::new();
|
||||
|
||||
@@ -1883,6 +1928,136 @@ async fn delete_trace(
|
||||
}
|
||||
}
|
||||
|
||||
/// Permanently remove a trace (unrecoverable): Qdrant points + trace_profiles
|
||||
/// + tkg_nodes + on-disk files. Does NOT write to trace_operation_history (no undo).
|
||||
async fn delete_trace_permanent(
|
||||
state: crate::api::types::AppState,
|
||||
file_uuid: &str,
|
||||
trace_id: i32,
|
||||
) -> Json<serde_json::Value> {
|
||||
let qdrant = crate::core::db::qdrant_db::QdrantDb::new();
|
||||
let filter = serde_json::json!({
|
||||
"must": [
|
||||
{"key": "file_uuid", "match": {"value": file_uuid}},
|
||||
{"key": "trace_id", "match": {"value": trace_id}}
|
||||
]
|
||||
});
|
||||
|
||||
// 1. Count + delete Qdrant _faces points
|
||||
let points_count = qdrant
|
||||
.scroll_all_points("_faces", filter.clone(), 1000)
|
||||
.await
|
||||
.map(|pts| pts.len() as u64)
|
||||
.unwrap_or(0);
|
||||
|
||||
let qdrant_ok = match qdrant.delete_points_by_filter("_faces", filter).await {
|
||||
Ok(_) => true,
|
||||
Err(e) => {
|
||||
tracing::error!(
|
||||
"[delete_trace] Failed to delete Qdrant points for trace {} in {}: {}",
|
||||
trace_id,
|
||||
file_uuid,
|
||||
e
|
||||
);
|
||||
return Json(serde_json::json!({
|
||||
"success": false,
|
||||
"error": format!("Failed to delete Qdrant points: {}", e)
|
||||
}));
|
||||
}
|
||||
};
|
||||
|
||||
// 2. Delete trace_profiles row
|
||||
let tp_table = crate::core::db::schema::table_name("trace_profiles");
|
||||
let tp_deleted = sqlx::query(&format!(
|
||||
"DELETE FROM {} WHERE file_uuid = $1 AND trace_id = $2",
|
||||
tp_table
|
||||
))
|
||||
.bind(file_uuid)
|
||||
.bind(trace_id)
|
||||
.execute(state.db.pool())
|
||||
.await
|
||||
.map(|r| r.rows_affected())
|
||||
.unwrap_or(0);
|
||||
|
||||
// 3. Delete tkg_nodes face_track rows (face_track_N or trace_N)
|
||||
let nodes_table = crate::core::db::schema::table_name("tkg_nodes");
|
||||
let ext_id_face = format!("face_track_{}", trace_id);
|
||||
let ext_id_trace = format!("trace_{}", trace_id);
|
||||
let tkg_deleted = sqlx::query(&format!(
|
||||
"DELETE FROM {} WHERE file_uuid = $1 AND node_type = 'face_track' AND (external_id = $2 OR external_id = $3)",
|
||||
nodes_table
|
||||
))
|
||||
.bind(file_uuid)
|
||||
.bind(&ext_id_face)
|
||||
.bind(&ext_id_trace)
|
||||
.execute(state.db.pool())
|
||||
.await
|
||||
.map(|r| r.rows_affected())
|
||||
.unwrap_or(0);
|
||||
|
||||
// 4. Best-effort disk removal (output/{file_uuid}/trace_{trace_id}/ and
|
||||
// output/.faces/{file_uuid}/{trace_id}/)
|
||||
let output_dir = crate::core::config::OUTPUT_DIR.as_str();
|
||||
let mut files_removed = true;
|
||||
let mut disk_error: Option<String> = None;
|
||||
|
||||
let trace_dir = crate::core::file_profile::FaceTraceProfile::trace_dir(
|
||||
output_dir,
|
||||
file_uuid,
|
||||
trace_id as i64,
|
||||
);
|
||||
if trace_dir.exists() {
|
||||
if let Err(e) = std::fs::remove_dir_all(&trace_dir) {
|
||||
files_removed = false;
|
||||
disk_error = Some(format!("trace dir: {}", e));
|
||||
tracing::error!(
|
||||
"[delete_trace] Failed to remove {}: {}",
|
||||
trace_dir.display(),
|
||||
e
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
let faces_dir = std::path::PathBuf::from(output_dir)
|
||||
.join(".faces")
|
||||
.join(file_uuid)
|
||||
.join(trace_id.to_string());
|
||||
if faces_dir.exists() {
|
||||
if let Err(e) = std::fs::remove_dir_all(&faces_dir) {
|
||||
files_removed = false;
|
||||
disk_error = Some(format!("faces dir: {}", e));
|
||||
tracing::error!(
|
||||
"[delete_trace] Failed to remove {}: {}",
|
||||
faces_dir.display(),
|
||||
e
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
tracing::info!(
|
||||
"[delete_trace] Permanently deleted trace {} in {} (qdrant: {}, profiles: {}, tkg: {}, files: {})",
|
||||
trace_id,
|
||||
file_uuid,
|
||||
points_count,
|
||||
tp_deleted,
|
||||
tkg_deleted,
|
||||
files_removed
|
||||
);
|
||||
|
||||
Json(serde_json::json!({
|
||||
"success": true,
|
||||
"file_uuid": file_uuid,
|
||||
"trace_id": trace_id,
|
||||
"mode": "delete",
|
||||
"qdrant_points_deleted": points_count,
|
||||
"trace_profiles_deleted": tp_deleted,
|
||||
"tkg_nodes_deleted": tkg_deleted,
|
||||
"files_removed": files_removed,
|
||||
"files_removed_error": disk_error,
|
||||
"qdrant_ok": qdrant_ok,
|
||||
}))
|
||||
}
|
||||
|
||||
async fn merge_trace(
|
||||
State(state): State<crate::api::types::AppState>,
|
||||
Path((file_uuid, trace_id, target_trace_id)): Path<(String, i32, i32)>,
|
||||
|
||||
Reference in New Issue
Block a user