From 1672a042afa31a1905d5a329ee82c3206ea091de Mon Sep 17 00:00:00 2001 From: Accusys Date: Sun, 4 Oct 2026 18:22:18 +0800 Subject: [PATCH] fix: G-4 (A-13) status consistency issue MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Problem: - GET /api/v1/files (list) returns DB status - GET /api/v1/file/:uuid (detail) returns DB status - DB status may be stale - Results in inconsistency Solution: 1. Add compute_file_status() helper function - Checks processing_status->>'phase' - Checks processor JSON files - Derives actual status 2. Fix get_file_detail: - Always use computed status - Ensures accuracy for single file queries 3. Fix list_files: - Add sync_status query parameter - sync_status=true: compute status for each file - sync_status=false (default): use DB status - Balances performance and accuracy Verification: cargo check --lib passes Impact: - detail endpoint now returns accurate status - list endpoint can optionally sync status - Both endpoints use same derivation logic G-4 status: ✅ Completed --- src/api/identity_api.rs | 101 ++++++++++++++++++++++++++++++++++------ 1 file changed, 87 insertions(+), 14 deletions(-) diff --git a/src/api/identity_api.rs b/src/api/identity_api.rs index 121a6126..8219e09e 100644 --- a/src/api/identity_api.rs +++ b/src/api/identity_api.rs @@ -82,6 +82,7 @@ pub struct FilesQuery { pub page_size: Option, pub status: Option, pub file_uuid: Option, + pub sync_status: Option, } async fn list_files( @@ -142,6 +143,8 @@ async fn list_files( .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?; let total = records.1; + + let sync = params.sync_status.unwrap_or(false); let mut data: Vec = records .0 @@ -155,6 +158,15 @@ async fn list_files( total_chunks: 0, }) .collect(); + + // If sync_status=true, compute status for each file + if sync { + for item in &mut data { + if let Ok(computed) = compute_file_status(&state, &item.file_uuid).await { + item.status = computed; + } + } + } // Fetch chunk counts for all files in one query let uuids: Vec = data.iter().map(|f| f.file_uuid.clone()).collect(); @@ -223,6 +235,60 @@ pub struct FileDetailResponse { pub created_at: Option>, } +async fn compute_file_status( + state: &crate::api::types::AppState, + file_uuid: &str, +) -> Result { + use crate::core::config::OUTPUT_DIR; + + let output_dir = OUTPUT_DIR.as_str(); + let videos_table = crate::core::db::schema::table_name("videos"); + + // Check processing_status->>'phase' + let phase: String = sqlx::query_scalar(&format!( + "SELECT COALESCE(processing_status->>'phase', '') FROM {} WHERE file_uuid = $1", + videos_table + )) + .bind(file_uuid) + .fetch_optional(state.db.pool()) + .await + .ok() + .flatten() + .unwrap_or_default(); + + let phase_completed = phase == "COMPLETED"; + + // Check if any processor JSON exists + let processors = [ + ("asr", "asr.json"), + ("asrx", "asrx.json"), + ("ocr", "ocr.json"), + ("cut", "cut.json"), + ("face", "face.json"), + ("face_cluster", "face_cluster.json"), + ]; + + let mut any_json_exists = false; + for (_name, filename) in &processors { + let path = std::path::Path::new(output_dir).join(format!("{}.{}", file_uuid, filename)); + if path.exists() { + any_json_exists = true; + break; + } + } + + // Derive status + let status = if phase_completed && any_json_exists { + "completed" + } else if any_json_exists { + "scanning" + } else { + "registered" + }; + + Ok(status.to_string()) +} + async fn get_file_detail( State(state): State, Path(file_uuid): Path, @@ -232,21 +298,28 @@ async fn get_file_detail( .get_video_by_uuid(&file_uuid) .await .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?; - + match file { - Some(f) => Ok(Json(FileDetailResponse { - success: true, - file_uuid: f.file_uuid, - file_name: f.file_name, - file_path: f.file_path, - status: f.status.as_str().to_string(), - duration: f.duration, - fps: f.fps, - metadata: f.probe_json, - created_at: chrono::DateTime::parse_from_rfc3339(&f.created_at) - .ok() - .map(|d| d.into()), - })), + Some(f) => { + // Compute status instead of using DB value + let computed_status = compute_file_status(&state, &file_uuid) + .await + .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?; + + Ok(Json(FileDetailResponse { + success: true, + file_uuid: f.file_uuid, + file_name: f.file_name, + file_path: f.file_path, + status: computed_status, + duration: f.duration, + fps: f.fps, + metadata: f.probe_json, + created_at: chrono::DateTime::parse_from_rfc3339(&f.created_at) + .ok() + .map(|d| d.into()), + })) + }, None => Err(( StatusCode::NOT_FOUND, format!("File not found: {}", file_uuid),