use anyhow::Result;
use std::collections::HashMap;
use std::sync::atomic::Ordering;
use tracing::{debug, info};
use uuid::Uuid;
use super::vectorizer::ContentVectorizer;
impl ContentVectorizer {
pub fn get_query_cache_stats(
&self,
) -> Option<crate::query_cache::QueryCacheStatsSnapshot> {
self.query_cache.as_ref().map(|cache| cache.get_stats())
}
pub fn get_cache_efficiency_metrics(&self) -> Option<HashMap<String, f32>> {
self.query_cache.as_ref().map(|cache| cache.get_efficiency_metrics())
}
pub fn get_recency_bias_metrics(&self) -> Option<HashMap<String, f32>> {
let calculation_count = self.recency_bias_calculation_count.load(Ordering::Relaxed);
if calculation_count == 0 {
return None;
}
let total_duration_ns = self.recency_bias_total_duration_ns.load(Ordering::Relaxed);
let total_results = self.recency_bias_total_results.load(Ordering::Relaxed);
let mut metrics = HashMap::new();
metrics.insert(
"recency_bias.total_duration_ns".to_string(),
total_duration_ns as f32,
);
metrics.insert("recency_bias.total_results".to_string(), total_results as f32);
metrics.insert(
"recency_bias.calculation_count".to_string(),
calculation_count as f32,
);
metrics.insert(
"recency_bias.avg_duration_ns".to_string(),
total_duration_ns as f32 / calculation_count as f32,
);
metrics.insert(
"recency_bias.avg_results_per_calculation".to_string(),
total_results as f32 / calculation_count as f32,
);
let avg_time_per_result = if total_results > 0 {
total_duration_ns as f32 / total_results as f32
} else {
0.0
};
metrics.insert(
"recency_bias.avg_time_per_result_ns".to_string(),
avg_time_per_result,
);
Some(metrics)
}
pub async fn clear_query_cache(&self) -> Result<()> {
if let Some(ref cache) = self.query_cache {
cache.clear()?;
info!("Query cache cleared successfully");
}
Ok(())
}
pub async fn invalidate_session_cache(&self, session_id: Uuid) -> Result<()> {
if let Some(ref cache) = self.query_cache {
cache.invalidate_session(session_id)?;
debug!("Invalidated cache entries for session {}", session_id);
}
Ok(())
}
pub const fn is_query_caching_enabled(&self) -> bool {
self.query_cache.is_some()
}
}