use crate::analyzer::{StreamingAnalyzer, StreamingConfigBuilder};
use std::path::PathBuf;
use anyhow::Result;
pub async fn run(
project_path: PathBuf,
memory_limit: usize,
chunk_size: usize,
max_concurrent: usize,
aggressive_cleanup: bool,
format: String,
) -> Result<()> {
println!("๐ Starting streaming analysis for large project...");
println!("๐ Project Path: {}", project_path.display());
println!("๐พ Memory Limit: {} MB", memory_limit);
println!("๐ฆ Chunk Size: {}", chunk_size);
println!("โก Max Concurrent: {}", max_concurrent);
println!("๐งน Aggressive Cleanup: {}", if aggressive_cleanup { "enabled" } else { "disabled" });
println!();
let config = StreamingConfigBuilder::new()
.memory_limit_mb(memory_limit)
.chunk_size(chunk_size)
.max_concurrent_files(max_concurrent)
.aggressive_cleanup(aggressive_cleanup)
.enable_memory_monitoring(true)
.build();
let mut analyzer = StreamingAnalyzer::new(project_path, config)?;
let start_time = std::time::Instant::now();
let analysis = analyzer.analyze_streaming().await?;
let duration = start_time.elapsed();
let memory_stats = analyzer.get_memory_stats();
match format.as_str() {
"json" => {
let json_output = serde_json::to_string_pretty(&analysis)?;
println!("{}", json_output);
}
"yaml" => {
let yaml_output = serde_yaml::to_string(&analysis)?;
println!("{}", yaml_output);
}
"table" | _ => {
display_streaming_results(&analysis, memory_stats, duration);
}
}
Ok(())
}
fn display_streaming_results(
analysis: &crate::analyzer::ProjectAnalysis,
memory_stats: &crate::analyzer::MemoryStats,
duration: std::time::Duration,
) {
println!("๐ฏ === Streaming Analysis Results ===\n");
println!("๐ Project Information:");
println!(" Name: {}", analysis.project_name);
println!(" Framework: {}", analysis.framework_version);
println!();
println!("๐ Component Summary:");
println!(" Controllers: {}", analysis.controllers.len());
println!(" Routes: {}", analysis.routes.len());
println!(" Middleware: {}", analysis.middleware.len());
println!(" Models: {}", analysis.models.len());
println!(" Views: {}", analysis.views.len());
println!(" Issues: {}", analysis.issues.len());
println!();
println!("โก Performance Metrics:");
println!(" Analysis Duration: {:.2}s", duration.as_secs_f64());
println!(" Peak Memory Usage: {:.2} MB", memory_stats.peak_memory_mb);
println!(" Files Processed: {}", memory_stats.files_processed);
println!(" Chunks Processed: {}", memory_stats.chunks_processed);
println!(" Memory Warnings: {}", memory_stats.memory_warnings);
println!(" GC Collections: {}", memory_stats.gc_collections_triggered);
println!();
let memory_efficiency = if memory_stats.peak_memory_mb > 0.0 {
(memory_stats.files_processed as f64) / memory_stats.peak_memory_mb
} else {
0.0
};
println!("๐ Memory Efficiency:");
println!(" Files per MB: {:.1}", memory_efficiency);
if memory_stats.memory_warnings > 0 {
println!(" โ ๏ธ Memory limit exceeded {} times", memory_stats.memory_warnings);
} else {
println!(" โ
Memory usage stayed within limits");
}
println!();
if !analysis.routes.is_empty() {
println!("๐ฃ๏ธ Route Summary:");
let mut methods = std::collections::HashMap::new();
for route in &analysis.routes {
*methods.entry(&route.method).or_insert(0) += 1;
}
for (method, count) in methods {
println!(" {}: {}", method, count);
}
println!();
}
if !analysis.controllers.is_empty() {
println!("๐ฎ Controllers:");
for controller in &analysis.controllers {
println!(" {} ({} handlers)", controller.name, controller.handlers.len());
for handler in &controller.handlers {
let route_count = handler.routes.len();
let complexity_indicator = match handler.complexity {
0..=5 => "๐ข",
6..=15 => "๐ก",
_ => "๐ด",
};
println!(" {} {} ({} routes, complexity: {}) {}",
complexity_indicator, handler.name, route_count, handler.complexity, complexity_indicator);
}
}
println!();
}
if !analysis.issues.is_empty() {
println!("โ ๏ธ Issues Found:");
for issue in &analysis.issues {
let severity_icon = match issue.severity.as_str() {
"error" => "โ",
"warning" => "โ ๏ธ",
_ => "โน๏ธ",
};
println!(" {} {}", severity_icon, issue.message);
}
println!();
}
println!("๐ก Performance Recommendations:");
if memory_stats.peak_memory_mb > 200.0 {
println!(" ๐ฆ Consider reducing chunk size for better memory control");
}
if memory_stats.gc_collections_triggered > 5 {
println!(" ๐งน Frequent GC detected - consider lowering memory limit");
}
if memory_stats.files_processed < 100 && memory_stats.peak_memory_mb < 50.0 {
println!(" ๐ Small project detected - regular analysis might be faster");
}
if memory_stats.memory_warnings == 0 && memory_stats.peak_memory_mb < 100.0 {
println!(" โ
Excellent memory efficiency - streaming worked well!");
}
}