refactor: cargo fmt across 27 files + behavioral fixes
Behavioral changes: - postgres_db: reorder default processors (cut first), prevent overwriting completed status - qdrant_db: fix scroll pagination exit condition (next.is_none()) - job_worker: add idempotency guards for face trace / TKG build; better error logging - main.rs: add LineWriter for stdout buffering Remaining diff is cargo fmt reformatting (line wrapping, import ordering).
This commit is contained in:
+65
-29
@@ -23,7 +23,11 @@ impl TkgLogger {
|
||||
.bind(operation)
|
||||
.fetch_one(pool)
|
||||
.await?;
|
||||
tracing::info!("[TKG-Log] Started operation {} for {}", operation, file_uuid);
|
||||
tracing::info!(
|
||||
"[TKG-Log] Started operation {} for {}",
|
||||
operation,
|
||||
file_uuid
|
||||
);
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
@@ -55,7 +59,11 @@ impl TkgLogger {
|
||||
pub async fn complete_operation(&self, error: Option<&str>) -> Result<()> {
|
||||
if let Some(log_id) = self.log_id {
|
||||
let table = schema::table_name("tkg_operation_log");
|
||||
let status = if error.is_some() { "failed" } else { "completed" };
|
||||
let status = if error.is_some() {
|
||||
"failed"
|
||||
} else {
|
||||
"completed"
|
||||
};
|
||||
sqlx::query(&format!(
|
||||
"UPDATE {table} \
|
||||
SET status = $1, \
|
||||
@@ -68,14 +76,41 @@ impl TkgLogger {
|
||||
.bind(log_id)
|
||||
.execute(&self.pool)
|
||||
.await?;
|
||||
tracing::info!("[TKG-Log] Operation {} completed with status: {}", log_id, status);
|
||||
tracing::info!(
|
||||
"[TKG-Log] Operation {} completed with status: {}",
|
||||
log_id,
|
||||
status
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn get_operations(pool: &PgPool, file_uuid: &str) -> Result<Vec<crate::core::tkg::models::TkgOperationLog>> {
|
||||
pub async fn get_operations(
|
||||
pool: &PgPool,
|
||||
file_uuid: &str,
|
||||
) -> Result<Vec<crate::core::tkg::models::TkgOperationLog>> {
|
||||
let table = schema::table_name("tkg_operation_log");
|
||||
let rows = sqlx::query_as::<_, (i64, String, String, Option<String>, Option<String>, i32, i32, i32, i32, i32, i32, String, Option<String>, String, Option<String>, Option<serde_json::Value>)>(&format!(
|
||||
let rows = sqlx::query_as::<
|
||||
_,
|
||||
(
|
||||
i64,
|
||||
String,
|
||||
String,
|
||||
Option<String>,
|
||||
Option<String>,
|
||||
i32,
|
||||
i32,
|
||||
i32,
|
||||
i32,
|
||||
i32,
|
||||
i32,
|
||||
String,
|
||||
Option<String>,
|
||||
String,
|
||||
Option<String>,
|
||||
Option<serde_json::Value>,
|
||||
),
|
||||
>(&format!(
|
||||
"SELECT id, file_uuid, operation, node_type, edge_type, \
|
||||
nodes_created, nodes_updated, nodes_deleted, \
|
||||
edges_created, edges_updated, edges_deleted, \
|
||||
@@ -86,34 +121,35 @@ impl TkgLogger {
|
||||
.fetch_all(pool)
|
||||
.await?;
|
||||
|
||||
Ok(rows.into_iter().map(|r| crate::core::tkg::models::TkgOperationLog {
|
||||
id: r.0,
|
||||
file_uuid: r.1,
|
||||
operation: r.2,
|
||||
node_type: r.3,
|
||||
edge_type: r.4,
|
||||
nodes_created: r.5,
|
||||
nodes_updated: r.6,
|
||||
nodes_deleted: r.7,
|
||||
edges_created: r.8,
|
||||
edges_updated: r.9,
|
||||
edges_deleted: r.10,
|
||||
status: r.11,
|
||||
error_message: r.12,
|
||||
started_at: r.13,
|
||||
completed_at: r.14,
|
||||
properties: r.15,
|
||||
}).collect())
|
||||
Ok(rows
|
||||
.into_iter()
|
||||
.map(|r| crate::core::tkg::models::TkgOperationLog {
|
||||
id: r.0,
|
||||
file_uuid: r.1,
|
||||
operation: r.2,
|
||||
node_type: r.3,
|
||||
edge_type: r.4,
|
||||
nodes_created: r.5,
|
||||
nodes_updated: r.6,
|
||||
nodes_deleted: r.7,
|
||||
edges_created: r.8,
|
||||
edges_updated: r.9,
|
||||
edges_deleted: r.10,
|
||||
status: r.11,
|
||||
error_message: r.12,
|
||||
started_at: r.13,
|
||||
completed_at: r.14,
|
||||
properties: r.15,
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
|
||||
pub async fn delete_operations(pool: &PgPool, file_uuid: &str) -> Result<i64> {
|
||||
let table = schema::table_name("tkg_operation_log");
|
||||
let result = sqlx::query(&format!(
|
||||
"DELETE FROM {table} WHERE file_uuid = $1"
|
||||
))
|
||||
.bind(file_uuid)
|
||||
.execute(pool)
|
||||
.await?;
|
||||
let result = sqlx::query(&format!("DELETE FROM {table} WHERE file_uuid = $1"))
|
||||
.bind(file_uuid)
|
||||
.execute(pool)
|
||||
.await?;
|
||||
Ok(result.rows_affected() as i64)
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -1,7 +1,7 @@
|
||||
pub mod service;
|
||||
pub mod log;
|
||||
pub mod models;
|
||||
pub mod service;
|
||||
|
||||
pub use service::TkgService;
|
||||
pub use log::TkgLogger;
|
||||
pub use models::*;
|
||||
pub use service::TkgService;
|
||||
|
||||
+17
-6
@@ -57,15 +57,26 @@ pub struct TkgBuildStats {
|
||||
|
||||
impl TkgBuildStats {
|
||||
pub fn total_nodes(&self) -> i32 {
|
||||
self.face_track_nodes + self.gaze_track_nodes + self.lip_track_nodes
|
||||
+ self.text_region_nodes + self.appearance_trace_nodes + self.accessory_nodes
|
||||
+ self.object_nodes + self.hand_nodes + self.speaker_nodes
|
||||
self.face_track_nodes
|
||||
+ self.gaze_track_nodes
|
||||
+ self.lip_track_nodes
|
||||
+ self.text_region_nodes
|
||||
+ self.appearance_trace_nodes
|
||||
+ self.accessory_nodes
|
||||
+ self.object_nodes
|
||||
+ self.hand_nodes
|
||||
+ self.speaker_nodes
|
||||
}
|
||||
|
||||
pub fn total_edges(&self) -> i32 {
|
||||
self.co_occurrence_edges + self.speaker_face_edges + self.face_face_edges
|
||||
+ self.mutual_gaze_edges + self.lip_sync_edges + self.has_appearance_edges
|
||||
+ self.wears_edges + self.hand_object_edges
|
||||
self.co_occurrence_edges
|
||||
+ self.speaker_face_edges
|
||||
+ self.face_face_edges
|
||||
+ self.mutual_gaze_edges
|
||||
+ self.lip_sync_edges
|
||||
+ self.has_appearance_edges
|
||||
+ self.wears_edges
|
||||
+ self.hand_object_edges
|
||||
}
|
||||
|
||||
pub fn to_json(&self) -> serde_json::Value {
|
||||
|
||||
+43
-23
@@ -1,5 +1,5 @@
|
||||
use crate::core::db::PostgresDb;
|
||||
use crate::core::db::schema;
|
||||
use crate::core::db::PostgresDb;
|
||||
use crate::core::tkg::log::TkgLogger;
|
||||
use crate::core::tkg::models::{TkgBuildStats, TkgOperationLog};
|
||||
use anyhow::Result;
|
||||
@@ -20,8 +20,11 @@ impl TkgService {
|
||||
let mut logger = TkgLogger::new(self.db.pool().clone());
|
||||
logger.log_id = Some(log_id);
|
||||
|
||||
let redis = crate::core::db::RedisClient::new().ok().map(|r| std::sync::Arc::new(r));
|
||||
let result = crate::core::processor::tkg::build_tkg(&self.db, file_uuid, output_dir, redis).await;
|
||||
let redis = crate::core::db::RedisClient::new()
|
||||
.ok()
|
||||
.map(|r| std::sync::Arc::new(r));
|
||||
let result =
|
||||
crate::core::processor::tkg::build_tkg(&self.db, file_uuid, output_dir, redis).await;
|
||||
|
||||
match result {
|
||||
Ok(r) => {
|
||||
@@ -45,8 +48,12 @@ impl TkgService {
|
||||
hand_object_edges: r.hand_object_edges as i32,
|
||||
};
|
||||
logger.complete_operation(None).await?;
|
||||
tracing::info!("[TKG-Service] Build completed for {}: {} nodes, {} edges",
|
||||
file_uuid, stats.total_nodes(), stats.total_edges());
|
||||
tracing::info!(
|
||||
"[TKG-Service] Build completed for {}: {} nodes, {} edges",
|
||||
file_uuid,
|
||||
stats.total_nodes(),
|
||||
stats.total_edges()
|
||||
);
|
||||
Ok(stats)
|
||||
}
|
||||
Err(e) => {
|
||||
@@ -57,7 +64,12 @@ impl TkgService {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn rebuild(&self, file_uuid: &str, output_dir: &str, force: bool) -> Result<TkgBuildStats> {
|
||||
pub async fn rebuild(
|
||||
&self,
|
||||
file_uuid: &str,
|
||||
output_dir: &str,
|
||||
force: bool,
|
||||
) -> Result<TkgBuildStats> {
|
||||
let operation = if force { "rebuild_force" } else { "rebuild" };
|
||||
let log_id = TkgLogger::start_operation(self.db.pool(), file_uuid, operation).await?;
|
||||
|
||||
@@ -70,7 +82,11 @@ impl TkgService {
|
||||
// Update the original log entry
|
||||
if let Some(id) = Some(log_id) {
|
||||
let table = schema::table_name("tkg_operation_log");
|
||||
let status = if result.is_ok() { "completed" } else { "failed" };
|
||||
let status = if result.is_ok() {
|
||||
"completed"
|
||||
} else {
|
||||
"failed"
|
||||
};
|
||||
let error = result.as_ref().err().map(|e| e.to_string());
|
||||
sqlx::query(&format!(
|
||||
"UPDATE {table} SET status = $1, error_message = $2, completed_at = NOW() WHERE id = $3"
|
||||
@@ -92,11 +108,15 @@ impl TkgService {
|
||||
|
||||
match result {
|
||||
Ok(_) => {
|
||||
TkgLogger::new(self.db.pool().clone()).complete_operation(None).await?;
|
||||
TkgLogger::new(self.db.pool().clone())
|
||||
.complete_operation(None)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
Err(e) => {
|
||||
TkgLogger::new(self.db.pool().clone()).complete_operation(Some(&e.to_string())).await?;
|
||||
TkgLogger::new(self.db.pool().clone())
|
||||
.complete_operation(Some(&e.to_string()))
|
||||
.await?;
|
||||
Err(e)
|
||||
}
|
||||
}
|
||||
@@ -107,23 +127,23 @@ impl TkgService {
|
||||
let edges_table = schema::table_name("tkg_edges");
|
||||
|
||||
// Delete edges first (foreign key constraint)
|
||||
let edges_deleted = sqlx::query(&format!(
|
||||
"DELETE FROM {edges_table} WHERE file_uuid = $1"
|
||||
))
|
||||
.bind(file_uuid)
|
||||
.execute(self.db.pool())
|
||||
.await?;
|
||||
let edges_deleted = sqlx::query(&format!("DELETE FROM {edges_table} WHERE file_uuid = $1"))
|
||||
.bind(file_uuid)
|
||||
.execute(self.db.pool())
|
||||
.await?;
|
||||
|
||||
// Delete nodes
|
||||
let nodes_deleted = sqlx::query(&format!(
|
||||
"DELETE FROM {nodes_table} WHERE file_uuid = $1"
|
||||
))
|
||||
.bind(file_uuid)
|
||||
.execute(self.db.pool())
|
||||
.await?;
|
||||
let nodes_deleted = sqlx::query(&format!("DELETE FROM {nodes_table} WHERE file_uuid = $1"))
|
||||
.bind(file_uuid)
|
||||
.execute(self.db.pool())
|
||||
.await?;
|
||||
|
||||
tracing::info!("[TKG-Service] Deleted {} nodes and {} edges for {}",
|
||||
nodes_deleted.rows_affected(), edges_deleted.rows_affected(), file_uuid);
|
||||
tracing::info!(
|
||||
"[TKG-Service] Deleted {} nodes and {} edges for {}",
|
||||
nodes_deleted.rows_affected(),
|
||||
edges_deleted.rows_affected(),
|
||||
file_uuid
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user