feat: add cache toggle, unregister API and chunk training docs

- Add POST /api/v1/config/cache for cache toggle
- Add POST /api/v1/unregister for video deletion
- Add CHUNK_DATA_STRUCTURE.md for marcom training
- Fix processor_results query in delete_video
This commit is contained in:
Warren
2026-03-25 15:44:47 +08:00
parent de80b96c15
commit e4d3365ada
4 changed files with 409 additions and 0 deletions

View File

@@ -148,6 +148,30 @@ struct SearchRequest {
uuid: Option<String>,
}
#[derive(Debug, Deserialize)]
struct CacheToggleRequest {
enabled: bool,
}
#[derive(Debug, Serialize)]
struct CacheToggleResponse {
success: bool,
cache_enabled: bool,
message: String,
}
#[derive(Debug, Deserialize)]
struct UnregisterRequest {
uuid: String,
}
#[derive(Debug, Serialize)]
struct UnregisterResponse {
success: bool,
uuid: String,
message: String,
}
#[derive(Debug, Serialize, Deserialize)]
struct SearchResult {
uuid: String,
@@ -1300,6 +1324,56 @@ async fn get_job(
Ok(Json(response))
}
async fn cache_toggle(
State(_state): State<AppState>,
Json(req): Json<CacheToggleRequest>,
) -> Result<Json<CacheToggleResponse>, StatusCode> {
tracing::info!("[cache_toggle] Setting cache enabled to: {}", req.enabled);
crate::core::config::set_cache_enabled(req.enabled);
let response = CacheToggleResponse {
success: true,
cache_enabled: req.enabled,
message: if req.enabled {
"Cache enabled".to_string()
} else {
"Cache disabled".to_string()
},
};
tracing::info!("[cache_toggle] SUCCESS");
Ok(Json(response))
}
async fn unregister(
State(state): State<AppState>,
Json(req): Json<UnregisterRequest>,
) -> Result<Json<UnregisterResponse>, StatusCode> {
tracing::info!("[unregister] Unregistering video: {}", req.uuid);
let db = &state.api_state.db;
match db.delete_video(&req.uuid).await {
Ok(_) => {
tracing::info!("[unregister] SUCCESS - deleted: {}", req.uuid);
Ok(Json(UnregisterResponse {
success: true,
uuid: req.uuid,
message: "Video unregistered successfully".to_string(),
}))
}
Err(e) => {
tracing::error!("[unregister] ERROR - {}", e);
Ok(Json(UnregisterResponse {
success: false,
uuid: req.uuid,
message: format!("Failed to unregister: {}", e),
}))
}
}
}
pub async fn start_server(host: &str, port: u16) -> anyhow::Result<()> {
let _ = SERVER_START.set(Instant::now());
@@ -1319,6 +1393,7 @@ pub async fn start_server(host: &str, port: u16) -> anyhow::Result<()> {
let protected_routes = Router::new()
.route("/api/v1/register", post(register))
.route("/api/v1/unregister", post(unregister))
.route("/api/v1/probe", post(probe))
.route("/api/v1/search", post(search))
.route("/api/v1/n8n/search", post(n8n_search))
@@ -1328,6 +1403,7 @@ pub async fn start_server(host: &str, port: u16) -> anyhow::Result<()> {
.route("/api/v1/progress/:uuid", get(get_progress))
.route("/api/v1/jobs", get(list_jobs))
.route("/api/v1/jobs/:uuid", get(get_job))
.route("/api/v1/config/cache", post(cache_toggle))
.layer(axum::middleware::from_fn_with_state(
state.api_state.clone(),
api_key_validation,

View File

@@ -1,5 +1,23 @@
use once_cell::sync::Lazy;
use std::env;
use std::sync::RwLock;
pub static RUNTIME_CACHE_ENABLED: Lazy<RwLock<bool>> = Lazy::new(|| {
let initial = env::var("MONGODB_CACHE_ENABLED")
.unwrap_or_else(|_| "true".to_string())
.parse()
.unwrap_or(true);
RwLock::new(initial)
});
pub fn get_cache_enabled() -> bool {
*RUNTIME_CACHE_ENABLED.read().unwrap()
}
pub fn set_cache_enabled(enabled: bool) {
*RUNTIME_CACHE_ENABLED.write().unwrap() = enabled;
tracing::info!("Cache enabled set to: {}", enabled);
}
pub static DATABASE_URL: Lazy<String> = Lazy::new(|| {
env::var("DATABASE_URL")

View File

@@ -584,6 +584,40 @@ impl PostgresDb {
Ok(())
}
pub async fn delete_video(&self, uuid: &str) -> Result<()> {
tracing::info!("[PostgresDb] Deleting video: {}", uuid);
let tx = self.pool.begin().await?;
sqlx::query("DELETE FROM chunk_vectors WHERE uuid = $1")
.bind(uuid)
.execute(&self.pool)
.await?;
sqlx::query("DELETE FROM chunks WHERE uuid = $1")
.bind(uuid)
.execute(&self.pool)
.await?;
sqlx::query("DELETE FROM processor_results WHERE video_id IN (SELECT id FROM videos WHERE uuid = $1)")
.bind(uuid)
.execute(&self.pool)
.await?;
sqlx::query("DELETE FROM videos WHERE uuid = $1")
.bind(uuid)
.execute(&self.pool)
.await?;
tx.commit().await?;
let mut cache = self.cache.write().await;
cache.videos.remove(uuid);
tracing::info!("[PostgresDb] Video deleted: {}", uuid);
Ok(())
}
pub async fn get_storage_status(&self, uuid: &str) -> Result<Option<StorageStatus>> {
if let Some(video) = self.get_video_by_uuid(uuid).await? {
Ok(Some(video.storage))