31d1b4050f
- Changed min_confidence from 50.0 to 0.5 (Vision uses 0.0-1.0 range) - Changed min_size from 32 to 1 (was filtering out all small faces) - Changed min_capture_quality from 25.0 to 0.25 - Fixes issue where all videos showed 0 faces detected Root cause: admin_config.rs used wrong defaults (50.0/32) instead of config.rs constants (0.5/1), causing Vision framework to filter out ALL faces because confidence threshold exceeded valid range. Verified: IMG_9449 now correctly detects 191 faces (was 0).
544 lines
17 KiB
Rust
544 lines
17 KiB
Rust
use anyhow::{Context, Result};
|
|
use serde::{Deserialize, Serialize};
|
|
use std::collections::HashMap;
|
|
use std::sync::Arc;
|
|
use tokio::sync::RwLock;
|
|
use tracing::error;
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct VlmPrompt {
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
pub system_prompt: Option<String>,
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
pub user_prompt: Option<String>,
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
pub prompt: Option<String>,
|
|
pub max_tokens: u32,
|
|
pub description: String,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct LlmConfig {
|
|
pub url: String,
|
|
pub model: String,
|
|
#[serde(default)]
|
|
pub timeout: u32,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct ProcessorConfig {
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
pub model_size: Option<String>,
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
pub language: Option<String>,
|
|
#[serde(default)]
|
|
pub timeout: u32,
|
|
#[serde(default = "default_true")]
|
|
pub enabled: bool,
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
pub confidence: Option<f32>,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct FilterConfig {
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
pub min_confidence: Option<f32>,
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
pub min_size: Option<u32>,
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
pub min_capture_quality: Option<f32>,
|
|
}
|
|
|
|
fn default_true() -> bool {
|
|
true
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct PathsConfig {
|
|
#[serde(default = "default_output_dir")]
|
|
pub output_dir: String,
|
|
#[serde(default = "default_backup_dir")]
|
|
pub backup_dir: String,
|
|
#[serde(default = "default_scripts_dir")]
|
|
pub scripts_dir: String,
|
|
#[serde(default = "default_python_path")]
|
|
pub python_path: String,
|
|
#[serde(default = "default_ffmpeg_path")]
|
|
pub ffmpeg_path: String,
|
|
}
|
|
|
|
fn default_output_dir() -> String {
|
|
std::env::var("MOMENTRY_OUTPUT_DIR")
|
|
.unwrap_or_else(|_| "/Users/accusys/momentry/output".to_string())
|
|
}
|
|
|
|
fn default_backup_dir() -> String {
|
|
std::env::var("MOMENTRY_BACKUP_DIR")
|
|
.unwrap_or_else(|_| "/Users/accusys/momentry/backup/momentry".to_string())
|
|
}
|
|
|
|
fn default_scripts_dir() -> String {
|
|
std::env::var("MOMENTRY_SCRIPTS_DIR")
|
|
.unwrap_or_else(|_| "/Users/accusys/momentry_core_0.1/scripts".to_string())
|
|
}
|
|
|
|
fn default_python_path() -> String {
|
|
std::env::var("MOMENTRY_PYTHON_PATH")
|
|
.unwrap_or_else(|_| "/opt/homebrew/bin/python3.11".to_string())
|
|
}
|
|
|
|
fn default_ffmpeg_path() -> String {
|
|
std::env::var("MOMENTRY_FFMPEG")
|
|
.unwrap_or_else(|_| "ffmpeg".to_string())
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct FeatureFlags {
|
|
#[serde(default = "default_cache_enabled")]
|
|
pub cache_enabled: bool,
|
|
#[serde(default)]
|
|
pub auto_pipeline_enabled: bool,
|
|
#[serde(default)]
|
|
pub watcher_auto_register: bool,
|
|
#[serde(default = "default_true")]
|
|
pub summary_enabled: bool,
|
|
}
|
|
|
|
fn default_cache_enabled() -> bool {
|
|
std::env::var("MONGODB_CACHE_ENABLED")
|
|
.map(|v| v == "true" || v == "1")
|
|
.unwrap_or(true)
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct ConcurrencyConfig {
|
|
#[serde(default = "default_max_concurrent_jobs")]
|
|
pub max_concurrent_jobs: u32,
|
|
#[serde(default = "default_poll_interval")]
|
|
pub poll_interval: u32,
|
|
#[serde(default = "default_batch_size")]
|
|
pub batch_size: u32,
|
|
#[serde(default = "default_processor_timeout_secs")]
|
|
pub processor_timeout_secs: u32,
|
|
}
|
|
|
|
fn default_max_concurrent_jobs() -> u32 {
|
|
std::env::var("MOMENTRY_MAX_CONCURRENT")
|
|
.ok()
|
|
.and_then(|v| v.parse().ok())
|
|
.unwrap_or(6)
|
|
}
|
|
|
|
fn default_poll_interval() -> u32 {
|
|
std::env::var("MOMENTRY_POLL_INTERVAL")
|
|
.ok()
|
|
.and_then(|v| v.parse().ok())
|
|
.unwrap_or(10)
|
|
}
|
|
|
|
fn default_batch_size() -> u32 {
|
|
std::env::var("MOMENTRY_WORKER_BATCH_SIZE")
|
|
.ok()
|
|
.and_then(|v| v.parse().ok())
|
|
.unwrap_or(10)
|
|
}
|
|
|
|
fn default_processor_timeout_secs() -> u32 {
|
|
std::env::var("MOMENTRY_WORKER_PROCESSOR_TIMEOUT")
|
|
.ok()
|
|
.and_then(|v| v.parse().ok())
|
|
.unwrap_or(3600)
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct AdminSettings {
|
|
#[serde(default = "default_session_timeout")]
|
|
pub session_timeout: u32,
|
|
#[serde(default = "default_max_login_attempts")]
|
|
pub max_login_attempts: u32,
|
|
#[serde(default = "default_api_key_expiry_days")]
|
|
pub api_key_expiry_days: u32,
|
|
#[serde(default = "default_system_stats_refresh_sec")]
|
|
pub system_stats_refresh_sec: u32,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct FaceFilterDefaults {
|
|
#[serde(default = "default_min_confidence")]
|
|
pub min_confidence: f64,
|
|
#[serde(default = "default_min_size")]
|
|
pub min_size: i32,
|
|
#[serde(default = "default_min_capture_quality")]
|
|
pub min_capture_quality: f64,
|
|
}
|
|
|
|
fn default_min_confidence() -> f64 { crate::core::config::FACE_MIN_CONFIDENCE_DEFAULT }
|
|
fn default_min_size() -> i32 { crate::core::config::FACE_MIN_SIZE_DEFAULT }
|
|
fn default_min_capture_quality() -> f64 { 0.25 }
|
|
|
|
fn default_session_timeout() -> u32 {
|
|
3600
|
|
}
|
|
|
|
fn default_max_login_attempts() -> u32 {
|
|
5
|
|
}
|
|
|
|
fn default_api_key_expiry_days() -> u32 {
|
|
365
|
|
}
|
|
|
|
fn default_system_stats_refresh_sec() -> u32 {
|
|
std::env::var("SYSTEM_STATS_REFRESH_SEC")
|
|
.ok()
|
|
.and_then(|v| v.parse().ok())
|
|
.unwrap_or(5)
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct ServerConfig {
|
|
#[serde(default = "default_server_port")]
|
|
pub port: u16,
|
|
#[serde(default = "default_media_base_url")]
|
|
pub media_base_url: String,
|
|
#[serde(default = "default_serve_base_url")]
|
|
pub serve_base_url: String,
|
|
#[serde(default = "default_storage_root")]
|
|
pub storage_root: String,
|
|
}
|
|
|
|
fn default_server_port() -> u16 {
|
|
std::env::var("MOMENTRY_SERVER_PORT")
|
|
.ok()
|
|
.and_then(|v| v.parse().ok())
|
|
.unwrap_or(3002)
|
|
}
|
|
|
|
fn default_media_base_url() -> String {
|
|
std::env::var("MOMENTRY_MEDIA_BASE_URL")
|
|
.unwrap_or_else(|_| "https://wp.momentry.ddns.net".to_string())
|
|
}
|
|
|
|
fn default_serve_base_url() -> String {
|
|
std::env::var("MOMENTRY_SERVE_BASE_URL")
|
|
.unwrap_or_else(|_| "https://m5wp.momentry.ddns.net/files".to_string())
|
|
}
|
|
|
|
fn default_storage_root() -> String {
|
|
std::env::var("MOMENTRY_STORAGE_ROOT")
|
|
.unwrap_or_else(|_| "/Users/accusys/momentry/var/sftpgo/data".to_string())
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct AdminConfig {
|
|
pub version: String,
|
|
pub updated_at: String,
|
|
pub updated_by: String,
|
|
pub vlm_prompts: HashMap<String, VlmPrompt>,
|
|
pub llm_config: HashMap<String, LlmConfig>,
|
|
pub processor_config: HashMap<String, ProcessorConfig>,
|
|
#[serde(default)]
|
|
pub filter_config: HashMap<String, FilterConfig>,
|
|
pub face_filter_defaults: FaceFilterDefaults,
|
|
pub paths: PathsConfig,
|
|
pub concurrency: ConcurrencyConfig,
|
|
pub feature_flags: FeatureFlags,
|
|
pub server: ServerConfig,
|
|
pub admin_settings: AdminSettings,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct PartialAdminConfig {
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
pub vlm_prompts: Option<HashMap<String, VlmPrompt>>,
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
pub llm_config: Option<HashMap<String, LlmConfig>>,
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
pub processor_config: Option<HashMap<String, ProcessorConfig>>,
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
pub filter_config: Option<HashMap<String, FilterConfig>>,
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
pub face_filter_defaults: Option<FaceFilterDefaults>,
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
pub paths: Option<PathsConfig>,
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
pub concurrency: Option<ConcurrencyConfig>,
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
pub feature_flags: Option<FeatureFlags>,
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
pub server: Option<ServerConfig>,
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
pub admin_settings: Option<AdminSettings>,
|
|
}
|
|
|
|
pub struct AdminConfigManager {
|
|
config: Arc<RwLock<AdminConfig>>,
|
|
config_path: String,
|
|
}
|
|
|
|
impl AdminConfigManager {
|
|
pub fn new(config_path: &str) -> Result<Self> {
|
|
let config = Self::load_config(config_path)?;
|
|
tracing::info!("[AdminConfig] Loaded config from {}", config_path);
|
|
Ok(Self {
|
|
config: Arc::new(RwLock::new(config)),
|
|
config_path: config_path.to_string(),
|
|
})
|
|
}
|
|
|
|
fn load_config(path: &str) -> Result<AdminConfig> {
|
|
let content = std::fs::read_to_string(path)
|
|
.with_context(|| format!("Failed to read config file: {}", path))?;
|
|
let config: AdminConfig = serde_json::from_str(&content)
|
|
.with_context(|| "Failed to parse config JSON")?;
|
|
Ok(config)
|
|
}
|
|
|
|
pub async fn get_vlm_prompt(&self, key: &str) -> Option<VlmPrompt> {
|
|
let config = self.config.read().await;
|
|
config.vlm_prompts.get(key).cloned()
|
|
}
|
|
|
|
pub async fn get_all_vlm_prompts(&self) -> HashMap<String, VlmPrompt> {
|
|
let config = self.config.read().await;
|
|
config.vlm_prompts.clone()
|
|
}
|
|
|
|
pub async fn get_llm_config(&self, key: &str) -> Option<LlmConfig> {
|
|
let config = self.config.read().await;
|
|
config.llm_config.get(key).cloned()
|
|
}
|
|
|
|
pub async fn get_processor_config(&self, key: &str) -> Option<ProcessorConfig> {
|
|
let config = self.config.read().await;
|
|
config.processor_config.get(key).cloned()
|
|
}
|
|
|
|
pub async fn get_filter_config(&self, key: &str) -> Option<FilterConfig> {
|
|
let config = self.config.read().await;
|
|
config.filter_config.get(key).cloned()
|
|
}
|
|
|
|
pub async fn get_paths(&self) -> PathsConfig {
|
|
let config = self.config.read().await;
|
|
config.paths.clone()
|
|
}
|
|
|
|
pub async fn get_concurrency(&self) -> ConcurrencyConfig {
|
|
let config = self.config.read().await;
|
|
config.concurrency.clone()
|
|
}
|
|
|
|
pub async fn get_feature_flags(&self) -> FeatureFlags {
|
|
let config = self.config.read().await;
|
|
config.feature_flags.clone()
|
|
}
|
|
|
|
pub async fn get_server(&self) -> ServerConfig {
|
|
let config = self.config.read().await;
|
|
config.server.clone()
|
|
}
|
|
|
|
pub async fn get_admin_settings(&self) -> AdminSettings {
|
|
let config = self.config.read().await;
|
|
config.admin_settings.clone()
|
|
}
|
|
|
|
pub async fn get_full_config(&self) -> AdminConfig {
|
|
let config = self.config.read().await;
|
|
config.clone()
|
|
}
|
|
|
|
pub async fn update_vlm_prompt(&self, key: &str, prompt: VlmPrompt) -> Result<()> {
|
|
let mut config = self.config.write().await;
|
|
config.vlm_prompts.insert(key.to_string(), prompt);
|
|
|
|
config.updated_at = chrono::Utc::now().to_rfc3339();
|
|
config.updated_by = "admin".to_string();
|
|
|
|
self.save_config(&config)?;
|
|
|
|
tracing::info!("[AdminConfig] Updated VLM prompt: {}", key);
|
|
Ok(())
|
|
}
|
|
|
|
pub async fn update_partial_config(&self, partial: PartialAdminConfig) -> Result<()> {
|
|
let mut config = self.config.write().await;
|
|
|
|
if let Some(vlm_prompts) = partial.vlm_prompts {
|
|
config.vlm_prompts.extend(vlm_prompts);
|
|
}
|
|
if let Some(llm_config) = partial.llm_config {
|
|
config.llm_config.extend(llm_config);
|
|
}
|
|
if let Some(processor_config) = partial.processor_config {
|
|
config.processor_config.extend(processor_config);
|
|
}
|
|
if let Some(filter_config) = partial.filter_config {
|
|
config.filter_config.extend(filter_config);
|
|
}
|
|
if let Some(face_filter_defaults) = partial.face_filter_defaults {
|
|
config.face_filter_defaults = face_filter_defaults;
|
|
}
|
|
if let Some(paths) = partial.paths {
|
|
config.paths = paths;
|
|
}
|
|
if let Some(concurrency) = partial.concurrency {
|
|
config.concurrency = concurrency;
|
|
}
|
|
if let Some(feature_flags) = partial.feature_flags {
|
|
config.feature_flags = feature_flags;
|
|
}
|
|
if let Some(server) = partial.server {
|
|
config.server = server;
|
|
}
|
|
if let Some(admin_settings) = partial.admin_settings {
|
|
config.admin_settings = admin_settings;
|
|
}
|
|
|
|
config.updated_at = chrono::Utc::now().to_rfc3339();
|
|
config.updated_by = "admin".to_string();
|
|
|
|
self.save_config(&config)?;
|
|
|
|
tracing::info!("[AdminConfig] Updated partial config");
|
|
Ok(())
|
|
}
|
|
|
|
pub async fn reload(&self) -> Result<()> {
|
|
let new_config = Self::load_config(&self.config_path)?;
|
|
let mut config = self.config.write().await;
|
|
*config = new_config;
|
|
|
|
tracing::info!("[AdminConfig] Reloaded config from {}", self.config_path);
|
|
Ok(())
|
|
}
|
|
|
|
fn save_config(&self, config: &AdminConfig) -> Result<()> {
|
|
let content = serde_json::to_string_pretty(config)
|
|
.with_context(|| "Failed to serialize config")?;
|
|
std::fs::write(&self.config_path, content)
|
|
.with_context(|| format!("Failed to write config file: {}", self.config_path))?;
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
pub fn get_default_config_path() -> String {
|
|
let mut path = std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from("."));
|
|
path.push("config");
|
|
path.push("admin_config.json");
|
|
path.to_str().unwrap_or("config/admin_config.json").to_string()
|
|
}
|
|
|
|
pub static ADMIN_CONFIG: once_cell::sync::Lazy<Arc<AdminConfigManager>> = once_cell::sync::Lazy::new(|| {
|
|
let config_path = get_default_config_path();
|
|
match AdminConfigManager::new(&config_path) {
|
|
Ok(manager) => Arc::new(manager),
|
|
Err(e) => {
|
|
error!("[AdminConfig] Failed to initialize: {}", e);
|
|
panic!("Failed to initialize AdminConfigManager: {}", e);
|
|
}
|
|
}
|
|
});
|
|
|
|
pub async fn get_vlm_prompt(key: &str) -> Option<VlmPrompt> {
|
|
ADMIN_CONFIG.get_vlm_prompt(key).await
|
|
}
|
|
|
|
pub async fn get_llm_config(key: &str) -> Option<LlmConfig> {
|
|
ADMIN_CONFIG.get_llm_config(key).await
|
|
}
|
|
|
|
pub async fn get_processor_config(key: &str) -> Option<ProcessorConfig> {
|
|
ADMIN_CONFIG.get_processor_config(key).await
|
|
}
|
|
|
|
pub async fn get_filter_config(key: &str) -> Option<FilterConfig> {
|
|
ADMIN_CONFIG.get_filter_config(key).await
|
|
}
|
|
|
|
/// Synchronous version for getting face filter defaults
|
|
/// Used in synchronous contexts like FileProfile::defaults()
|
|
pub fn get_face_filter_defaults() -> (f64, i32, f64) {
|
|
// Use blocking_read for sync context
|
|
// Returns (min_confidence, min_size, min_capture_quality)
|
|
let config = ADMIN_CONFIG.config.blocking_read();
|
|
|
|
(
|
|
config.face_filter_defaults.min_confidence,
|
|
config.face_filter_defaults.min_size,
|
|
config.face_filter_defaults.min_capture_quality,
|
|
)
|
|
}
|
|
|
|
pub async fn get_paths() -> PathsConfig {
|
|
ADMIN_CONFIG.get_paths().await
|
|
}
|
|
|
|
pub async fn get_concurrency() -> ConcurrencyConfig {
|
|
ADMIN_CONFIG.get_concurrency().await
|
|
}
|
|
|
|
pub async fn get_feature_flags() -> FeatureFlags {
|
|
ADMIN_CONFIG.get_feature_flags().await
|
|
}
|
|
|
|
pub async fn get_server() -> ServerConfig {
|
|
ADMIN_CONFIG.get_server().await
|
|
}
|
|
|
|
pub fn get_paths_sync() -> PathsConfig {
|
|
PathsConfig {
|
|
output_dir: std::env::var("MOMENTRY_OUTPUT_DIR")
|
|
.unwrap_or_else(|_| "/Users/accusys/momentry/output".to_string()),
|
|
backup_dir: std::env::var("MOMENTRY_BACKUP_DIR")
|
|
.unwrap_or_else(|_| "/Users/accusys/momentry/backup/momentry".to_string()),
|
|
scripts_dir: std::env::var("MOMENTRY_SCRIPTS_DIR")
|
|
.unwrap_or_else(|_| "/Users/accusys/momentry_core_0.1/scripts".to_string()),
|
|
python_path: std::env::var("MOMENTRY_PYTHON_PATH")
|
|
.unwrap_or_else(|_| "/opt/homebrew/bin/python3.11".to_string()),
|
|
ffmpeg_path: std::env::var("MOMENTRY_FFMPEG")
|
|
.unwrap_or_else(|_| "ffmpeg".to_string()),
|
|
}
|
|
}
|
|
|
|
pub fn get_output_dir() -> String {
|
|
get_paths_sync().output_dir
|
|
}
|
|
|
|
pub fn get_backup_dir() -> String {
|
|
get_paths_sync().backup_dir
|
|
}
|
|
|
|
pub fn get_scripts_dir() -> String {
|
|
get_paths_sync().scripts_dir
|
|
}
|
|
|
|
pub fn get_python_path() -> String {
|
|
get_paths_sync().python_path
|
|
}
|
|
|
|
pub fn get_ffmpeg_path() -> String {
|
|
get_paths_sync().ffmpeg_path
|
|
}
|
|
|
|
pub fn get_concurrency_sync() -> ConcurrencyConfig {
|
|
ConcurrencyConfig {
|
|
max_concurrent_jobs: std::env::var("MOMENTRY_MAX_CONCURRENT")
|
|
.ok()
|
|
.and_then(|v| v.parse().ok())
|
|
.unwrap_or(6),
|
|
poll_interval: std::env::var("MOMENTRY_POLL_INTERVAL")
|
|
.ok()
|
|
.and_then(|v| v.parse().ok())
|
|
.unwrap_or(10),
|
|
batch_size: std::env::var("MOMENTRY_WORKER_BATCH_SIZE")
|
|
.ok()
|
|
.and_then(|v| v.parse().ok())
|
|
.unwrap_or(10),
|
|
processor_timeout_secs: std::env::var("MOMENTRY_WORKER_PROCESSOR_TIMEOUT")
|
|
.ok()
|
|
.and_then(|v| v.parse().ok())
|
|
.unwrap_or(3600),
|
|
}
|
|
} |