Skip to main content

ironflow_cli/commands/
stats.rs

1//! Stats commands: aggregate and historical.
2
3use anyhow::Result;
4use clap::{Args, Subcommand};
5use ironflow_sdk::IronflowClient;
6
7use crate::output;
8
9/// Stats subcommand arguments.
10///
11/// # Examples
12///
13/// ```
14/// use ironflow_cli::commands::stats::StatsArgs;
15/// ```
16#[derive(Debug, Args)]
17pub struct StatsArgs {
18    /// Stats subcommand. Omit for aggregate stats.
19    #[command(subcommand)]
20    pub command: Option<StatsCommands>,
21}
22
23/// Available stats subcommands.
24///
25/// # Examples
26///
27/// ```
28/// use ironflow_cli::commands::stats::StatsCommands;
29/// ```
30#[derive(Debug, Subcommand)]
31pub enum StatsCommands {
32    /// Show time-bucketed historical statistics.
33    History {
34        /// Filter by workflow name.
35        #[arg(long)]
36        workflow: Option<String>,
37        /// Time period: 24h, 7d, 30d, 90d. Defaults to 7d.
38        #[arg(long, default_value = "7d")]
39        period: String,
40        /// Bucket granularity: 1h, 1d, 1w. Auto-derived from period when omitted.
41        #[arg(long)]
42        granularity: Option<String>,
43    },
44}
45
46/// Execute the `stats` command tree.
47///
48/// # Errors
49///
50/// Returns an error on API failure.
51pub async fn execute(client: &IronflowClient, args: &StatsArgs, json_mode: bool) -> Result<()> {
52    match &args.command {
53        None => {
54            let response = client.get_stats().await?;
55            output::print_output(json_mode, &response, || output::stats_table(&response.data))?;
56        }
57        Some(StatsCommands::History {
58            workflow,
59            period,
60            granularity,
61        }) => {
62            let response = client
63                .stats_history(
64                    workflow.as_deref(),
65                    Some(period.as_str()),
66                    granularity.as_deref(),
67                )
68                .await?;
69            output::print_output(json_mode, &response, || {
70                output::stats_history_table(&response.data)
71            })?;
72        }
73    }
74    Ok(())
75}