use anyhow::{Result, Context};
use clap::Args;
use colored::*;
use std::sync::Arc;
use crate::config::AppConfig;
use crate::cli::repo_commands::helpers::get_collection_name;
use crate::vectordb::qdrant_client_trait::QdrantClientTrait;
use std::path::PathBuf;
use qdrant_client::qdrant::CountPointsBuilder;
#[derive(Args, Debug)]
#[derive(Clone)]
pub struct StatsArgs {
#[arg(short, long, value_parser = clap::value_parser!(PathBuf))]
pub config_file: Option<PathBuf>,
}
pub async fn handle_stats<C>(
_args: StatsArgs,
config: AppConfig,
_client: Arc<C>, ) -> Result<()>
where
C: QdrantClientTrait + Send + Sync + 'static,
{
let active_repo_name = config.active_repository.as_ref().ok_or_else(|| {
anyhow::anyhow!("No active repository set. Use 'repo use <repo_name>' first.")
})?;
let collection_name = get_collection_name(active_repo_name);
log::info!("Getting stats for repository: '{}', collection: '{}'", active_repo_name, collection_name);
println!("Fetching statistics for collection: {}", collection_name.cyan());
let collection_info_result = _client.get_collection_info(collection_name.clone()).await
.context(format!("Failed to get info for collection '{}'", collection_name));
if collection_info_result.is_err() {
println!("{}", " Error: Could not retrieve collection info (collection might not exist yet). Run 'repo sync'?".red());
return Ok(());
}
let collection_info = collection_info_result.unwrap();
let info = collection_info;
let count_request = CountPointsBuilder::new(&collection_name).exact(true).build();
let count_result = _client.count(count_request).await
.context(format!("Failed to count points in collection '{}'", collection_name))?;
let exact_count = count_result.result.map(|r| r.count).unwrap_or(0);
let info_status = info.status.to_string(); let vectors_count = info.vectors_count.unwrap_or(0);
let segments_count = info.segments_count;
println!(" Status: {}", info_status.green());
println!(" Point count: {}", exact_count.to_string().yellow());
println!(" Vector count: {}", vectors_count.to_string().yellow());
println!(" Segments: {}", segments_count.to_string().yellow());
Ok(())
}