fix: G-4 (A-13) status consistency issue
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
This commit is contained in:
+87
-14
@@ -82,6 +82,7 @@ pub struct FilesQuery {
|
||||
pub page_size: Option<usize>,
|
||||
pub status: Option<String>,
|
||||
pub file_uuid: Option<String>,
|
||||
pub sync_status: Option<bool>,
|
||||
}
|
||||
|
||||
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<FileItem> = 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<String> = data.iter().map(|f| f.file_uuid.clone()).collect();
|
||||
@@ -223,6 +235,60 @@ pub struct FileDetailResponse {
|
||||
pub created_at: Option<chrono::DateTime<chrono::Utc>>,
|
||||
}
|
||||
|
||||
async fn compute_file_status(
|
||||
state: &crate::api::types::AppState,
|
||||
file_uuid: &str,
|
||||
) -> Result<String, StatusCode> {
|
||||
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<crate::api::types::AppState>,
|
||||
Path(file_uuid): Path<String>,
|
||||
@@ -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),
|
||||
|
||||
Reference in New Issue
Block a user