Something went wrong. Try again.
POC : Observability infrastructure managment
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106use serde::Deserialize;
use anyhow::{Context, Result};
use crate::config::Config;use crate::ssh;
#[derive(Debug, Clone, Deserialize)]pub struct ComposeService { #[serde(default, alias = "Service")] pub service: String, #[serde(default, alias = "Name")] pub name: String, #[serde(default, alias = "State")] pub state: String, #[serde(default, alias = "Status")] pub status: String, #[serde(default, alias = "Health")] pub health: String,}
impl ComposeService { pub fn display_name(&self) -> &str { if !self.service.is_empty() { &self.service } else { &self.name } }
pub fn is_running(&self) -> bool { self.state.eq_ignore_ascii_case("running") }
pub fn health_label(&self) -> &str { if self.health.is_empty() { "—" } else { &self.health } }}
pub fn parse_compose_ps(raw: &str) -> Result<Vec<ComposeService>> { let trimmed = raw.trim(); if trimmed.is_empty() { return Ok(Vec::new()); }
if trimmed.starts_with('[') { return serde_json::from_str(trimmed).context("parse docker compose ps (array)"); }
let mut services = Vec::new(); for line in trimmed.lines() { let line = line.trim(); if line.is_empty() { continue; } let svc: ComposeService = serde_json::from_str(line).with_context(|| format!("parse compose ps line: {line}"))?; services.push(svc); } Ok(services)}
fn compose_ps_cmd(path: &str) -> String { format!("cd {path} && docker compose ps --format json")}
pub fn status_hub_data(cfg: &Config) -> Result<Vec<ComposeService>> { let out = ssh::ssh_capture( &cfg.hub.user, &cfg.hub.host, &compose_ps_cmd(&cfg.hub.path), )?; parse_compose_ps(&out)}
pub fn status_satellite_data(cfg: &Config, name: &str) -> Result<Vec<ComposeService>> { let sat = cfg.satellite(name)?; let out = ssh::ssh_capture(&sat.user, &sat.host, &compose_ps_cmd(&sat.path))?; parse_compose_ps(&out)}
pub fn format_services_table(services: &[ComposeService]) -> String { if services.is_empty() { return "(no services)".to_string(); } let mut lines = Vec::with_capacity(services.len() + 1); lines.push(format!( "{:<20} {:<12} {:<10} {}", "SERVICE", "STATE", "HEALTH", "STATUS" )); for s in services { lines.push(format!( "{:<20} {:<12} {:<10} {}", s.display_name(), s.state, s.health_label(), s.status )); } lines.join("\n")}