feat: add face_operations.rs SQLite module for face pipeline state management
This commit is contained in:
@@ -0,0 +1,287 @@
|
|||||||
|
use anyhow::{Context, Result};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use sqlx::sqlite::SqlitePoolOptions;
|
||||||
|
use sqlx::SqlitePool;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use crate::core::config::OUTPUT_DIR;
|
||||||
|
|
||||||
|
pub fn face_ops_path(file_uuid: &str) -> PathBuf {
|
||||||
|
PathBuf::from(OUTPUT_DIR.as_str())
|
||||||
|
.join(file_uuid)
|
||||||
|
.join("face_operations.sqlite")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct FaceOperationsDb {
|
||||||
|
pool: SqlitePool,
|
||||||
|
file_uuid: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize, Default)]
|
||||||
|
pub struct ProcessingStatus {
|
||||||
|
pub file_uuid: String,
|
||||||
|
pub detection_done: bool,
|
||||||
|
pub traces_done: bool,
|
||||||
|
pub clusters_done: bool,
|
||||||
|
pub vlm_done: bool,
|
||||||
|
pub last_updated: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
pub struct FaceGroup {
|
||||||
|
pub id: Option<i64>,
|
||||||
|
pub file_uuid: String,
|
||||||
|
pub group_name: String,
|
||||||
|
pub trace_ids: String,
|
||||||
|
pub total_faces: i64,
|
||||||
|
pub created_at: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
pub struct TraceCustom {
|
||||||
|
pub id: Option<i64>,
|
||||||
|
pub file_uuid: String,
|
||||||
|
pub trace_id: i64,
|
||||||
|
pub label: Option<String>,
|
||||||
|
pub group_name: Option<String>,
|
||||||
|
pub notes: Option<String>,
|
||||||
|
pub custom_properties: Option<String>,
|
||||||
|
pub updated_at: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
pub struct GroupOperation {
|
||||||
|
pub id: Option<i64>,
|
||||||
|
pub file_uuid: String,
|
||||||
|
pub operation: String,
|
||||||
|
pub before_json: Option<String>,
|
||||||
|
pub after_json: Option<String>,
|
||||||
|
pub created_at: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FaceOperationsDb {
|
||||||
|
pub async fn open(file_uuid: &str) -> Result<Self> {
|
||||||
|
let path = face_ops_path(file_uuid);
|
||||||
|
|
||||||
|
if let Some(parent) = path.parent() {
|
||||||
|
tokio::fs::create_dir_all(parent).await
|
||||||
|
.with_context(|| format!("Failed to create directory: {}", parent.display()))?;
|
||||||
|
}
|
||||||
|
|
||||||
|
let url = format!("sqlite://{}?mode=rwc", path.display());
|
||||||
|
let pool = SqlitePoolOptions::new()
|
||||||
|
.max_connections(1)
|
||||||
|
.acquire_timeout(Duration::from_secs(5))
|
||||||
|
.connect(&url)
|
||||||
|
.await
|
||||||
|
.with_context(|| format!("Failed to open SQLite: {}", path.display()))?;
|
||||||
|
|
||||||
|
let db = Self {
|
||||||
|
pool,
|
||||||
|
file_uuid: file_uuid.to_string(),
|
||||||
|
};
|
||||||
|
|
||||||
|
db.init_tables().await?;
|
||||||
|
Ok(db)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn init_tables(&self) -> Result<()> {
|
||||||
|
sqlx::query("
|
||||||
|
CREATE TABLE IF NOT EXISTS processing_status (
|
||||||
|
file_uuid TEXT PRIMARY KEY,
|
||||||
|
detection_done BOOLEAN DEFAULT FALSE,
|
||||||
|
traces_done BOOLEAN DEFAULT FALSE,
|
||||||
|
clusters_done BOOLEAN DEFAULT FALSE,
|
||||||
|
vlm_done BOOLEAN DEFAULT FALSE,
|
||||||
|
last_updated DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||||
|
)
|
||||||
|
").execute(&self.pool).await?;
|
||||||
|
|
||||||
|
sqlx::query("
|
||||||
|
CREATE TABLE IF NOT EXISTS face_groups (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
file_uuid TEXT NOT NULL,
|
||||||
|
group_name TEXT NOT NULL,
|
||||||
|
trace_ids TEXT,
|
||||||
|
total_faces INTEGER DEFAULT 0,
|
||||||
|
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
UNIQUE(file_uuid, group_name)
|
||||||
|
)
|
||||||
|
").execute(&self.pool).await?;
|
||||||
|
|
||||||
|
sqlx::query("
|
||||||
|
CREATE TABLE IF NOT EXISTS trace_custom (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
file_uuid TEXT NOT NULL,
|
||||||
|
trace_id INTEGER NOT NULL,
|
||||||
|
label TEXT,
|
||||||
|
group_name TEXT,
|
||||||
|
notes TEXT,
|
||||||
|
custom_properties TEXT,
|
||||||
|
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
UNIQUE(file_uuid, trace_id)
|
||||||
|
)
|
||||||
|
").execute(&self.pool).await?;
|
||||||
|
|
||||||
|
sqlx::query("
|
||||||
|
CREATE TABLE IF NOT EXISTS group_operations (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
file_uuid TEXT NOT NULL,
|
||||||
|
operation TEXT NOT NULL,
|
||||||
|
before_json TEXT,
|
||||||
|
after_json TEXT,
|
||||||
|
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||||
|
)
|
||||||
|
").execute(&self.pool).await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Processing Status
|
||||||
|
pub async fn get_status(&self) -> Result<ProcessingStatus> {
|
||||||
|
let row = sqlx::query_as::<_, (String, bool, bool, bool, bool, String)>(
|
||||||
|
"SELECT file_uuid, detection_done, traces_done, clusters_done, vlm_done,
|
||||||
|
COALESCE(last_updated, CURRENT_TIMESTAMP)
|
||||||
|
FROM processing_status WHERE file_uuid = ?1"
|
||||||
|
).bind(&self.file_uuid).fetch_optional(&self.pool).await?;
|
||||||
|
|
||||||
|
match row {
|
||||||
|
Some((file_uuid, detection_done, traces_done, clusters_done, vlm_done, last_updated)) => {
|
||||||
|
Ok(ProcessingStatus {
|
||||||
|
file_uuid,
|
||||||
|
detection_done,
|
||||||
|
traces_done,
|
||||||
|
clusters_done,
|
||||||
|
vlm_done,
|
||||||
|
last_updated,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
None => Ok(ProcessingStatus {
|
||||||
|
file_uuid: self.file_uuid.clone(),
|
||||||
|
..Default::default()
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn update_step(&self, step: &str, done: bool) -> Result<()> {
|
||||||
|
let column = match step {
|
||||||
|
"detection" => "detection_done",
|
||||||
|
"traces" => "traces_done",
|
||||||
|
"clusters" => "clusters_done",
|
||||||
|
"vlm" => "vlm_done",
|
||||||
|
_ => return Ok(()),
|
||||||
|
};
|
||||||
|
|
||||||
|
sqlx::query(&format!(
|
||||||
|
"INSERT INTO processing_status (file_uuid, {}) VALUES (?1, ?2)
|
||||||
|
ON CONFLICT(file_uuid) DO UPDATE SET {} = ?2, last_updated = CURRENT_TIMESTAMP",
|
||||||
|
column, column
|
||||||
|
)).bind(&self.file_uuid).bind(done).execute(&self.pool).await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Face Groups
|
||||||
|
pub async fn insert_group(&self, group_name: &str, trace_ids: &[i32], total_faces: i64) -> Result<i64> {
|
||||||
|
let trace_ids_json = serde_json::to_string(trace_ids)?;
|
||||||
|
|
||||||
|
let result = sqlx::query(
|
||||||
|
"INSERT INTO face_groups (file_uuid, group_name, trace_ids, total_faces) VALUES (?1, ?2, ?3, ?4)"
|
||||||
|
).bind(&self.file_uuid).bind(group_name).bind(&trace_ids_json).bind(total_faces)
|
||||||
|
.execute(&self.pool).await?;
|
||||||
|
|
||||||
|
Ok(result.last_insert_rowid())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_groups(&self) -> Result<Vec<FaceGroup>> {
|
||||||
|
let groups = sqlx::query_as::<_, (i64, String, String, String, i64, String)>(
|
||||||
|
"SELECT id, file_uuid, group_name, trace_ids, total_faces, created_at
|
||||||
|
FROM face_groups WHERE file_uuid = ?1"
|
||||||
|
).bind(&self.file_uuid).fetch_all(&self.pool).await?;
|
||||||
|
|
||||||
|
Ok(groups.into_iter().map(|(id, file_uuid, group_name, trace_ids, total_faces, created_at)| {
|
||||||
|
FaceGroup {
|
||||||
|
id: Some(id),
|
||||||
|
file_uuid,
|
||||||
|
group_name,
|
||||||
|
trace_ids,
|
||||||
|
total_faces,
|
||||||
|
created_at,
|
||||||
|
}
|
||||||
|
}).collect())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Trace Custom
|
||||||
|
pub async fn upsert_trace_custom(&self, trace_id: i64, label: Option<&str>, group_name: Option<&str>) -> Result<()> {
|
||||||
|
sqlx::query(
|
||||||
|
"INSERT INTO trace_custom (file_uuid, trace_id, label, group_name) VALUES (?1, ?2, ?3, ?4)
|
||||||
|
ON CONFLICT(file_uuid, trace_id) DO UPDATE SET
|
||||||
|
label = ?3, group_name = ?4, updated_at = CURRENT_TIMESTAMP"
|
||||||
|
).bind(&self.file_uuid).bind(trace_id).bind(label).bind(group_name)
|
||||||
|
.execute(&self.pool).await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_trace_custom(&self, trace_id: i64) -> Result<Option<TraceCustom>> {
|
||||||
|
let row = sqlx::query_as::<_, (i64, String, i64, Option<String>, Option<String>, Option<String>, Option<String>, String)>(
|
||||||
|
"SELECT id, file_uuid, trace_id, label, group_name, notes, custom_properties, updated_at
|
||||||
|
FROM trace_custom WHERE file_uuid = ?1 AND trace_id = ?2"
|
||||||
|
).bind(&self.file_uuid).bind(trace_id).fetch_optional(&self.pool).await?;
|
||||||
|
|
||||||
|
Ok(row.map(|(id, file_uuid, trace_id, label, group_name, notes, custom_properties, updated_at)| {
|
||||||
|
TraceCustom {
|
||||||
|
id: Some(id),
|
||||||
|
file_uuid,
|
||||||
|
trace_id,
|
||||||
|
label,
|
||||||
|
group_name,
|
||||||
|
notes,
|
||||||
|
custom_properties,
|
||||||
|
updated_at,
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Group Operations
|
||||||
|
pub async fn log_operation(&self, operation: &str, before: Option<&str>, after: Option<&str>) -> Result<i64> {
|
||||||
|
let result = sqlx::query(
|
||||||
|
"INSERT INTO group_operations (file_uuid, operation, before_json, after_json) VALUES (?1, ?2, ?3, ?4)"
|
||||||
|
).bind(&self.file_uuid).bind(operation).bind(before).bind(after)
|
||||||
|
.execute(&self.pool).await?;
|
||||||
|
|
||||||
|
Ok(result.last_insert_rowid())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_last_operation(&self) -> Result<Option<GroupOperation>> {
|
||||||
|
let row = sqlx::query_as::<_, (i64, String, String, Option<String>, Option<String>, String)>(
|
||||||
|
"SELECT id, file_uuid, operation, before_json, after_json, created_at
|
||||||
|
FROM group_operations WHERE file_uuid = ?1 ORDER BY id DESC LIMIT 1"
|
||||||
|
).bind(&self.file_uuid).fetch_optional(&self.pool).await?;
|
||||||
|
|
||||||
|
Ok(row.map(|(id, file_uuid, operation, before_json, after_json, created_at)| {
|
||||||
|
GroupOperation {
|
||||||
|
id: Some(id),
|
||||||
|
file_uuid,
|
||||||
|
operation,
|
||||||
|
before_json,
|
||||||
|
after_json,
|
||||||
|
created_at,
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn undo_last_operation(&self) -> Result<Option<GroupOperation>> {
|
||||||
|
let last_op = self.get_last_operation().await?;
|
||||||
|
|
||||||
|
if let Some(ref op) = last_op {
|
||||||
|
if let Some(ref before_json) = op.before_json {
|
||||||
|
let _before: serde_json::Value = serde_json::from_str(before_json)?;
|
||||||
|
tracing::info!("[FaceOps] Undo operation: {} (id={})", op.operation, op.id.unwrap_or(0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(last_op)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -64,3 +64,4 @@ pub use workspace_sqlite::{
|
|||||||
workspace_path, ChunkRow, FaceDetectionBatchItem, FaceDetectionRow, PreChunkRow,
|
workspace_path, ChunkRow, FaceDetectionBatchItem, FaceDetectionRow, PreChunkRow,
|
||||||
ProcessorResultRow, SpeakerDetectionBatchItem, SpeakerDetectionRow, WorkspaceDb,
|
ProcessorResultRow, SpeakerDetectionBatchItem, SpeakerDetectionRow, WorkspaceDb,
|
||||||
};
|
};
|
||||||
|
pub mod face_operations;
|
||||||
|
|||||||
Reference in New Issue
Block a user