pub mod api;
use std::sync::Arc;
use anyhow::Result;
use colored::Colorize;
use serde_json::Value;
use toon_format::encode_default as toon_encode;
use crate::config::OutputFormat;
use crate::execution::{fan_out, ExecutionTarget};
use crate::render;
use api::DataUsageApi;
pub async fn run_summary(
targets: &[Arc<ExecutionTarget>],
start: Option<&str>,
end: Option<&str>,
output: OutputFormat,
) -> Result<()> {
eprintln!("{}", "Fetching data usage summary...".dimmed());
let include_profile = targets.len() > 1;
let from = match start.map(crate::time::parse_timestamp).transpose()? {
Some(s) => s,
None => chrono::Utc::now()
.checked_sub_signed(chrono::Duration::hours(24))
.unwrap_or_else(chrono::Utc::now)
.to_rfc3339(),
};
let to = match end.map(crate::time::parse_timestamp).transpose()? {
Some(s) => s,
None => chrono::Utc::now().to_rfc3339(),
};
let per_profile = fan_out(targets, |t| {
let from = from.clone();
let to = to.clone();
async move {
let api = DataUsageApi::new(&t.client);
Ok(api
.get_usage(&[
("date_range.fromDate", &from),
("date_range.toDate", &to),
("resolution", "1h"),
])
.await?)
}
})
.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 => {
if all_results.is_empty() {
render::print_no_results("No data usage found.");
return Ok(());
}
for val in &all_results {
println!("{}", serde_json::to_string_pretty(val)?);
}
}
}
Ok(())
}
pub async fn run_daily(
targets: &[Arc<ExecutionTarget>],
data_type: &str,
start: Option<&str>,
end: Option<&str>,
output: OutputFormat,
) -> Result<()> {
eprintln!(
"{}",
format!("Fetching daily {data_type} usage...").dimmed()
);
let include_profile = targets.len() > 1;
let data_type = data_type.to_string();
let start = start.map(crate::time::parse_timestamp).transpose()?;
let end = end.map(crate::time::parse_timestamp).transpose()?;
let per_profile = fan_out(targets, |t| {
let data_type = data_type.clone();
let start = start.clone();
let end = end.clone();
async move {
let api = DataUsageApi::new(&t.client);
let mut body = serde_json::Map::new();
let mut date_range = serde_json::Map::new();
if let Some(ref s) = start {
date_range.insert("fromDate".into(), Value::String(s.clone()));
}
if let Some(ref e) = end {
date_range.insert("toDate".into(), Value::String(e.clone()));
}
if !date_range.is_empty() {
body.insert("date_range".into(), Value::Object(date_range));
}
Ok(api.daily(&data_type, &Value::Object(body)).await?)
}
})
.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 => {
if all_results.is_empty() {
render::print_no_results("No daily usage data found.");
return Ok(());
}
for val in &all_results {
println!("{}", serde_json::to_string_pretty(val)?);
}
}
}
Ok(())
}
pub async fn run_logs_count(targets: &[Arc<ExecutionTarget>], output: OutputFormat) -> Result<()> {
eprintln!("{}", "Fetching logs count...".dimmed());
let include_profile = targets.len() > 1;
let per_profile = fan_out(targets, |t| async move {
let api = DataUsageApi::new(&t.client);
Ok(api.logs_count().await?)
})
.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 => {
for val in &all_results {
println!("{}", serde_json::to_string_pretty(val)?);
}
}
}
Ok(())
}
pub async fn run_spans_count(targets: &[Arc<ExecutionTarget>], output: OutputFormat) -> Result<()> {
eprintln!("{}", "Fetching spans count...".dimmed());
let include_profile = targets.len() > 1;
let per_profile = fan_out(targets, |t| async move {
let api = DataUsageApi::new(&t.client);
Ok(api.spans_count().await?)
})
.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 => {
for val in &all_results {
println!("{}", serde_json::to_string_pretty(val)?);
}
}
}
Ok(())
}
pub async fn run_export_status(
targets: &[Arc<ExecutionTarget>],
output: OutputFormat,
) -> Result<()> {
eprintln!("{}", "Fetching export status...".dimmed());
let include_profile = targets.len() > 1;
let per_profile = fan_out(targets, |t| async move {
let api = DataUsageApi::new(&t.client);
Ok(api.export_status().await?)
})
.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 => {
for val in &all_results {
println!("{}", serde_json::to_string_pretty(val)?);
}
}
}
Ok(())
}