feat: Phase 1 handover - schema migration, correction mechanism, API fixes

Schema changes: dev.chunks->dev.chunk, remove old_chunk_id/chunk_index
Correction: asr-1.json format, generate/apply scripts
API: 37/37 endpoints fixed and tested
Docs: HANDOVER_V2.0.md for M4
This commit is contained in:
Accusys
2026-05-11 07:03:22 +08:00
parent ef894a44ad
commit 39ba5ddf76
147 changed files with 19843 additions and 3053 deletions
+1 -1
View File
@@ -6,6 +6,6 @@ pub mod types;
pub use rule1_ingest::execute_rule1;
pub use rule3_ingest::ingest_rule3;
pub use trace_ingest::ingest_traces;
pub use splitter::{AsrSegment, ChunkSplitter};
pub use trace_ingest::ingest_traces;
pub use types::{Chunk, ChunkType};
+1 -1
View File
@@ -50,7 +50,7 @@ pub async fn execute_rule1(db: &PostgresDb, file_uuid: &str, fps: f64) -> Result
let chunk = Chunk::from_seconds(
file_id as i32,
file_uuid.to_string(),
idx as u32,
format!("{}", idx),
ChunkType::Sentence,
ChunkRule::Rule1,
seg.start_time,
+11 -10
View File
@@ -73,7 +73,7 @@ pub async fn ingest_rule3(pool: &PgPool, file_uuid: &str) -> Result<usize> {
// Query chunks table for Rule 1 sentence chunks
let rule1_rows: Vec<(String,)> = sqlx::query_as(
r#"
SELECT chunk_id FROM chunks
SELECT chunk_id FROM dev.chunk
WHERE file_uuid = $1 AND chunk_type = 'sentence'
AND start_frame >= $2
AND end_frame <= $3
@@ -98,7 +98,7 @@ pub async fn ingest_rule3(pool: &PgPool, file_uuid: &str) -> Result<usize> {
let texts: Vec<String> = sqlx::query_scalar(
r#"
SELECT text_content FROM chunks
SELECT text_content FROM dev.chunk
WHERE file_uuid = $1 AND chunk_type = 'sentence'
AND start_frame >= $2
AND end_frame <= $3
@@ -135,10 +135,11 @@ pub async fn ingest_rule3(pool: &PgPool, file_uuid: &str) -> Result<usize> {
);
// 4. Insert into dev.chunks
let fps_query: Option<f64> = sqlx::query_scalar("SELECT fps FROM videos WHERE file_uuid = $1")
.bind(file_uuid)
.fetch_optional(&mut *tx)
.await?;
let fps_query: Option<f64> =
sqlx::query_scalar("SELECT fps FROM videos WHERE file_uuid = $1")
.bind(file_uuid)
.fetch_optional(&mut *tx)
.await?;
let fps = fps_query.unwrap_or(29.97);
// Prepare metadata JSON
@@ -149,12 +150,12 @@ pub async fn ingest_rule3(pool: &PgPool, file_uuid: &str) -> Result<usize> {
sqlx::query(
r#"
INSERT INTO chunks (
file_uuid, chunk_id, old_chunk_id, chunk_index, chunk_type,
INSERT INTO dev.chunk (
file_uuid, chunk_id, chunk_type,
start_time, end_time, fps, start_frame, end_frame,
content, text_content, summary_text, metadata, child_chunk_ids
) VALUES ($1, $2, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)
ON CONFLICT (file_uuid, old_chunk_id) DO NOTHING
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
ON CONFLICT (file_uuid, chunk_id) DO NOTHING
"#,
)
.bind(file_uuid)
+2 -2
View File
@@ -23,7 +23,7 @@ impl ChunkSplitter {
chunks.push(Chunk::from_seconds(
0, // file_id
uuid.to_string(),
index,
format!("{}", index),
ChunkType::TimeBased,
ChunkRule::Rule1,
current_time,
@@ -48,7 +48,7 @@ impl ChunkSplitter {
chunks.push(Chunk::from_seconds(
0, // file_id
uuid.to_string(),
index as u32,
format!("{}", index),
ChunkType::Sentence,
ChunkRule::Rule1,
segment.start,
+24 -15
View File
@@ -95,7 +95,7 @@ pub async fn ingest_traces(db: &PostgresDb, file_uuid: &str) -> Result<usize> {
let chunk = Chunk::new(
file_id,
file_uuid.to_string(),
(count + 1) as u32,
format!("trace_{}", count + 1),
ChunkType::Trace,
ChunkRule::Rule1,
trace.first_frame as i64,
@@ -110,17 +110,29 @@ pub async fn ingest_traces(db: &PostgresDb, file_uuid: &str) -> Result<usize> {
if let Err(e) = db.store_chunk(&chunk).await {
error!("Failed to store trace chunk {}: {}", trace.trace_id, e);
} else {
let preview = chunk.text_content.as_deref().unwrap_or("").chars().take(60).collect::<String>();
let co = chunk.metadata.as_ref()
let preview = chunk
.text_content
.as_deref()
.unwrap_or("")
.chars()
.take(60)
.collect::<String>();
let co = chunk
.metadata
.as_ref()
.and_then(|m| m.get("co_appearances"))
.and_then(|c| c.as_array())
.map(|a| a.len())
.unwrap_or(0);
info!(
"Trace chunk {}: trace_id={} frames={}-{} faces={} co_appear={} text={}",
chunk.chunk_id, trace.trace_id,
trace.first_frame, trace.last_frame,
trace.face_count, co, preview,
chunk.chunk_id,
trace.trace_id,
trace.first_frame,
trace.last_frame,
trace.face_count,
co,
preview,
);
count += 1;
}
@@ -209,14 +221,11 @@ impl<'r> sqlx::FromRow<'r, sqlx::postgres::PgRow> for AsrSegment {
impl AsrSegment {
fn text(&self) -> Option<&str> {
self.data
.get("text")
.and_then(|v| v.as_str())
.or_else(|| {
self.data
.get("data")
.and_then(|d| d.get("text"))
.and_then(|v| v.as_str())
})
self.data.get("text").and_then(|v| v.as_str()).or_else(|| {
self.data
.get("data")
.and_then(|d| d.get("text"))
.and_then(|v| v.as_str())
})
}
}
+7 -10
View File
@@ -115,7 +115,6 @@ pub struct Chunk {
pub file_id: i32,
pub uuid: String,
pub chunk_id: String,
pub chunk_index: u32,
pub chunk_type: ChunkType,
pub rule: ChunkRule,
/// Frames per second (can be fractional, e.g., 29.97, 23.976)
@@ -140,7 +139,7 @@ impl Chunk {
pub fn new(
file_id: i32,
uuid: String,
chunk_index: u32,
chunk_id: String,
chunk_type: ChunkType,
rule: ChunkRule,
start_frame: i64,
@@ -149,13 +148,11 @@ impl Chunk {
content: serde_json::Value,
) -> Self {
let frame_count = (end_frame - start_frame) as i32;
let chunk_id = format!("{}_{}", uuid, chunk_index);
Self {
file_id,
uuid,
chunk_id,
chunk_index,
chunk_type,
rule,
fps,
@@ -177,7 +174,7 @@ impl Chunk {
pub fn new_visual(
file_id: i32,
uuid: String,
chunk_index: u32,
chunk_id: String,
start_frame: i64,
end_frame: i64,
fps: f64,
@@ -189,7 +186,7 @@ impl Chunk {
Self::new(
file_id,
uuid,
chunk_index,
chunk_id,
ChunkType::Visual,
ChunkRule::Rule2,
start_frame,
@@ -203,7 +200,7 @@ impl Chunk {
pub fn from_yolo_frames(
file_id: i32,
uuid: String,
chunk_index: u32,
chunk_id: String,
start_frame: i64,
end_frame: i64,
fps: f64,
@@ -307,7 +304,7 @@ impl Chunk {
Self::new_visual(
file_id,
uuid,
chunk_index,
chunk_id,
start_frame,
end_frame,
fps,
@@ -334,7 +331,7 @@ impl Chunk {
pub fn from_seconds(
file_id: i32,
uuid: String,
chunk_index: u32,
chunk_id: String,
chunk_type: ChunkType,
rule: ChunkRule,
start_time: f64,
@@ -347,7 +344,7 @@ impl Chunk {
Self::new(
file_id,
uuid,
chunk_index,
chunk_id,
chunk_type,
rule,
start_frame,
+5 -8
View File
@@ -103,7 +103,6 @@ pub struct Chunk {
pub file_id: i32,
pub uuid: String,
pub chunk_id: String,
pub chunk_index: u32,
pub chunk_type: ChunkType,
pub rule: ChunkRule,
/// Frames per second (can be fractional, e.g., 29.97, 23.976)
@@ -128,7 +127,7 @@ impl Chunk {
pub fn new_visual(
file_id: i32,
uuid: String,
chunk_index: u32,
chunk_id: String,
start_frame: i64,
end_frame: i64,
fps: f64,
@@ -140,7 +139,7 @@ impl Chunk {
Self::new(
file_id,
uuid,
chunk_index,
chunk_id,
ChunkType::Visual,
ChunkRule::Rule2,
start_frame,
@@ -154,7 +153,7 @@ impl Chunk {
pub fn from_yolo_result(
file_id: i32,
uuid: String,
chunk_index: u32,
chunk_id: String,
start_frame: i64,
end_frame: i64,
fps: f64,
@@ -263,7 +262,7 @@ impl Chunk {
Self::new_visual(
file_id,
uuid,
chunk_index,
chunk_id,
start_frame,
end_frame,
fps,
@@ -275,7 +274,7 @@ impl Chunk {
pub fn new(
file_id: i32,
uuid: String,
chunk_index: u32,
chunk_id: String,
chunk_type: ChunkType,
rule: ChunkRule,
start_frame: i64,
@@ -284,13 +283,11 @@ impl Chunk {
content: serde_json::Value,
) -> Self {
let frame_count = (end_frame - start_frame) as i32;
let chunk_id = format!("{}_{}", uuid, chunk_index);
Self {
file_id,
uuid,
chunk_id,
chunk_index,
chunk_type,
rule,
fps,