feat: sync_trace_label unified function + fix is_tkg_done

- Created trace_sync module with sync_trace_label() to sync labels across:
  1. tkg_nodes.label (PostgreSQL)
  2. trace_profiles.name (PostgreSQL)
  3. trace_profile.json label (FS)
  4. Qdrant _faces payload.label (NEW - was missing)

- Fixed is_tkg_done: allow 0 trace dirs if tkg_nodes exist
  (prevents permanent stuck in 'Scanning' status)

- Updated Face Cluster to use sync_trace_label
- Updated merge_trace to use sync_trace_label + cleanup source dir

- Created Face_Operations_Complete.md reference document
  (consolidates all face-related docs into single source of truth)
This commit is contained in:
Accusys
2026-10-18 14:15:12 +08:00
parent f3013c8509
commit 58431d1d8d
5 changed files with 622 additions and 53 deletions
+37 -53
View File
@@ -2052,11 +2052,13 @@ async fn process_job(&self, job: crate::core::db::MonitorJob) -> Result<()> {
.count();
if trace_count == 0 {
// 有 tkg_nodes 記錄但沒有 trace 目錄 — 視為完成(可事後修復)
// 這避免文件永久卡在 "Scanning" 狀態
tracing::warn!(
"[TKG] Database has {} records but no trace dirs for {}, returning false",
count, uuid
"[TKG] {} has {} nodes but no trace dirs, proceeding anyway (trace dirs can be backfilled later)",
uuid, count
);
return false;
return true;
}
tracing::info!(
@@ -2066,11 +2068,12 @@ async fn process_job(&self, job: crate::core::db::MonitorJob) -> Result<()> {
return true;
}
// 無法讀取目錄 — 如果有 tkg_nodes 記錄,仍視為完成
tracing::warn!(
"[TKG] Database has {} records but cannot read trace dir for {}, returning false",
count, uuid
"[TKG] {} has {} nodes but cannot read trace dir, proceeding anyway",
uuid, count
);
false
true
}
/// 檢查所有入庫步驟是否已完成(與 ingestion-status endpoint 同步邏輯)
@@ -4405,57 +4408,38 @@ async fn process_job(&self, job: crate::core::db::MonitorJob) -> Result<()> {
}
}
let tkg_table = crate::core::db::schema::table_name("tkg_nodes");
// Update traces that ARE in clusters
// Update traces that ARE in clusters using sync_trace_label
let qdrant = crate::core::db::qdrant_db::QdrantDb::new();
for (trace_id, cluster_id) in &trace_to_cluster {
let external_id = format!("trace_{}", trace_id);
let label = cluster_id.clone();
let rows_updated = sqlx::query(&format!(
"UPDATE {} SET label = $1 WHERE file_uuid = $2 AND node_type = 'face_track' AND external_id = $3",
tkg_table
))
.bind(&label)
.bind(uuid)
.bind(&external_id)
.execute(db.pool())
// 使用統一同步函數(tkg_nodes + trace_profiles + FS + Qdrant)
match crate::core::trace_sync::sync_trace_label(
db.pool(),
&qdrant,
uuid,
*trace_id as i32,
&label,
)
.await
.context("Failed to update tkg_nodes label")?
.rows_affected();
if rows_updated == 0 {
sqlx::query(&format!(
"INSERT INTO {} (file_uuid, node_type, external_id, label, properties) VALUES ($1, 'face_track', $2, $3, $4)",
tkg_table
))
.bind(uuid)
.bind(&external_id)
.bind(&label)
.bind(serde_json::json!({"trace_id": trace_id}))
.execute(db.pool())
.await
.context("Failed to insert tkg_nodes")?;
}
// Sync name to trace_profiles DB + on-disk JSON
let _ =
schema::upsert_trace_profile(db.pool(), uuid, *trace_id as i32, &label, None).await;
// Update on-disk trace_profile.json
let output_dir = crate::core::config::OUTPUT_DIR.as_str();
let profile_path = crate::core::file_profile::FaceTraceProfile::profile_path(
output_dir, uuid, *trace_id,
);
if profile_path.exists() {
if let Ok(content) = std::fs::read_to_string(&profile_path) {
if let Ok(mut profile) = serde_json::from_str::<serde_json::Value>(&content) {
profile["label"] = serde_json::json!(label);
let _ = std::fs::write(
&profile_path,
serde_json::to_string_pretty(&profile).unwrap_or_default(),
);
}
{
Ok(sync_result) => {
tracing::info!(
"[FaceCluster] sync_trace_label for trace_{}: tkg={}, tp={}, fs={}, qdrant={} ({} points)",
trace_id,
sync_result.tkg_nodes_updated,
sync_result.trace_profiles_updated,
sync_result.fs_updated,
sync_result.qdrant_updated,
sync_result.qdrant_points_count
);
}
Err(e) => {
tracing::warn!(
"[FaceCluster] sync_trace_label failed for trace_{}: {}",
trace_id,
e
);
}
}
}