1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// File system event handling for QualityMonitorEngine
// Included by quality_monitor.rs - shares parent module scope
impl QualityMonitorEngine {
/// Handle file system events
async fn handle_file_system_event(
project_id: &str,
event: Event,
project_path: &Path,
config: &QualityMonitorConfig,
metrics: &Arc<RwLock<HashMap<String, QualityMetrics>>>,
event_sender: &Option<mpsc::Sender<QualityEvent>>,
) -> Result<()> {
debug!("File system event: {:?}", event);
// Filter relevant events
match event.kind {
EventKind::Create(_) | EventKind::Modify(_) | EventKind::Remove(_) => {
// Process changed files
for path in &event.paths {
if let Ok(relative_path) = path.strip_prefix(project_path) {
if Self::should_analyze_file(relative_path, &config.watch_patterns) {
info!("File changed: {:?}, triggering analysis", relative_path);
// Debounce: wait a bit to avoid excessive analysis
tokio::time::sleep(config.debounce_interval).await;
// Perform incremental analysis for the changed file
if let Err(e) = Self::analyze_changed_file(
project_id,
path,
relative_path,
&event.kind,
metrics,
event_sender,
)
.await
{
error!("Failed to analyze changed file {:?}: {}", relative_path, e);
}
}
}
}
}
_ => {
// Ignore other event types
}
}
Ok(())
}
/// Analyze a changed file and update metrics
async fn analyze_changed_file(
project_id: &str,
file_path: &PathBuf,
relative_path: &Path,
event_kind: &EventKind,
metrics: &Arc<RwLock<HashMap<String, QualityMetrics>>>,
event_sender: &Option<mpsc::Sender<QualityEvent>>,
) -> Result<()> {
info!(
"Analyzing changed file: {:?} (event: {:?})",
relative_path, event_kind
);
// Get current file metrics
let file_metrics = Self::analyze_file_metrics(file_path, relative_path).await?;
// Update project metrics
{
let mut metrics_map = metrics.write().await;
if let Some(project_metrics) = metrics_map.get_mut(project_id) {
let file_path_str = relative_path.to_string_lossy().to_string();
// Check if this is a new file or updated file
let is_new_file = !project_metrics.file_metrics.contains_key(&file_path_str);
let old_metrics = project_metrics.file_metrics.get(&file_path_str).cloned();
// Update file metrics
project_metrics
.file_metrics
.insert(file_path_str.clone(), file_metrics.clone());
project_metrics.last_updated = SystemTime::now();
// Update aggregate metrics
Self::update_aggregate_metrics(project_metrics);
// Send event notification
if let Some(sender) = event_sender {
let event = if is_new_file {
QualityEvent::FileAnalyzed {
project_id: project_id.to_string(),
file_path: file_path_str,
metrics: file_metrics,
}
} else if let Some(old) = old_metrics {
// Check for quality changes
let changes =
Self::detect_quality_changes(&old, &file_metrics, &file_path_str);
QualityEvent::MetricsUpdated {
project_id: project_id.to_string(),
metrics: project_metrics.clone(),
changes,
}
} else {
QualityEvent::FileAnalyzed {
project_id: project_id.to_string(),
file_path: file_path_str,
metrics: file_metrics,
}
};
if let Err(e) = sender.try_send(event) {
warn!("Failed to send quality event: {}", e);
}
}
}
}
Ok(())
}
/// Check if a file should be analyzed based on patterns
fn should_analyze_file(file_path: &Path, patterns: &[String]) -> bool {
let file_str = file_path.to_string_lossy();
// Check if file matches any watch pattern
for pattern in patterns {
if pattern.contains("**") {
// Simple glob pattern matching
let extension = pattern.strip_prefix("**/").unwrap_or(pattern);
if file_str.ends_with(extension.strip_prefix("*").unwrap_or(extension)) {
return true;
}
} else if file_str.contains(pattern) {
return true;
}
}
false
}
}