use std::sync::Arc;
use anyhow::{bail, Result};
use colored::Colorize;
use serde_json::{json, Value};
use toon_format::encode_default as toon_encode;
pub mod api;
use api::{AlertDef, AlertsApi};
use crate::config::OutputFormat;
use crate::error::CxError;
use crate::execution::{fan_out, ExecutionTarget};
use crate::render;
fn alert_to_json(alert: &AlertDef, include_profile: bool, profile: &str) -> Value {
let mut v = json!({
"id": alert.id,
"name": alert.display_name(),
"description": alert.display_description(),
"type": alert.display_type(),
"priority": alert.display_priority(),
"enabled": alert.display_enabled(),
"status": alert.status,
"created_time": alert.created_time,
"updated_time": alert.updated_time,
"last_triggered_time": alert.last_triggered_time,
});
if include_profile {
if let Value::Object(ref mut m) = v {
m.insert("profile".to_string(), Value::String(profile.to_string()));
}
}
v
}
pub async fn run_list(
targets: &[Arc<ExecutionTarget>],
name_filter: Option<&str>,
output: OutputFormat,
) -> Result<()> {
eprintln!("{}", "Fetching alerts...".dimmed());
let include_profile = targets.len() > 1;
let per_profile = fan_out(targets, |t| async move {
let api = AlertsApi::new(&t.client);
Ok(api.list().await?)
})
.await;
let mut all_json: Vec<Value> = Vec::new();
let mut all_items: Vec<(String, AlertDef)> = Vec::new();
for (profile, result) in per_profile {
match result {
Ok(resp) => {
for alert in resp.alert_defs {
if let Some(filter) = name_filter {
let name = alert.display_name().to_lowercase();
if !name.contains(&filter.to_lowercase()) {
continue;
}
}
all_json.push(alert_to_json(&alert, include_profile, &profile));
all_items.push((profile.clone(), alert));
}
}
Err(e) => eprintln!("{}", format!("error from profile '{profile}': {e:#}").red()),
}
}
match output {
OutputFormat::Json => render::render_json(&all_json)?,
OutputFormat::Agents => {
let toon =
toon_encode(&all_json).map_err(|e| anyhow::anyhow!("TOON encoding failed: {e}"))?;
println!("{toon}");
}
OutputFormat::Text => {
if all_items.is_empty() {
render::print_no_results("No alerts found.");
return Ok(());
}
let rows: Vec<Vec<String>> = all_items
.iter()
.map(|(profile, alert)| {
vec![
profile.clone(),
alert.id.clone().unwrap_or_default(),
alert.display_name(),
alert.display_type(),
alert.display_priority(),
render::bool_display(alert.display_enabled()),
alert.status.clone().unwrap_or_default(),
alert.updated_time.clone().unwrap_or_default(),
]
})
.collect();
render::render_table(
&[
"ID", "Name", "Type", "Priority", "Enabled", "Status", "Updated",
],
rows,
include_profile,
);
}
}
Ok(())
}
pub async fn run_get(
targets: &[Arc<ExecutionTarget>],
alert_id: &str,
output: OutputFormat,
) -> Result<()> {
eprintln!("{}", format!("Fetching alert {alert_id}...").dimmed());
let include_profile = targets.len() > 1;
let id = alert_id.to_string();
let per_profile = fan_out(targets, |t| {
let id = id.clone();
async move {
let api = AlertsApi::new(&t.client);
match api.get(&id).await {
Ok(val) => Ok(val),
Err(CxError::Api { status: 404, .. }) => Ok(api.get_by_version_id(&id).await?),
Err(e) => Err(anyhow::Error::from(e)),
}
}
})
.await;
let mut all_results: Vec<Value> = Vec::new();
for (profile, result) in per_profile {
match result {
Ok(mut val) => {
if include_profile {
render::tag_get_result(&mut val, &profile);
}
all_results.push(val);
}
Err(e) => eprintln!("{}", format!("error from profile '{profile}': {e:#}").red()),
}
}
match output {
OutputFormat::Json => render::render_json_auto(&all_results)?,
OutputFormat::Agents => {
let toon = toon_encode(&all_results)
.map_err(|e| anyhow::anyhow!("TOON encoding failed: {e}"))?;
println!("{toon}");
}
OutputFormat::Text => {
render::render_get_text(
&all_results,
include_profile,
"Alert not found.",
Some(&|val| {
if let Some(alert_def) = val.get("alertDef") {
if let Ok(alert) = serde_json::from_value::<AlertDef>(alert_def.clone()) {
println!("{}: {}", "Name".bold(), alert.display_name());
println!(
"{}: {}",
"ID".bold(),
alert.id.as_deref().unwrap_or("-")
);
println!("{}: {}", "Type".bold(), alert.display_type());
println!("{}: {}", "Priority".bold(), alert.display_priority());
println!(
"{}: {}",
"Enabled".bold(),
render::bool_display(alert.display_enabled())
);
println!(
"{}: {}",
"Status".bold(),
alert.status.as_deref().unwrap_or("-")
);
let desc = alert.display_description();
if !desc.is_empty() {
println!("{}: {}", "Description".bold(), desc);
}
}
}
}),
)?;
}
}
Ok(())
}
pub async fn run_create(
targets: &[Arc<ExecutionTarget>],
from_file: &str,
output: OutputFormat,
) -> Result<()> {
let raw = if from_file == "-" {
eprintln!("{}", "Reading alert definition from stdin...".dimmed());
use std::io::Read;
let mut buf = String::new();
std::io::stdin().read_to_string(&mut buf)?;
buf
} else {
eprintln!(
"{}",
format!("Reading alert definition from {from_file}...").dimmed()
);
std::fs::read_to_string(from_file)?
};
let body: Value = serde_json::from_str(&raw)?;
if body.get("alertDefProperties").is_none() {
bail!("JSON must contain an 'alertDefProperties' key. See `cx alerts create --help`.");
}
eprintln!("{}", "Creating alert...".dimmed());
let include_profile = targets.len() > 1;
let per_profile = fan_out(targets, |t| {
let body = body.clone();
async move {
let api = AlertsApi::new(&t.client);
Ok(api.create(&body).await?)
}
})
.await;
let mut all_results: Vec<Value> = Vec::new();
for (profile, result) in per_profile {
match result {
Ok(resp) => {
if let Some(alert) = resp.alert_def {
let name = alert.display_name();
let id = alert.id.as_deref().unwrap_or("unknown");
eprintln!(
"{}",
format!("Created alert '{name}' (ID: {id}) in profile '{profile}'.")
.green()
);
let json = alert_to_json(&alert, include_profile, &profile);
all_results.push(json);
} else {
eprintln!(
"{}",
format!("Alert created in profile '{profile}' but response was empty.")
.yellow()
);
}
}
Err(e) => eprintln!("{}", format!("error from profile '{profile}': {e:#}").red()),
}
}
match output {
OutputFormat::Json => render::render_json_auto(&all_results)?,
OutputFormat::Agents => {
let toon = toon_encode(&all_results)
.map_err(|e| anyhow::anyhow!("TOON encoding failed: {e}"))?;
println!("{toon}");
}
OutputFormat::Text => {
}
}
Ok(())
}
pub async fn run_enable(targets: &[Arc<ExecutionTarget>], alert_id: &str) -> Result<()> {
eprintln!("{}", format!("Enabling alert {alert_id}...").dimmed());
let id = alert_id.to_string();
let per_profile = fan_out(targets, |t| {
let id = id.clone();
async move {
let api = AlertsApi::new(&t.client);
api.set_active(&id, true).await?;
Ok(())
}
})
.await;
for (profile, result) in per_profile {
match result {
Ok(()) => eprintln!(
"{}",
format!("Alert {alert_id} enabled in profile '{profile}'.").green()
),
Err(e) => eprintln!("{}", format!("error from profile '{profile}': {e:#}").red()),
}
}
Ok(())
}
pub async fn run_disable(targets: &[Arc<ExecutionTarget>], alert_id: &str) -> Result<()> {
eprintln!("{}", format!("Disabling alert {alert_id}...").dimmed());
let id = alert_id.to_string();
let per_profile = fan_out(targets, |t| {
let id = id.clone();
async move {
let api = AlertsApi::new(&t.client);
api.set_active(&id, false).await?;
Ok(())
}
})
.await;
for (profile, result) in per_profile {
match result {
Ok(()) => eprintln!(
"{}",
format!("Alert {alert_id} disabled in profile '{profile}'.").green()
),
Err(e) => eprintln!("{}", format!("error from profile '{profile}': {e:#}").red()),
}
}
Ok(())
}