fix: face group name read consistency, sync_file_status fix, cleanup ghost records, identity_agent replaced with face_dedup
- get_face_groups_handler: COALESCE(tp.name, tn.label) for name consistency - sync_file_status: compare JSON vs pre_chunks (not chunk table) - face consistency: compare frames.len() not total_faces - cleanup 2 ghost records with NULL file_name/file_path - replace identity_agent with face_dedup in pipeline stages - remove identity_agent_api.rs and all references - update required_processors to match actual processors - update AGENTS.md with team responsibilities - add Studio pipeline changes documentation
This commit is contained in:
+234
-27
@@ -140,6 +140,8 @@ struct RegisterFileResponse {
|
||||
registration_time: Option<String>,
|
||||
already_exists: bool,
|
||||
message: String,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
warning: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
@@ -214,6 +216,7 @@ async fn register_single_file(
|
||||
registration_time: None,
|
||||
already_exists: false,
|
||||
message: format!("File not found: {}", file_path),
|
||||
warning: None,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -245,6 +248,7 @@ async fn register_single_file(
|
||||
registration_time: None,
|
||||
already_exists: false,
|
||||
message: format!("DB init failed: {}", e),
|
||||
warning: None,
|
||||
};
|
||||
}
|
||||
};
|
||||
@@ -369,6 +373,7 @@ async fn register_single_file(
|
||||
registration_time: rt,
|
||||
already_exists: true,
|
||||
message: format!("Content already registered: {}", epath),
|
||||
warning: None,
|
||||
};
|
||||
}
|
||||
return RegisterFileResponse {
|
||||
@@ -385,6 +390,7 @@ async fn register_single_file(
|
||||
registration_time: None,
|
||||
already_exists: true,
|
||||
message: "Content already registered (identical file)".to_string(),
|
||||
warning: None,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -620,6 +626,7 @@ async fn register_single_file(
|
||||
registration_time: None,
|
||||
already_exists: false,
|
||||
message: "File registered successfully".to_string(),
|
||||
warning: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -627,6 +634,13 @@ async fn register_file(
|
||||
State(state): State<AppState>,
|
||||
Json(req): Json<RegisterFileRequest>,
|
||||
) -> Result<Json<RegisterFileResponse>, StatusCode> {
|
||||
let worker_running = super::health::check_worker_alive(&state).await;
|
||||
let worker_warning = if !worker_running {
|
||||
Some("Worker is not running - file will be registered but not processed automatically".to_string())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let file_path = req.file_path.clone();
|
||||
let pattern = req.pattern;
|
||||
|
||||
@@ -650,6 +664,7 @@ async fn register_file(
|
||||
"Pattern requires a directory, but path is not a dir: {}",
|
||||
file_path
|
||||
),
|
||||
warning: None,
|
||||
}));
|
||||
}
|
||||
let re = regex::Regex::new(pat).map_err(|e| {
|
||||
@@ -713,6 +728,7 @@ async fn register_file(
|
||||
"Batch register: {} registered, {} skipped, {} failed",
|
||||
registered, skipped, failed
|
||||
),
|
||||
warning: worker_warning.clone(),
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -765,8 +781,12 @@ async fn register_file(
|
||||
}
|
||||
}
|
||||
|
||||
let resp =
|
||||
let mut resp =
|
||||
register_single_file(&state, &file_path, req.user_id, req.content_hash, req.force).await;
|
||||
|
||||
if worker_warning.is_some() {
|
||||
resp.warning = worker_warning;
|
||||
}
|
||||
|
||||
if resp.success
|
||||
&& !resp.already_exists
|
||||
@@ -805,7 +825,7 @@ async fn register_file(
|
||||
.init_processing_status(&auto_uuid, all_procs, total as u64)
|
||||
.await;
|
||||
let _ = sqlx::query(&format!(
|
||||
"UPDATE {} SET status = 'processing' WHERE file_uuid = $1",
|
||||
"UPDATE {} SET status = 'scanning' WHERE file_uuid = $1",
|
||||
schema::table_name("videos")
|
||||
))
|
||||
.bind(&auto_uuid)
|
||||
@@ -1617,42 +1637,229 @@ async fn sync_file_status(
|
||||
use crate::core::config::OUTPUT_DIR;
|
||||
|
||||
let output_dir = OUTPUT_DIR.as_str();
|
||||
let required_processors = ["face.json", "asrx.json", "ocr.json", "pose.json", "appearance.json"];
|
||||
let chunk_table = schema::table_name("chunk");
|
||||
let tp_table = schema::table_name("trace_profiles");
|
||||
let tkg_nodes_table = schema::table_name("tkg_nodes");
|
||||
let tkg_edges_table = schema::table_name("tkg_edges");
|
||||
|
||||
let mut completed_count = 0;
|
||||
for processor in &required_processors {
|
||||
let path = std::path::Path::new(output_dir)
|
||||
.join(format!("{}.{}", file_uuid, processor));
|
||||
if path.exists() {
|
||||
completed_count += 1;
|
||||
// 檢查 processor JSON 是否存在
|
||||
let processors = [
|
||||
("asr", "asr.json"),
|
||||
("asrx", "asrx.json"),
|
||||
("ocr", "ocr.json"),
|
||||
("pose", "pose.json"),
|
||||
("cut", "cut.json"),
|
||||
("face", "face.json"),
|
||||
("face_cluster", "face_cluster.json"),
|
||||
];
|
||||
|
||||
let mut processor_status = serde_json::Map::new();
|
||||
let mut errors = Vec::new();
|
||||
let mut all_consistent = true;
|
||||
let mut any_json_exists = false;
|
||||
|
||||
for (name, filename) in &processors {
|
||||
let path = std::path::Path::new(output_dir).join(format!("{}.{}", file_uuid, filename));
|
||||
let json_exists = path.exists();
|
||||
let mut consistent = false;
|
||||
|
||||
if json_exists {
|
||||
any_json_exists = true;
|
||||
|
||||
// 讀取 JSON 內容
|
||||
match std::fs::read_to_string(&path) {
|
||||
Ok(content) => {
|
||||
if let Ok(json_data) = serde_json::from_str::<serde_json::Value>(&content) {
|
||||
// 檢查 DB 與 JSON 是否一致
|
||||
let pre_table = schema::table_name("pre_chunks");
|
||||
consistent = match *name {
|
||||
"asr" => {
|
||||
let json_count = json_data.get("segments").and_then(|s| s.as_array()).map(|a| a.len()).unwrap_or(0);
|
||||
let is_no_audio = json_data.get("status").and_then(|s| s.as_str()) == Some("no_audio_track")
|
||||
|| json_data.get("status").and_then(|s| s.as_str()) == Some("silent_audio");
|
||||
let db_count: i64 = sqlx::query_scalar(&format!(
|
||||
"SELECT COUNT(*) FROM {} WHERE file_uuid = $1 AND processor_type = 'asr'", pre_table
|
||||
))
|
||||
.bind(&file_uuid).fetch_one(state.db.pool()).await.unwrap_or(0);
|
||||
is_no_audio || (json_count as i64) == db_count
|
||||
}
|
||||
"asrx" => {
|
||||
let json_count = json_data.get("segments").and_then(|s| s.as_array()).map(|a| a.len()).unwrap_or(0);
|
||||
let db_count: i64 = sqlx::query_scalar(&format!(
|
||||
"SELECT COUNT(*) FROM {} WHERE file_uuid = $1 AND processor_type = 'asrx'", pre_table
|
||||
))
|
||||
.bind(&file_uuid).fetch_one(state.db.pool()).await.unwrap_or(0);
|
||||
(json_count as i64) == db_count
|
||||
}
|
||||
"ocr" => {
|
||||
let json_count = json_data.get("frames").and_then(|s| s.as_array()).map(|a| a.len()).unwrap_or(0);
|
||||
let db_count: i64 = sqlx::query_scalar(&format!(
|
||||
"SELECT COUNT(*) FROM {} WHERE file_uuid = $1 AND processor_type = 'ocr'", pre_table
|
||||
))
|
||||
.bind(&file_uuid).fetch_one(state.db.pool()).await.unwrap_or(0);
|
||||
(json_count as i64) == db_count
|
||||
}
|
||||
"cut" => {
|
||||
let json_count = json_data.get("scenes").and_then(|s| s.as_array()).map(|a| a.len()).unwrap_or(0);
|
||||
let db_count: i64 = sqlx::query_scalar(&format!(
|
||||
"SELECT COUNT(*) FROM {} WHERE file_uuid = $1 AND processor_type = 'cut'", pre_table
|
||||
))
|
||||
.bind(&file_uuid).fetch_one(state.db.pool()).await.unwrap_or(0);
|
||||
(json_count as i64) == db_count
|
||||
}
|
||||
"face" => {
|
||||
let json_frame_count = json_data.get("frames").and_then(|f| f.as_array()).map(|a| a.len()).unwrap_or(0);
|
||||
let is_no_faces = json_data.get("status").and_then(|s| s.as_str()) == Some("no_faces");
|
||||
let db_count: i64 = sqlx::query_scalar(&format!(
|
||||
"SELECT COUNT(*) FROM {} WHERE file_uuid = $1 AND processor_type = 'face'", pre_table
|
||||
))
|
||||
.bind(&file_uuid).fetch_one(state.db.pool()).await.unwrap_or(0);
|
||||
is_no_faces || json_frame_count == (db_count as usize)
|
||||
}
|
||||
"face_cluster" => {
|
||||
// cluster 數量可能不等於 trace 數量,只要 JSON 存在就視為一致
|
||||
true
|
||||
}
|
||||
"pose" => {
|
||||
// pose 通常不需要入庫 chunks,只要 JSON 存在就視為一致
|
||||
true
|
||||
}
|
||||
_ => true,
|
||||
};
|
||||
} else {
|
||||
errors.push(serde_json::json!({
|
||||
"processor": name,
|
||||
"error": "JSON parse failed"
|
||||
}));
|
||||
consistent = false;
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
errors.push(serde_json::json!({
|
||||
"processor": name,
|
||||
"error": format!("Read failed: {}", e)
|
||||
}));
|
||||
consistent = false;
|
||||
}
|
||||
}
|
||||
|
||||
if !consistent {
|
||||
all_consistent = false;
|
||||
}
|
||||
}
|
||||
|
||||
processor_status.insert(name.to_string(), serde_json::json!({
|
||||
"json_exists": json_exists,
|
||||
"consistent": consistent
|
||||
}));
|
||||
}
|
||||
|
||||
let all_complete = completed_count == required_processors.len();
|
||||
let new_status = if all_complete { "completed" } else { "processing" };
|
||||
// 如果有錯誤,回傳錯誤
|
||||
if !errors.is_empty() {
|
||||
return Ok(Json(serde_json::json!({
|
||||
"success": false,
|
||||
"file_uuid": file_uuid,
|
||||
"status": "error",
|
||||
"errors": errors
|
||||
})));
|
||||
}
|
||||
|
||||
// 檢查 DB 進階狀態
|
||||
let has_sentence: bool = sqlx::query_scalar(&format!(
|
||||
"SELECT 1 FROM {} WHERE file_uuid = $1 AND chunk_type = 'sentence' LIMIT 1", chunk_table
|
||||
))
|
||||
.bind(&file_uuid)
|
||||
.fetch_optional(state.db.pool())
|
||||
.await
|
||||
.unwrap_or(None)
|
||||
.unwrap_or(0) > 0;
|
||||
|
||||
let has_tkg_nodes: bool = sqlx::query_scalar(&format!(
|
||||
"SELECT 1 FROM {} WHERE file_uuid = $1 LIMIT 1", tkg_nodes_table
|
||||
))
|
||||
.bind(&file_uuid)
|
||||
.fetch_optional(state.db.pool())
|
||||
.await
|
||||
.unwrap_or(None)
|
||||
.unwrap_or(0) > 0;
|
||||
|
||||
let has_tkg_edges: bool = sqlx::query_scalar(&format!(
|
||||
"SELECT 1 FROM {} WHERE file_uuid = $1 LIMIT 1", tkg_edges_table
|
||||
))
|
||||
.bind(&file_uuid)
|
||||
.fetch_optional(state.db.pool())
|
||||
.await
|
||||
.unwrap_or(None)
|
||||
.unwrap_or(0) > 0;
|
||||
|
||||
// 狀態判斷
|
||||
let new_status = if all_consistent && any_json_exists {
|
||||
// 所有 JSON 都存在且與 DB 一致
|
||||
if has_tkg_edges {
|
||||
"agent_ready"
|
||||
} else {
|
||||
"completed" // 即使 0 筆也算完成
|
||||
}
|
||||
} else if any_json_exists {
|
||||
// 部分 JSON 存在但不一致 → 入庫中
|
||||
"scanning"
|
||||
} else {
|
||||
// 無 JSON → 未開始
|
||||
"registered"
|
||||
};
|
||||
|
||||
// 更新 DB status
|
||||
let videos_table = schema::table_name("videos");
|
||||
let result = sqlx::query(&format!(
|
||||
sqlx::query(&format!(
|
||||
"UPDATE {} SET status = $1, updated_at = NOW() WHERE file_uuid = $2",
|
||||
videos_table
|
||||
))
|
||||
.bind(new_status)
|
||||
.bind(&file_uuid)
|
||||
.execute(state.db.pool())
|
||||
.await;
|
||||
.await
|
||||
.map_err(|e| {
|
||||
tracing::error!("[SYNC_STATUS] Update failed: {}", e);
|
||||
StatusCode::INTERNAL_SERVER_ERROR
|
||||
})?;
|
||||
|
||||
match result {
|
||||
Ok(res) if res.rows_affected() > 0 => Ok(Json(serde_json::json!({
|
||||
"success": true,
|
||||
"file_uuid": file_uuid,
|
||||
"status": new_status,
|
||||
"processors_complete": completed_count,
|
||||
"processors_total": required_processors.len()
|
||||
}))),
|
||||
Ok(_) => Err(StatusCode::NOT_FOUND),
|
||||
Err(e) => {
|
||||
tracing::error!("[SYNC_STATUS] Update failed: {}", e);
|
||||
Err(StatusCode::INTERNAL_SERVER_ERROR)
|
||||
}
|
||||
}
|
||||
// 查詢 monitor_jobs 取得 worker 狀態
|
||||
let monitor_jobs_table = schema::table_name("monitor_jobs");
|
||||
let job: Option<(String, Option<String>, i32, i32, Vec<String>, Vec<String>, Option<chrono::NaiveDateTime>)> =
|
||||
sqlx::query_as(&format!(
|
||||
"SELECT status, current_processor, progress_total, progress_current, \
|
||||
completed_processors, failed_processors, updated_at \
|
||||
FROM {} WHERE uuid = $1 ORDER BY created_at DESC LIMIT 1",
|
||||
monitor_jobs_table
|
||||
))
|
||||
.bind(&file_uuid)
|
||||
.fetch_optional(state.db.pool())
|
||||
.await
|
||||
.ok()
|
||||
.flatten();
|
||||
|
||||
let worker_info = if let Some((status, current_proc, total, current, completed, failed, updated)) = job {
|
||||
serde_json::json!({
|
||||
"has_job": true,
|
||||
"job_status": status,
|
||||
"current_processor": current_proc,
|
||||
"progress_total": total,
|
||||
"progress_current": current,
|
||||
"completed_processors": completed,
|
||||
"failed_processors": failed,
|
||||
"updated_at": updated.map(|t| t.format("%Y-%m-%dT%H:%M:%S").to_string())
|
||||
})
|
||||
} else {
|
||||
serde_json::json!({
|
||||
"has_job": false
|
||||
})
|
||||
};
|
||||
|
||||
Ok(Json(serde_json::json!({
|
||||
"success": true,
|
||||
"file_uuid": file_uuid,
|
||||
"status": new_status,
|
||||
"processors": processor_status,
|
||||
"worker": worker_info
|
||||
})))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user