feat: 新增 Job Worker 系統與 API 文檔全面更新
This commit is contained in:
@@ -1,11 +1,22 @@
|
||||
use anyhow::{Context, Result};
|
||||
use std::collections::HashMap;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::{mpsc, RwLock};
|
||||
use tracing::{error, info};
|
||||
|
||||
use crate::core::chunk::types::{Chunk, ChunkRule, ChunkType};
|
||||
use crate::core::config::{OUTPUT_DIR, PYTHON_PATH, SCRIPTS_DIR};
|
||||
use crate::core::db::RedisClient;
|
||||
use crate::core::db::{MonitorJob, PostgresDb, ProcessorJobStatus, ProcessorType};
|
||||
use crate::core::processor;
|
||||
use crate::core::processor::asr::AsrResult;
|
||||
use crate::core::processor::asrx::AsrxResult;
|
||||
use crate::core::processor::cut::CutResult;
|
||||
use crate::core::processor::face::FaceResult;
|
||||
use crate::core::processor::ocr::OcrResult;
|
||||
use crate::core::processor::pose::PoseResult;
|
||||
use crate::core::processor::yolo::YoloResult;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ProcessorTask {
|
||||
@@ -104,46 +115,58 @@ impl ProcessorPool {
|
||||
"Processor {} completed for job {}",
|
||||
processor_name, job.uuid
|
||||
);
|
||||
let _ = db
|
||||
if let Err(e) = db
|
||||
.update_processor_result(
|
||||
processor_result_id,
|
||||
ProcessorJobStatus::Completed,
|
||||
None,
|
||||
Some(&output),
|
||||
)
|
||||
.await;
|
||||
.await
|
||||
{
|
||||
error!("Failed to update processor result to completed: {}", e);
|
||||
}
|
||||
|
||||
let _ = redis
|
||||
if let Err(e) = redis
|
||||
.update_worker_processor_status(
|
||||
&job.uuid,
|
||||
&processor_name,
|
||||
"completed",
|
||||
None,
|
||||
)
|
||||
.await;
|
||||
.await
|
||||
{
|
||||
error!("Failed to update Redis processor status: {}", e);
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
error!(
|
||||
"Processor {} failed for job {}: {}",
|
||||
processor_name, job.uuid, e
|
||||
);
|
||||
let _ = db
|
||||
if let Err(db_err) = db
|
||||
.update_processor_result(
|
||||
processor_result_id,
|
||||
ProcessorJobStatus::Failed,
|
||||
Some(&e.to_string()),
|
||||
None,
|
||||
)
|
||||
.await;
|
||||
.await
|
||||
{
|
||||
error!("Failed to update processor result to failed: {}", db_err);
|
||||
}
|
||||
|
||||
let _ = redis
|
||||
if let Err(redis_err) = redis
|
||||
.update_worker_processor_status(
|
||||
&job.uuid,
|
||||
&processor_name,
|
||||
"failed",
|
||||
Some(&e.to_string()),
|
||||
)
|
||||
.await;
|
||||
.await
|
||||
{
|
||||
error!("Failed to update Redis processor status: {}", redis_err);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -153,24 +176,136 @@ impl ProcessorPool {
|
||||
|
||||
async fn run_processor(
|
||||
db: &PostgresDb,
|
||||
redis: &RedisClient,
|
||||
_redis: &RedisClient,
|
||||
job: &MonitorJob,
|
||||
processor_type: ProcessorType,
|
||||
mut cancel_rx: mpsc::Receiver<()>,
|
||||
_cancel_rx: mpsc::Receiver<()>,
|
||||
) -> Result<serde_json::Value> {
|
||||
let video_path = job.video_path.as_ref().context("No video path in job")?;
|
||||
|
||||
// Generate output path
|
||||
let output_dir = PathBuf::from(OUTPUT_DIR.as_str());
|
||||
let output_path = output_dir.join(format!(
|
||||
"job_{}_{}_{}.json",
|
||||
job.id,
|
||||
processor_type.as_str(),
|
||||
chrono::Utc::now().timestamp_millis()
|
||||
));
|
||||
|
||||
// Ensure output directory exists
|
||||
if let Some(parent) = output_path.parent() {
|
||||
tokio::fs::create_dir_all(parent).await?;
|
||||
}
|
||||
|
||||
let uuid = Some(job.uuid.as_str());
|
||||
|
||||
match processor_type {
|
||||
ProcessorType::Asr => Self::run_asr(db, redis, video_path, &mut cancel_rx).await,
|
||||
ProcessorType::Cut => Self::run_cut(db, redis, video_path, &mut cancel_rx).await,
|
||||
ProcessorType::Yolo => Self::run_yolo(db, redis, video_path, &mut cancel_rx).await,
|
||||
ProcessorType::Ocr => Self::run_ocr(db, redis, video_path, &mut cancel_rx).await,
|
||||
ProcessorType::Face => Self::run_face(db, redis, video_path, &mut cancel_rx).await,
|
||||
ProcessorType::Pose => Self::run_pose(db, redis, video_path, &mut cancel_rx).await,
|
||||
ProcessorType::Asrx => Self::run_asrx(db, redis, video_path, &mut cancel_rx).await,
|
||||
ProcessorType::Asr => {
|
||||
let result =
|
||||
processor::process_asr(video_path, output_path.to_str().unwrap(), uuid).await?;
|
||||
// Store ASR chunks in database
|
||||
tracing::info!(
|
||||
"ASR completed, storing {} segments for {}",
|
||||
result.segments.len(),
|
||||
job.uuid
|
||||
);
|
||||
if let Err(e) = Self::store_asr_chunks(db, &job.uuid, &result).await {
|
||||
tracing::error!("Failed to store ASR chunks for {}: {}", job.uuid, e);
|
||||
}
|
||||
Ok(serde_json::to_value(result)?)
|
||||
}
|
||||
ProcessorType::Cut => {
|
||||
let result =
|
||||
processor::process_cut(video_path, output_path.to_str().unwrap(), uuid).await?;
|
||||
// Store CUT chunks in database
|
||||
tracing::info!(
|
||||
"CUT completed, storing {} scenes for {}",
|
||||
result.scenes.len(),
|
||||
job.uuid
|
||||
);
|
||||
if let Err(e) = Self::store_cut_chunks(db, &job.uuid, &result).await {
|
||||
tracing::error!("Failed to store CUT chunks for {}: {}", job.uuid, e);
|
||||
}
|
||||
Ok(serde_json::to_value(result)?)
|
||||
}
|
||||
ProcessorType::Yolo => {
|
||||
let result =
|
||||
processor::process_yolo(video_path, output_path.to_str().unwrap(), uuid)
|
||||
.await?;
|
||||
// Store YOLO chunks in database
|
||||
tracing::info!(
|
||||
"YOLO completed, storing {} frames for {}",
|
||||
result.frames.len(),
|
||||
job.uuid
|
||||
);
|
||||
if let Err(e) = Self::store_yolo_chunks(db, &job.uuid, &result).await {
|
||||
tracing::error!("Failed to store YOLO chunks for {}: {}", job.uuid, e);
|
||||
}
|
||||
Ok(serde_json::to_value(result)?)
|
||||
}
|
||||
ProcessorType::Ocr => {
|
||||
let result =
|
||||
processor::process_ocr(video_path, output_path.to_str().unwrap(), uuid).await?;
|
||||
// Store OCR chunks in database
|
||||
tracing::info!(
|
||||
"OCR completed, storing {} frames for {}",
|
||||
result.frames.len(),
|
||||
job.uuid
|
||||
);
|
||||
if let Err(e) = Self::store_ocr_chunks(db, &job.uuid, &result).await {
|
||||
tracing::error!("Failed to store OCR chunks for {}: {}", job.uuid, e);
|
||||
}
|
||||
Ok(serde_json::to_value(result)?)
|
||||
}
|
||||
ProcessorType::Face => {
|
||||
let result =
|
||||
processor::process_face(video_path, output_path.to_str().unwrap(), uuid)
|
||||
.await?;
|
||||
// Store FACE chunks in database
|
||||
tracing::info!(
|
||||
"FACE completed, storing {} frames for {}",
|
||||
result.frames.len(),
|
||||
job.uuid
|
||||
);
|
||||
if let Err(e) = Self::store_face_chunks(db, &job.uuid, &result).await {
|
||||
tracing::error!("Failed to store FACE chunks for {}: {}", job.uuid, e);
|
||||
}
|
||||
Ok(serde_json::to_value(result)?)
|
||||
}
|
||||
ProcessorType::Pose => {
|
||||
let result =
|
||||
processor::process_pose(video_path, output_path.to_str().unwrap(), uuid)
|
||||
.await?;
|
||||
// Store POSE chunks in database
|
||||
tracing::info!(
|
||||
"POSE completed, storing {} frames for {}",
|
||||
result.frames.len(),
|
||||
job.uuid
|
||||
);
|
||||
if let Err(e) = Self::store_pose_chunks(db, &job.uuid, &result).await {
|
||||
tracing::error!("Failed to store POSE chunks for {}: {}", job.uuid, e);
|
||||
}
|
||||
Ok(serde_json::to_value(result)?)
|
||||
}
|
||||
ProcessorType::Asrx => {
|
||||
let result =
|
||||
processor::process_asrx(video_path, output_path.to_str().unwrap(), uuid)
|
||||
.await?;
|
||||
// Store ASRX chunks in database
|
||||
tracing::info!(
|
||||
"ASRX completed, storing {} segments for {}",
|
||||
result.segments.len(),
|
||||
job.uuid
|
||||
);
|
||||
if let Err(e) = Self::store_asrx_chunks(db, &job.uuid, &result).await {
|
||||
tracing::error!("Failed to store ASRX chunks for {}: {}", job.uuid, e);
|
||||
}
|
||||
Ok(serde_json::to_value(result)?)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
async fn run_asr(
|
||||
_db: &PostgresDb,
|
||||
_redis: &RedisClient,
|
||||
@@ -178,9 +313,9 @@ impl ProcessorPool {
|
||||
_cancel_rx: &mut mpsc::Receiver<()>,
|
||||
) -> Result<serde_json::Value> {
|
||||
let script_path = std::env::var("MOMENTRY_ASR_SCRIPT")
|
||||
.unwrap_or_else(|_| "/Users/accusys/momentry/scripts/asr.py".to_string());
|
||||
.unwrap_or_else(|_| format!("{}/asr_processor.py", SCRIPTS_DIR.as_str()));
|
||||
|
||||
let output = tokio::process::Command::new("/opt/homebrew/bin/python3.11")
|
||||
let output = tokio::process::Command::new(PYTHON_PATH.as_str())
|
||||
.arg(&script_path)
|
||||
.arg(video_path)
|
||||
.output()
|
||||
@@ -195,6 +330,7 @@ impl ProcessorPool {
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
async fn run_cut(
|
||||
_db: &PostgresDb,
|
||||
_redis: &RedisClient,
|
||||
@@ -202,9 +338,9 @@ impl ProcessorPool {
|
||||
_cancel_rx: &mut mpsc::Receiver<()>,
|
||||
) -> Result<serde_json::Value> {
|
||||
let script_path = std::env::var("MOMENTRY_CUT_SCRIPT")
|
||||
.unwrap_or_else(|_| "/Users/accusys/momentry/scripts/cut.py".to_string());
|
||||
.unwrap_or_else(|_| format!("{}/cut_processor.py", SCRIPTS_DIR.as_str()));
|
||||
|
||||
let output = tokio::process::Command::new("/opt/homebrew/bin/python3.11")
|
||||
let output = tokio::process::Command::new(PYTHON_PATH.as_str())
|
||||
.arg(&script_path)
|
||||
.arg(video_path)
|
||||
.output()
|
||||
@@ -219,6 +355,7 @@ impl ProcessorPool {
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
async fn run_yolo(
|
||||
_db: &PostgresDb,
|
||||
_redis: &RedisClient,
|
||||
@@ -226,9 +363,9 @@ impl ProcessorPool {
|
||||
_cancel_rx: &mut mpsc::Receiver<()>,
|
||||
) -> Result<serde_json::Value> {
|
||||
let script_path = std::env::var("MOMENTRY_YOLO_SCRIPT")
|
||||
.unwrap_or_else(|_| "/Users/accusys/momentry/scripts/yolo_processor.py".to_string());
|
||||
.unwrap_or_else(|_| format!("{}/yolo_processor.py", SCRIPTS_DIR.as_str()));
|
||||
|
||||
let output = tokio::process::Command::new("/opt/homebrew/bin/python3.11")
|
||||
let output = tokio::process::Command::new(PYTHON_PATH.as_str())
|
||||
.arg(&script_path)
|
||||
.arg(video_path)
|
||||
.output()
|
||||
@@ -243,6 +380,7 @@ impl ProcessorPool {
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
async fn run_ocr(
|
||||
_db: &PostgresDb,
|
||||
_redis: &RedisClient,
|
||||
@@ -250,9 +388,9 @@ impl ProcessorPool {
|
||||
_cancel_rx: &mut mpsc::Receiver<()>,
|
||||
) -> Result<serde_json::Value> {
|
||||
let script_path = std::env::var("MOMENTRY_OCR_SCRIPT")
|
||||
.unwrap_or_else(|_| "/Users/accusys/momentry/scripts/ocr.py".to_string());
|
||||
.unwrap_or_else(|_| format!("{}/ocr_processor.py", SCRIPTS_DIR.as_str()));
|
||||
|
||||
let output = tokio::process::Command::new("/opt/homebrew/bin/python3.11")
|
||||
let output = tokio::process::Command::new(PYTHON_PATH.as_str())
|
||||
.arg(&script_path)
|
||||
.arg(video_path)
|
||||
.output()
|
||||
@@ -267,6 +405,7 @@ impl ProcessorPool {
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
async fn run_face(
|
||||
_db: &PostgresDb,
|
||||
_redis: &RedisClient,
|
||||
@@ -274,9 +413,9 @@ impl ProcessorPool {
|
||||
_cancel_rx: &mut mpsc::Receiver<()>,
|
||||
) -> Result<serde_json::Value> {
|
||||
let script_path = std::env::var("MOMENTRY_FACE_SCRIPT")
|
||||
.unwrap_or_else(|_| "/Users/accusys/momentry/scripts/face.py".to_string());
|
||||
.unwrap_or_else(|_| format!("{}/face_processor.py", SCRIPTS_DIR.as_str()));
|
||||
|
||||
let output = tokio::process::Command::new("/opt/homebrew/bin/python3.11")
|
||||
let output = tokio::process::Command::new(PYTHON_PATH.as_str())
|
||||
.arg(&script_path)
|
||||
.arg(video_path)
|
||||
.output()
|
||||
@@ -291,6 +430,7 @@ impl ProcessorPool {
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
async fn run_pose(
|
||||
_db: &PostgresDb,
|
||||
_redis: &RedisClient,
|
||||
@@ -298,9 +438,9 @@ impl ProcessorPool {
|
||||
_cancel_rx: &mut mpsc::Receiver<()>,
|
||||
) -> Result<serde_json::Value> {
|
||||
let script_path = std::env::var("MOMENTRY_POSE_SCRIPT")
|
||||
.unwrap_or_else(|_| "/Users/accusys/momentry/scripts/pose.py".to_string());
|
||||
.unwrap_or_else(|_| format!("{}/pose_processor.py", SCRIPTS_DIR.as_str()));
|
||||
|
||||
let output = tokio::process::Command::new("/opt/homebrew/bin/python3.11")
|
||||
let output = tokio::process::Command::new(PYTHON_PATH.as_str())
|
||||
.arg(&script_path)
|
||||
.arg(video_path)
|
||||
.output()
|
||||
@@ -315,6 +455,7 @@ impl ProcessorPool {
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
async fn run_asrx(
|
||||
_db: &PostgresDb,
|
||||
_redis: &RedisClient,
|
||||
@@ -322,9 +463,9 @@ impl ProcessorPool {
|
||||
_cancel_rx: &mut mpsc::Receiver<()>,
|
||||
) -> Result<serde_json::Value> {
|
||||
let script_path = std::env::var("MOMENTRY_ASRX_SCRIPT")
|
||||
.unwrap_or_else(|_| "/Users/accusys/momentry/scripts/asrx.py".to_string());
|
||||
.unwrap_or_else(|_| format!("{}/asrx_processor.py", SCRIPTS_DIR.as_str()));
|
||||
|
||||
let output = tokio::process::Command::new("/opt/homebrew/bin/python3.11")
|
||||
let output = tokio::process::Command::new(PYTHON_PATH.as_str())
|
||||
.arg(&script_path)
|
||||
.arg(video_path)
|
||||
.output()
|
||||
@@ -339,6 +480,377 @@ impl ProcessorPool {
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub async fn store_asr_chunks(
|
||||
db: &PostgresDb,
|
||||
uuid: &str,
|
||||
asr_result: &AsrResult,
|
||||
) -> Result<()> {
|
||||
// Get video record to obtain file_id and fps
|
||||
let video = match db.get_video_by_uuid(uuid).await {
|
||||
Ok(Some(video)) => video,
|
||||
Ok(None) => {
|
||||
tracing::error!("Video not found for uuid: {}", uuid);
|
||||
return Ok(());
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to get video for uuid {}: {}", uuid, e);
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
let file_id = video.id;
|
||||
let fps = if video.fps > 0.0 { video.fps } else { 30.0 };
|
||||
|
||||
for (i, segment) in asr_result.segments.iter().enumerate() {
|
||||
let chunk = Chunk::from_seconds(
|
||||
file_id as i32,
|
||||
uuid.to_string(),
|
||||
i as u32,
|
||||
ChunkType::Sentence,
|
||||
ChunkRule::Rule1,
|
||||
segment.start,
|
||||
segment.end,
|
||||
fps,
|
||||
serde_json::json!({
|
||||
"text": segment.text,
|
||||
"text_normalized": segment.text.to_lowercase(),
|
||||
}),
|
||||
)
|
||||
.with_metadata(serde_json::json!({
|
||||
"language": asr_result.language,
|
||||
"language_probability": asr_result.language_probability,
|
||||
}));
|
||||
|
||||
match db.store_chunk(&chunk).await {
|
||||
Ok(_) => {
|
||||
tracing::info!("Stored ASR chunk {} for video {}", i, uuid);
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to store ASR chunk {}: {}", i, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn store_cut_chunks(
|
||||
db: &PostgresDb,
|
||||
uuid: &str,
|
||||
cut_result: &CutResult,
|
||||
) -> Result<()> {
|
||||
// Get video record to obtain file_id and fps
|
||||
let video = match db.get_video_by_uuid(uuid).await {
|
||||
Ok(Some(video)) => video,
|
||||
Ok(None) => {
|
||||
tracing::error!("Video not found for uuid: {}", uuid);
|
||||
return Ok(());
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to get video for uuid {}: {}", uuid, e);
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
let file_id = video.id;
|
||||
let fps = if video.fps > 0.0 { video.fps } else { 30.0 };
|
||||
|
||||
for (i, scene) in cut_result.scenes.iter().enumerate() {
|
||||
let chunk = Chunk::from_seconds(
|
||||
file_id as i32,
|
||||
uuid.to_string(),
|
||||
i as u32,
|
||||
ChunkType::Cut,
|
||||
ChunkRule::Rule1,
|
||||
scene.start_time,
|
||||
scene.end_time,
|
||||
fps,
|
||||
serde_json::json!({
|
||||
"scene_number": scene.scene_number,
|
||||
"start_frame": scene.start_frame,
|
||||
"end_frame": scene.end_frame,
|
||||
}),
|
||||
);
|
||||
|
||||
match db.store_chunk(&chunk).await {
|
||||
Ok(_) => {
|
||||
tracing::info!("Stored CUT chunk {} for video {}", i, uuid);
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to store CUT chunk {}: {}", i, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn store_yolo_chunks(
|
||||
db: &PostgresDb,
|
||||
uuid: &str,
|
||||
yolo_result: &YoloResult,
|
||||
) -> Result<()> {
|
||||
// Get video record to obtain file_id and fps
|
||||
let video = match db.get_video_by_uuid(uuid).await {
|
||||
Ok(Some(video)) => video,
|
||||
Ok(None) => {
|
||||
tracing::error!("Video not found for uuid: {}", uuid);
|
||||
return Ok(());
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to get video for uuid {}: {}", uuid, e);
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
let file_id = video.id;
|
||||
let fps = if video.fps > 0.0 { video.fps } else { 30.0 };
|
||||
|
||||
for (i, frame) in yolo_result.frames.iter().enumerate() {
|
||||
let mut chunk = Chunk::new(
|
||||
file_id as i32,
|
||||
uuid.to_string(),
|
||||
i as u32,
|
||||
ChunkType::Trace,
|
||||
ChunkRule::Rule1,
|
||||
frame.frame as i64,
|
||||
frame.frame as i64 + 1,
|
||||
fps,
|
||||
serde_json::json!({
|
||||
"objects": frame.objects,
|
||||
"timestamp": frame.timestamp,
|
||||
}),
|
||||
);
|
||||
// Override chunk_id to include processor prefix for uniqueness
|
||||
chunk.chunk_id = format!("trace_yolo_{:04}", i);
|
||||
|
||||
match db.store_chunk(&chunk).await {
|
||||
Ok(_) => {
|
||||
tracing::info!(
|
||||
"Stored YOLO chunk {} (frame {}) for video {}",
|
||||
i,
|
||||
frame.frame,
|
||||
uuid
|
||||
);
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to store YOLO chunk {}: {}", i, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn store_ocr_chunks(
|
||||
db: &PostgresDb,
|
||||
uuid: &str,
|
||||
ocr_result: &OcrResult,
|
||||
) -> Result<()> {
|
||||
// Get video record to obtain file_id and fps
|
||||
let video = match db.get_video_by_uuid(uuid).await {
|
||||
Ok(Some(video)) => video,
|
||||
Ok(None) => {
|
||||
tracing::error!("Video not found for uuid: {}", uuid);
|
||||
return Ok(());
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to get video for uuid {}: {}", uuid, e);
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
let file_id = video.id;
|
||||
let fps = if video.fps > 0.0 { video.fps } else { 30.0 };
|
||||
|
||||
for (i, frame) in ocr_result.frames.iter().enumerate() {
|
||||
let mut chunk = Chunk::new(
|
||||
file_id as i32,
|
||||
uuid.to_string(),
|
||||
i as u32,
|
||||
ChunkType::Trace,
|
||||
ChunkRule::Rule1,
|
||||
frame.frame as i64,
|
||||
frame.frame as i64 + 1,
|
||||
fps,
|
||||
serde_json::json!({
|
||||
"texts": frame.texts,
|
||||
"timestamp": frame.timestamp,
|
||||
}),
|
||||
);
|
||||
// Override chunk_id to include processor prefix for uniqueness
|
||||
chunk.chunk_id = format!("trace_ocr_{:04}", i);
|
||||
|
||||
match db.store_chunk(&chunk).await {
|
||||
Ok(_) => {
|
||||
tracing::info!(
|
||||
"Stored OCR chunk {} (frame {}) for video {}",
|
||||
i,
|
||||
frame.frame,
|
||||
uuid
|
||||
);
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to store OCR chunk {}: {}", i, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn store_face_chunks(
|
||||
db: &PostgresDb,
|
||||
uuid: &str,
|
||||
face_result: &FaceResult,
|
||||
) -> Result<()> {
|
||||
// Get video record to obtain file_id and fps
|
||||
let video = match db.get_video_by_uuid(uuid).await {
|
||||
Ok(Some(video)) => video,
|
||||
Ok(None) => {
|
||||
tracing::error!("Video not found for uuid: {}", uuid);
|
||||
return Ok(());
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to get video for uuid {}: {}", uuid, e);
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
let file_id = video.id;
|
||||
let fps = if video.fps > 0.0 { video.fps } else { 30.0 };
|
||||
|
||||
for (i, frame) in face_result.frames.iter().enumerate() {
|
||||
let mut chunk = Chunk::new(
|
||||
file_id as i32,
|
||||
uuid.to_string(),
|
||||
i as u32,
|
||||
ChunkType::Trace,
|
||||
ChunkRule::Rule1,
|
||||
frame.frame as i64,
|
||||
frame.frame as i64 + 1,
|
||||
fps,
|
||||
serde_json::json!({
|
||||
"faces": frame.faces,
|
||||
"timestamp": frame.timestamp,
|
||||
}),
|
||||
);
|
||||
// Override chunk_id to include processor prefix for uniqueness
|
||||
chunk.chunk_id = format!("trace_face_{:04}", i);
|
||||
|
||||
match db.store_chunk(&chunk).await {
|
||||
Ok(_) => {
|
||||
tracing::info!(
|
||||
"Stored FACE chunk {} (frame {}) for video {}",
|
||||
i,
|
||||
frame.frame,
|
||||
uuid
|
||||
);
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to store FACE chunk {}: {}", i, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn store_pose_chunks(
|
||||
db: &PostgresDb,
|
||||
uuid: &str,
|
||||
pose_result: &PoseResult,
|
||||
) -> Result<()> {
|
||||
// Get video record to obtain file_id and fps
|
||||
let video = match db.get_video_by_uuid(uuid).await {
|
||||
Ok(Some(video)) => video,
|
||||
Ok(None) => {
|
||||
tracing::error!("Video not found for uuid: {}", uuid);
|
||||
return Ok(());
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to get video for uuid {}: {}", uuid, e);
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
let file_id = video.id;
|
||||
let fps = if video.fps > 0.0 { video.fps } else { 30.0 };
|
||||
|
||||
for (i, frame) in pose_result.frames.iter().enumerate() {
|
||||
let mut chunk = Chunk::new(
|
||||
file_id as i32,
|
||||
uuid.to_string(),
|
||||
i as u32,
|
||||
ChunkType::Trace,
|
||||
ChunkRule::Rule1,
|
||||
frame.frame as i64,
|
||||
frame.frame as i64 + 1,
|
||||
fps,
|
||||
serde_json::json!({
|
||||
"persons": frame.persons,
|
||||
"timestamp": frame.timestamp,
|
||||
}),
|
||||
);
|
||||
// Override chunk_id to include processor prefix for uniqueness
|
||||
chunk.chunk_id = format!("trace_pose_{:04}", i);
|
||||
|
||||
match db.store_chunk(&chunk).await {
|
||||
Ok(_) => {
|
||||
tracing::info!(
|
||||
"Stored POSE chunk {} (frame {}) for video {}",
|
||||
i,
|
||||
frame.frame,
|
||||
uuid
|
||||
);
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to store POSE chunk {}: {}", i, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn store_asrx_chunks(
|
||||
db: &PostgresDb,
|
||||
uuid: &str,
|
||||
asrx_result: &AsrxResult,
|
||||
) -> Result<()> {
|
||||
// Get video record to obtain file_id and fps
|
||||
let video = match db.get_video_by_uuid(uuid).await {
|
||||
Ok(Some(video)) => video,
|
||||
Ok(None) => {
|
||||
tracing::error!("Video not found for uuid: {}", uuid);
|
||||
return Ok(());
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to get video for uuid {}: {}", uuid, e);
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
let file_id = video.id;
|
||||
let fps = if video.fps > 0.0 { video.fps } else { 30.0 };
|
||||
|
||||
for (i, segment) in asrx_result.segments.iter().enumerate() {
|
||||
let mut chunk = Chunk::from_seconds(
|
||||
file_id as i32,
|
||||
uuid.to_string(),
|
||||
i as u32,
|
||||
ChunkType::Trace,
|
||||
ChunkRule::Rule1,
|
||||
segment.start,
|
||||
segment.end,
|
||||
fps,
|
||||
serde_json::json!({
|
||||
"text": segment.text,
|
||||
"timestamp": segment.start,
|
||||
}),
|
||||
);
|
||||
// Override chunk_id to include processor prefix for uniqueness
|
||||
chunk.chunk_id = format!("trace_asrx_{:04}", i);
|
||||
|
||||
match db.store_chunk(&chunk).await {
|
||||
Ok(_) => {
|
||||
tracing::info!("Stored ASRX chunk {} for video {}", i, uuid);
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to store ASRX chunk {}: {}", i, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn get_running_count(&self) -> usize {
|
||||
*self.running_count.read().await
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user