ruvector-memopt 0.5.0

Intelligent cross-platform memory optimizer with neural learning capabilities for smart optimization decisions
Documentation
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
//! Smart suggestions engine
//!
//! Analyzes system state and provides intelligent optimization recommendations:
//! - Prioritizes suggestions by impact
//! - Considers user patterns and usage
//! - Provides actionable recommendations
//! - Learns from system behavior

use super::{
    browser::BrowserOptimizer,
    docker::DockerManager,
    electron::ElectronManager,
    leaks::LeakDetector,
    AppCategory, OptimizationAction,
};
use serde::{Deserialize, Serialize};
use sysinfo::{System, ProcessesToUpdate};

/// Optimization suggestion
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Suggestion {
    pub priority: SuggestionPriority,
    pub category: AppCategory,
    pub title: String,
    pub description: String,
    pub action: OptimizationAction,
    pub estimated_savings_mb: f64,
    pub app_name: Option<String>,
    pub pids: Vec<u32>,
}

/// Suggestion priority level
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum SuggestionPriority {
    Low = 1,
    Medium = 2,
    High = 3,
    Critical = 4,
}

impl std::fmt::Display for SuggestionPriority {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            SuggestionPriority::Low => write!(f, "Low"),
            SuggestionPriority::Medium => write!(f, "Medium"),
            SuggestionPriority::High => write!(f, "High"),
            SuggestionPriority::Critical => write!(f, "Critical"),
        }
    }
}

/// System memory pressure level
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MemoryPressure {
    Low,      // <50% used
    Normal,   // 50-70% used
    High,     // 70-85% used
    Critical, // >85% used
}

/// Smart suggestions engine
pub struct SmartSuggestions {
    system: System,
    browser_optimizer: BrowserOptimizer,
    electron_manager: ElectronManager,
    docker_manager: DockerManager,
    suggestions: Vec<Suggestion>,
}

impl SmartSuggestions {
    pub fn new() -> Self {
        Self {
            system: System::new_all(),
            browser_optimizer: BrowserOptimizer::new(),
            electron_manager: ElectronManager::new(),
            docker_manager: DockerManager::new(),
            suggestions: Vec::new(),
        }
    }

    /// Refresh all data and generate suggestions
    pub fn refresh(&mut self) {
        self.system.refresh_all();
        self.browser_optimizer.refresh();
        self.electron_manager.refresh();
        self.docker_manager.refresh();

        self.generate_suggestions();
    }

    /// Get current memory pressure level
    pub fn memory_pressure(&self) -> MemoryPressure {
        let total = self.system.total_memory();
        let used = self.system.used_memory();

        if total == 0 {
            return MemoryPressure::Normal;
        }

        let percent = (used as f64 / total as f64) * 100.0;

        if percent > 85.0 {
            MemoryPressure::Critical
        } else if percent > 70.0 {
            MemoryPressure::High
        } else if percent > 50.0 {
            MemoryPressure::Normal
        } else {
            MemoryPressure::Low
        }
    }

    /// Generate all suggestions
    fn generate_suggestions(&mut self) {
        self.suggestions.clear();

        let pressure = self.memory_pressure();

        // Browser suggestions
        self.add_browser_suggestions(pressure);

        // Electron app suggestions
        self.add_electron_suggestions(pressure);

        // Docker suggestions
        self.add_docker_suggestions(pressure);

        // General high-memory process suggestions
        self.add_general_suggestions(pressure);

        // Sort by priority (highest first) then by estimated savings
        self.suggestions.sort_by(|a, b| {
            b.priority
                .cmp(&a.priority)
                .then(b.estimated_savings_mb.partial_cmp(&a.estimated_savings_mb).unwrap())
        });
    }

    /// Add browser-related suggestions
    fn add_browser_suggestions(&mut self, pressure: MemoryPressure) {
        for browser in self.browser_optimizer.get_browsers() {
            // Tab reduction suggestions
            if browser.estimated_tabs > 20 {
                let priority = if browser.total_memory_mb > 2000.0 || pressure == MemoryPressure::Critical {
                    SuggestionPriority::High
                } else if browser.total_memory_mb > 1000.0 || pressure == MemoryPressure::High {
                    SuggestionPriority::Medium
                } else {
                    SuggestionPriority::Low
                };

                let suggested_tabs = (browser.estimated_tabs / 2).max(10);
                let estimated_savings = browser.total_memory_mb * 0.3; // ~30% savings

                self.suggestions.push(Suggestion {
                    priority,
                    category: AppCategory::Browser,
                    title: format!("Reduce {} tabs", browser.name),
                    description: format!(
                        "{} has ~{} tabs using {:.0} MB. Consider reducing to {} tabs.",
                        browser.name, browser.estimated_tabs, browser.total_memory_mb, suggested_tabs
                    ),
                    action: OptimizationAction::ReduceTabs {
                        suggested_count: suggested_tabs,
                    },
                    estimated_savings_mb: estimated_savings,
                    app_name: Some(browser.name.clone()),
                    pids: browser.pids.clone(),
                });
            }

            // High memory browser warning
            if browser.total_memory_mb > 3000.0 {
                self.suggestions.push(Suggestion {
                    priority: SuggestionPriority::High,
                    category: AppCategory::Browser,
                    title: format!("Restart {}", browser.name),
                    description: format!(
                        "{} is using {:.0} MB - consider restarting to free memory",
                        browser.name, browser.total_memory_mb
                    ),
                    action: OptimizationAction::Restart,
                    estimated_savings_mb: browser.total_memory_mb * 0.5,
                    app_name: Some(browser.name.clone()),
                    pids: browser.pids.clone(),
                });
            }
        }
    }

    /// Add Electron app suggestions
    fn add_electron_suggestions(&mut self, pressure: MemoryPressure) {
        for app in self.electron_manager.get_apps() {
            // Bloated app warning
            if app.is_bloated() {
                let priority = if app.total_memory_mb > 1000.0 || pressure == MemoryPressure::Critical {
                    SuggestionPriority::High
                } else if pressure == MemoryPressure::High {
                    SuggestionPriority::Medium
                } else {
                    SuggestionPriority::Low
                };

                let excess = app.total_memory_mb - app.baseline_memory_mb;

                self.suggestions.push(Suggestion {
                    priority,
                    category: AppCategory::Electron,
                    title: format!("Restart {}", app.display_name),
                    description: format!(
                        "{} is using {:.0}% more memory than expected ({:.0} MB vs {:.0} MB baseline). Restart to reclaim ~{:.0} MB.",
                        app.display_name,
                        app.memory_overhead_percent - 100.0,
                        app.total_memory_mb,
                        app.baseline_memory_mb,
                        excess
                    ),
                    action: OptimizationAction::Restart,
                    estimated_savings_mb: excess,
                    app_name: Some(app.display_name.clone()),
                    pids: app.pids.clone(),
                });
            }

            // Very high memory apps
            if app.total_memory_mb > 1500.0 {
                self.suggestions.push(Suggestion {
                    priority: SuggestionPriority::High,
                    category: AppCategory::Electron,
                    title: format!("{} high memory", app.display_name),
                    description: format!(
                        "{} is using {:.0} MB across {} processes. Consider closing if not needed.",
                        app.display_name, app.total_memory_mb, app.process_count
                    ),
                    action: OptimizationAction::Close,
                    estimated_savings_mb: app.total_memory_mb,
                    app_name: Some(app.display_name.clone()),
                    pids: app.pids.clone(),
                });
            }
        }
    }

    /// Add Docker container suggestions
    fn add_docker_suggestions(&mut self, pressure: MemoryPressure) {
        if !self.docker_manager.is_available() {
            return;
        }

        // Idle containers
        for container in self.docker_manager.get_idle_containers() {
            if container.memory_mb > 200.0 {
                let priority = if pressure == MemoryPressure::Critical {
                    SuggestionPriority::High
                } else if pressure == MemoryPressure::High {
                    SuggestionPriority::Medium
                } else {
                    SuggestionPriority::Low
                };

                self.suggestions.push(Suggestion {
                    priority,
                    category: AppCategory::Container,
                    title: format!("Pause container {}", container.name),
                    description: format!(
                        "Container '{}' is idle but using {:.0} MB. Pause to save resources.",
                        container.name, container.memory_mb
                    ),
                    action: OptimizationAction::PauseContainer,
                    estimated_savings_mb: 0.0, // Pausing doesn't free memory but saves CPU
                    app_name: Some(container.name.clone()),
                    pids: Vec::new(),
                });
            }
        }

        // High memory containers
        for container in self.docker_manager.get_containers() {
            if container.memory_mb > 2000.0 {
                self.suggestions.push(Suggestion {
                    priority: SuggestionPriority::Medium,
                    category: AppCategory::Container,
                    title: format!("Container {} high memory", container.name),
                    description: format!(
                        "Container '{}' ({}) is using {:.0} MB ({:.0}% of limit).",
                        container.name, container.image, container.memory_mb, container.memory_percent
                    ),
                    action: OptimizationAction::StopContainer,
                    estimated_savings_mb: container.memory_mb,
                    app_name: Some(container.name.clone()),
                    pids: Vec::new(),
                });
            }
        }
    }

    /// Add general process suggestions
    fn add_general_suggestions(&mut self, pressure: MemoryPressure) {
        // Find high-memory processes not covered by specific optimizers
        let browser_pids: std::collections::HashSet<u32> = self
            .browser_optimizer
            .get_browsers()
            .iter()
            .flat_map(|b| b.pids.clone())
            .collect();

        let electron_pids: std::collections::HashSet<u32> = self
            .electron_manager
            .get_apps()
            .iter()
            .flat_map(|a| a.pids.clone())
            .collect();

        for (pid, process) in self.system.processes() {
            let pid_u32 = pid.as_u32();

            // Skip if already covered
            if browser_pids.contains(&pid_u32) || electron_pids.contains(&pid_u32) {
                continue;
            }

            let memory_mb = process.memory() as f64 / (1024.0 * 1024.0);
            let name = process.name().to_string_lossy().to_string();

            // High memory processes
            if memory_mb > 1000.0 {
                let priority = if memory_mb > 2000.0 || pressure == MemoryPressure::Critical {
                    SuggestionPriority::High
                } else {
                    SuggestionPriority::Medium
                };

                self.suggestions.push(Suggestion {
                    priority,
                    category: AppCategory::Other,
                    title: format!("{} high memory", name),
                    description: format!(
                        "'{}' (PID {}) is using {:.0} MB. Consider closing if not needed.",
                        name, pid_u32, memory_mb
                    ),
                    action: OptimizationAction::Close,
                    estimated_savings_mb: memory_mb,
                    app_name: Some(name),
                    pids: vec![pid_u32],
                });
            }
        }

        // Memory pressure warning
        if pressure == MemoryPressure::Critical {
            let total = self.system.total_memory() as f64 / (1024.0 * 1024.0);
            let used = self.system.used_memory() as f64 / (1024.0 * 1024.0);
            let available = total - used;

            self.suggestions.push(Suggestion {
                priority: SuggestionPriority::Critical,
                category: AppCategory::System,
                title: "Critical memory pressure".to_string(),
                description: format!(
                    "Only {:.0} MB available out of {:.0} MB total ({:.0}% used). System may become unstable.",
                    available, total, (used / total) * 100.0
                ),
                action: OptimizationAction::None,
                estimated_savings_mb: 0.0,
                app_name: None,
                pids: Vec::new(),
            });
        }
    }

    /// Get all suggestions
    pub fn get_suggestions(&self) -> &[Suggestion] {
        &self.suggestions
    }

    /// Get suggestions by category
    pub fn get_by_category(&self, category: AppCategory) -> Vec<&Suggestion> {
        self.suggestions
            .iter()
            .filter(|s| s.category == category)
            .collect()
    }

    /// Get suggestions by priority
    pub fn get_by_priority(&self, priority: SuggestionPriority) -> Vec<&Suggestion> {
        self.suggestions
            .iter()
            .filter(|s| s.priority == priority)
            .collect()
    }

    /// Get top N suggestions
    pub fn get_top(&self, n: usize) -> Vec<&Suggestion> {
        self.suggestions.iter().take(n).collect()
    }

    /// Get total potential savings
    pub fn total_potential_savings(&self) -> f64 {
        self.suggestions.iter().map(|s| s.estimated_savings_mb).sum()
    }

    /// Print suggestions summary
    pub fn print_summary(&self) {
        let pressure = self.memory_pressure();
        let pressure_icon = match pressure {
            MemoryPressure::Low => "🟢",
            MemoryPressure::Normal => "🟡",
            MemoryPressure::High => "🟠",
            MemoryPressure::Critical => "🔴",
        };

        println!("\n💡 Smart Optimization Suggestions\n");
        println!("Memory Pressure: {} {:?}", pressure_icon, pressure);

        let total = self.system.total_memory() as f64 / (1024.0 * 1024.0);
        let used = self.system.used_memory() as f64 / (1024.0 * 1024.0);
        println!(
            "Memory Usage: {:.0} MB / {:.0} MB ({:.0}%)\n",
            used,
            total,
            (used / total) * 100.0
        );

        if self.suggestions.is_empty() {
            println!("✅ No optimization suggestions at this time.");
            return;
        }

        println!("┌──────────┬──────────────────────────────┬───────────────┐");
        println!("│ Priority │ Suggestion                   │ Est. Savings  │");
        println!("├──────────┼──────────────────────────────┼───────────────┤");

        for suggestion in self.suggestions.iter().take(10) {
            let priority_icon = match suggestion.priority {
                SuggestionPriority::Critical => "🔴 Crit",
                SuggestionPriority::High => "🟠 High",
                SuggestionPriority::Medium => "🟡 Med",
                SuggestionPriority::Low => "🟢 Low",
            };

            let savings = if suggestion.estimated_savings_mb > 0.0 {
                format!("{:.0} MB", suggestion.estimated_savings_mb)
            } else {
                "-".to_string()
            };

            println!(
                "{:8} │ {:28} │ {:>13} │",
                priority_icon,
                truncate(&suggestion.title, 28),
                savings
            );
        }

        println!("└──────────┴──────────────────────────────┴───────────────┘");

        let total_savings = self.total_potential_savings();
        if total_savings > 0.0 {
            println!("\n💰 Total potential savings: {:.0} MB", total_savings);
        }

        println!("\n📋 Details:");
        for (i, suggestion) in self.suggestions.iter().take(5).enumerate() {
            println!("{}. {}", i + 1, suggestion.description);
        }

        if self.suggestions.len() > 5 {
            println!("   ... and {} more suggestions", self.suggestions.len() - 5);
        }
    }
}

impl Default for SmartSuggestions {
    fn default() -> Self {
        Self::new()
    }
}

fn truncate(s: &str, max: usize) -> String {
    if s.len() <= max {
        format!("{:width$}", s, width = max)
    } else {
        format!("{}...", &s[..max - 3])
    }
}