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:
+86
-13
@@ -82,6 +82,7 @@ pub struct FilesQuery {
|
|||||||
pub page_size: Option<usize>,
|
pub page_size: Option<usize>,
|
||||||
pub status: Option<String>,
|
pub status: Option<String>,
|
||||||
pub file_uuid: Option<String>,
|
pub file_uuid: Option<String>,
|
||||||
|
pub sync_status: Option<bool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn list_files(
|
async fn list_files(
|
||||||
@@ -143,6 +144,8 @@ async fn list_files(
|
|||||||
|
|
||||||
let total = records.1;
|
let total = records.1;
|
||||||
|
|
||||||
|
let sync = params.sync_status.unwrap_or(false);
|
||||||
|
|
||||||
let mut data: Vec<FileItem> = records
|
let mut data: Vec<FileItem> = records
|
||||||
.0
|
.0
|
||||||
.into_iter()
|
.into_iter()
|
||||||
@@ -156,6 +159,15 @@ async fn list_files(
|
|||||||
})
|
})
|
||||||
.collect();
|
.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
|
// Fetch chunk counts for all files in one query
|
||||||
let uuids: Vec<String> = data.iter().map(|f| f.file_uuid.clone()).collect();
|
let uuids: Vec<String> = data.iter().map(|f| f.file_uuid.clone()).collect();
|
||||||
if !uuids.is_empty() {
|
if !uuids.is_empty() {
|
||||||
@@ -223,6 +235,60 @@ pub struct FileDetailResponse {
|
|||||||
pub created_at: Option<chrono::DateTime<chrono::Utc>>,
|
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(
|
async fn get_file_detail(
|
||||||
State(state): State<crate::api::types::AppState>,
|
State(state): State<crate::api::types::AppState>,
|
||||||
Path(file_uuid): Path<String>,
|
Path(file_uuid): Path<String>,
|
||||||
@@ -234,19 +300,26 @@ async fn get_file_detail(
|
|||||||
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
||||||
|
|
||||||
match file {
|
match file {
|
||||||
Some(f) => Ok(Json(FileDetailResponse {
|
Some(f) => {
|
||||||
success: true,
|
// Compute status instead of using DB value
|
||||||
file_uuid: f.file_uuid,
|
let computed_status = compute_file_status(&state, &file_uuid)
|
||||||
file_name: f.file_name,
|
.await
|
||||||
file_path: f.file_path,
|
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
||||||
status: f.status.as_str().to_string(),
|
|
||||||
duration: f.duration,
|
Ok(Json(FileDetailResponse {
|
||||||
fps: f.fps,
|
success: true,
|
||||||
metadata: f.probe_json,
|
file_uuid: f.file_uuid,
|
||||||
created_at: chrono::DateTime::parse_from_rfc3339(&f.created_at)
|
file_name: f.file_name,
|
||||||
.ok()
|
file_path: f.file_path,
|
||||||
.map(|d| d.into()),
|
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((
|
None => Err((
|
||||||
StatusCode::NOT_FOUND,
|
StatusCode::NOT_FOUND,
|
||||||
format!("File not found: {}", file_uuid),
|
format!("File not found: {}", file_uuid),
|
||||||
|
|||||||
Reference in New Issue
Block a user