Files
markbase/markbase-smb/src/monitor.rs
Warren d94cb2df4c Fix code quality: trailing whitespace, unused imports, clippy warnings
- Fix trailing whitespace in kex.rs and s3.rs
- Add missing KexProposal import in kex_complete.rs
- Auto-fix clippy warnings across all crates
- All 153 tests pass
2026-06-19 05:21:38 +08:00

114 lines
2.7 KiB
Rust

use serde::{Deserialize, Serialize};
use std::time::{Duration, SystemTime};
#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Default)]
pub struct ConnectionStats {
pub total_connections: u64,
pub active_connections: u32,
pub read_operations: u64,
pub write_operations: u64,
pub errors: u64,
pub bytes_transferred: u64,
pub uptime_seconds: u64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AccessLogEntry {
pub timestamp: String,
pub username: String,
pub action: String,
pub path: String,
pub success: bool,
pub bytes: u64,
pub duration_ms: u64,
}
impl AccessLogEntry {
pub fn new(
username: String,
action: String,
path: String,
success: bool,
bytes: u64,
duration: Duration,
) -> Self {
let timestamp = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_secs();
AccessLogEntry {
timestamp: timestamp.to_string(),
username,
action,
path,
success,
bytes,
duration_ms: duration.as_millis() as u64,
}
}
}
pub struct SMBMonitor {
stats: ConnectionStats,
logs: Vec<AccessLogEntry>,
start_time: SystemTime,
}
impl Default for SMBMonitor {
fn default() -> Self {
Self::new()
}
}
impl SMBMonitor {
pub fn new() -> Self {
SMBMonitor {
stats: ConnectionStats::default(),
logs: Vec::new(),
start_time: SystemTime::now(),
}
}
pub fn log_access(&mut self, entry: AccessLogEntry) {
self.logs.push(entry.clone());
if entry.success {
if entry.action == "read" {
self.stats.read_operations += 1;
} else if entry.action == "write" {
self.stats.write_operations += 1;
}
self.stats.bytes_transferred += entry.bytes;
} else {
self.stats.errors += 1;
}
}
pub fn connection_opened(&mut self) {
self.stats.total_connections += 1;
self.stats.active_connections += 1;
}
pub fn connection_closed(&mut self) {
self.stats.active_connections -= 1;
}
pub fn get_stats(&self) -> ConnectionStats {
let uptime = SystemTime::now()
.duration_since(self.start_time)
.unwrap()
.as_secs();
let mut stats = self.stats.clone();
stats.uptime_seconds = uptime;
stats
}
pub fn get_logs(&self, limit: usize) -> Vec<AccessLogEntry> {
self.logs.iter().rev().take(limit).cloned().collect()
}
}