Initial Commit
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
use serde::Deserialize;
|
||||
use std::fs::File;
|
||||
|
||||
use crate::models::host::HostResult;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct ExportRequest {
|
||||
pub path: String,
|
||||
pub hosts: Vec<HostResult>,
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn export_csv(request: ExportRequest) -> Result<(), String> {
|
||||
let file = File::create(&request.path)
|
||||
.map_err(|e| format!("Failed to create file: {}", e))?;
|
||||
|
||||
let mut writer = csv::Writer::from_writer(file);
|
||||
|
||||
// Header
|
||||
writer
|
||||
.write_record(["IP Address", "Status", "Latency (ms)", "Hostname"])
|
||||
.map_err(|e| format!("Failed to write header: {}", e))?;
|
||||
|
||||
// Rows
|
||||
for host in &request.hosts {
|
||||
let ip = host.ip.to_string();
|
||||
let status = format!("{:?}", host.status);
|
||||
let rtt = match &host.ping {
|
||||
Some(p) => format!("{:.1}", p.rtt_ms),
|
||||
None => String::from("-"),
|
||||
};
|
||||
let hostname = host.hostname.clone().unwrap_or_default();
|
||||
|
||||
writer
|
||||
.write_record([&ip, &status, &rtt, &hostname])
|
||||
.map_err(|e| format!("Failed to write row: {}", e))?;
|
||||
}
|
||||
|
||||
writer
|
||||
.flush()
|
||||
.map_err(|e| format!("Failed to flush: {}", e))?;
|
||||
|
||||
tracing::info!("Exported {} hosts to {}", request.hosts.len(), request.path);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user