feat: migrate logs API from REST to IPC streaming (#4277)

* feat: migrate logs API from REST to IPC streaming

- Replace REST API `/logs` calls with IPC streaming implementation
- Add new `src-tauri/src/ipc/logs.rs` with `LogsMonitor` for real-time log streaming
- Implement duplicate stream prevention with level tracking
- Add frontend-backend communication via Tauri commands for log management
- Remove WebSocket compatibility, maintain IPC-only mode
- Fix duplicate monitoring task startup when toggling log service
- Add proper task lifecycle management with JoinHandle cleanup

* refactor: remove dead code from logs.rs to fix clippy warnings

- Remove unused `timestamp` field from LogItem struct
- Remove unused `client` field from LogsMonitor struct
- Remove unused methods: `is_fresh`, `get_current_monitoring_level`, `get_current_logs`
- Simplify LogsMonitor initialization by removing client dependency
- All clippy warnings with -D warnings now resolved

* refactor: extract duplicate fmt_bytes function to utils module

- Create new utils/format.rs module with fmt_bytes function
- Remove duplicate fmt_bytes implementations from traffic.rs and memory.rs
- Update imports to use shared utils::format::fmt_bytes
- Add comprehensive unit tests for fmt_bytes function
- Ensure DRY principle compliance and code maintainability
This commit is contained in:
Tunglies
2025-07-30 23:11:21 +08:00
committed by GitHub
parent e2a548f6a5
commit 1f78d576a3
16 changed files with 526 additions and 161 deletions

View File

@@ -0,0 +1,25 @@
/// Format bytes into human readable string (B, KB, MB, GB)
pub fn fmt_bytes(bytes: u64) -> String {
const UNITS: &[&str] = &["B", "KB", "MB", "GB"];
let (mut val, mut unit) = (bytes as f64, 0);
while val >= 1024.0 && unit < 3 {
val /= 1024.0;
unit += 1;
}
format!("{:.1}{}", val, UNITS[unit])
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_fmt_bytes() {
assert_eq!(fmt_bytes(0), "0.0B");
assert_eq!(fmt_bytes(512), "512.0B");
assert_eq!(fmt_bytes(1024), "1.0KB");
assert_eq!(fmt_bytes(1536), "1.5KB");
assert_eq!(fmt_bytes(1024 * 1024), "1.0MB");
assert_eq!(fmt_bytes(1024 * 1024 * 1024), "1.0GB");
}
}