use anyhow::{Context, Result};
use colored::Colorize;
use std::path::PathBuf;
use firecloud_storage::ChunkStore;
use crate::config::FireCloudConfig;
use indicatif::ProgressBar;
pub async fn run(data_dir: PathBuf) -> Result<()> {
println!("\n{}", "📊 Storage Provider Dashboard".bold().cyan());
println!("{}", "─".repeat(50).dimmed());
let config_path = data_dir.join("config.toml");
let config = FireCloudConfig::load(&config_path)
.context("Failed to load configuration")?;
let quota = match config.storage_quota {
Some(q) => q,
None => {
println!("\n{} You are not configured as a storage provider", "ℹ".blue());
println!(" To provide storage, run: {}\n", "firecloud mod".cyan());
return Ok(());
}
};
println!("\n{} Storage Provider Status: {}", "✓".green(), "Active".bold());
let chunk_store_path = data_dir.join("chunks");
let chunk_store = ChunkStore::open(&chunk_store_path)
.context("Failed to open chunk store")?;
let chunk_count = chunk_store.count()? as u64;
let total_bytes = chunk_store.total_size()?;
let usage_percent = if quota > 0 {
((total_bytes as f64 / quota as f64) * 100.0) as u64
} else {
0
};
println!("\n{}", "Storage Allocation:".bold());
println!(" Total Quota: {}", format_size(quota).yellow());
println!(" Used Space: {}", format_size(total_bytes).cyan());
println!(" Free Space: {}", format_size(quota.saturating_sub(total_bytes)).green());
println!("\n{}", "Usage:".bold());
let bar = ProgressBar::new(100);
bar.set_style(
indicatif::ProgressStyle::default_bar()
.template("{bar:40.cyan/blue} {percent}%")
.unwrap()
.progress_chars("█▓▒░ ")
);
bar.set_position(usage_percent.min(100));
bar.finish();
println!("\n{}", "Chunk Statistics:".bold());
println!(" Total Chunks Stored: {}", chunk_count);
println!(" Average Chunk Size: {}",
if chunk_count > 0 {
format_size(total_bytes / chunk_count)
} else {
"N/A".to_string()
});
println!("\n{}", "Health Status:".bold());
if usage_percent < 80 {
println!(" Storage: {}", "🟢 Healthy".green());
} else if usage_percent < 95 {
println!(" Storage: {}", "🟡 Warning - Low Space".yellow());
} else {
println!(" Storage: {}", "🔴 Critical - Almost Full".red());
}
if usage_percent > 90 {
println!("\n{}", "⚠️ Recommendations:".yellow().bold());
println!(" • Consider increasing storage quota: {}", "firecloud mod".cyan());
println!(" • Or disable storage sharing to prevent new chunks");
}
println!("\n{}", "Actions:".bold());
println!(" • Modify quota: {}", "firecloud mod".cyan());
println!(" • View network: {}", "firecloud network".cyan());
println!();
Ok(())
}
fn format_size(bytes: u64) -> String {
const KB: u64 = 1_024;
const MB: u64 = 1_024 * 1_024;
const GB: u64 = 1_024 * 1_024 * 1_024;
const TB: u64 = 1_024 * 1_024 * 1_024 * 1_024;
if bytes >= TB {
format!("{:.2} TB", bytes as f64 / TB as f64)
} else if bytes >= GB {
format!("{:.2} GB", bytes as f64 / GB as f64)
} else if bytes >= MB {
format!("{:.2} MB", bytes as f64 / MB as f64)
} else if bytes >= KB {
format!("{:.2} KB", bytes as f64 / KB as f64)
} else {
format!("{} B", bytes)
}
}