show actual metrics
This commit is contained in:
parent
e679896ca0
commit
cea133b7da
@ -11,6 +11,7 @@ zellij-tile = "0.40.0"
|
|||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
socktop_connector = { version = "0.1.5", default-features = false, features = ["wasm"] }
|
socktop_connector = { version = "0.1.5", default-features = false, features = ["wasm"] }
|
||||||
|
futures = "0.3"
|
||||||
|
|
||||||
[dependencies.chrono]
|
[dependencies.chrono]
|
||||||
version = "0.4"
|
version = "0.4"
|
||||||
|
|||||||
@ -131,25 +131,16 @@ impl State {
|
|||||||
fn fetch_metrics(&mut self) {
|
fn fetch_metrics(&mut self) {
|
||||||
unsafe {
|
unsafe {
|
||||||
if let Some(ref mut connector) = STATE.connector {
|
if let Some(ref mut connector) = STATE.connector {
|
||||||
// In a real implementation, you would:
|
// Try to get real metrics from socktop agent
|
||||||
// 1. Make an async call to connector.request(AgentRequest::Metrics)
|
match self.try_get_metrics(connector) {
|
||||||
// 2. Handle the response and update STATE.metrics_data
|
Ok(metrics_text) => {
|
||||||
// 3. Handle errors and update STATE.error_message
|
STATE.metrics_data = Some(metrics_text);
|
||||||
|
|
||||||
// For this scaffold, we'll simulate a response
|
|
||||||
match STATE.update_counter % 4 {
|
|
||||||
0 => {
|
|
||||||
STATE.metrics_data = Some("CPU: 45.2%, Memory: 67.8%".to_string());
|
|
||||||
STATE.connection_status = "Active".to_string();
|
STATE.connection_status = "Active".to_string();
|
||||||
|
STATE.error_message = None;
|
||||||
}
|
}
|
||||||
1 => {
|
Err(error) => {
|
||||||
STATE.metrics_data = Some("CPU: 32.1%, Memory: 71.3%".to_string());
|
STATE.error_message = Some(error);
|
||||||
}
|
STATE.connection_status = "Error".to_string();
|
||||||
2 => {
|
|
||||||
STATE.metrics_data = Some("CPU: 58.7%, Memory: 69.1%".to_string());
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
STATE.metrics_data = Some("CPU: 41.9%, Memory: 72.4%".to_string());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -158,29 +149,32 @@ impl State {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Async helper for real WebSocket operations (commented out for scaffold)
|
fn try_get_metrics(&mut self, connector: &mut SocktopConnector) -> Result<String, String> {
|
||||||
/*
|
// Note: This is synchronous for simplicity. In a real plugin you might need
|
||||||
async fn connect_and_fetch(connector: &mut SocktopConnector) -> Result<String, String> {
|
// to handle async operations differently depending on Zellij's threading model.
|
||||||
// Connect to socktop agent
|
|
||||||
connector.connect().await
|
// For now, we'll use a blocking approach or return a placeholder
|
||||||
.map_err(|e| format!("Connection failed: {}", e))?;
|
// that indicates we're trying to connect
|
||||||
|
|
||||||
|
// Attempt connection if not connected
|
||||||
|
if let Err(e) = futures::executor::block_on(connector.connect()) {
|
||||||
|
return Err(format!("Connection failed: {}", e));
|
||||||
|
}
|
||||||
|
|
||||||
// Request metrics
|
// Request metrics
|
||||||
let response = connector.request(AgentRequest::Metrics).await
|
match futures::executor::block_on(connector.request(AgentRequest::Metrics)) {
|
||||||
.map_err(|e| format!("Metrics request failed: {}", e))?;
|
Ok(AgentResponse::Metrics(metrics)) => {
|
||||||
|
Ok(format!(
|
||||||
// Format response
|
"CPU: {:.1}% | Mem: {:.1}% | Host: {} | Load: {:.2}",
|
||||||
match response {
|
|
||||||
AgentResponse::Metrics(metrics) => {
|
|
||||||
Ok(format!("CPU: {:.1}%, Mem: {:.1}%, Host: {}",
|
|
||||||
metrics.cpu_total,
|
metrics.cpu_total,
|
||||||
(metrics.mem_used as f64 / metrics.mem_total as f64) * 100.0,
|
(metrics.mem_used as f64 / metrics.mem_total as f64) * 100.0,
|
||||||
metrics.hostname
|
metrics.hostname,
|
||||||
|
metrics.load_avg_1m
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
_ => Err("Unexpected response type".to_string())
|
Ok(_) => Err("Unexpected response type".to_string()),
|
||||||
|
Err(e) => Err(format!("Request failed: {}", e)),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user