Skip to main content

skm_cli/commands/
export.rs

1//! Export command: export metrics in various formats.
2
3use clap::Args;
4use std::path::PathBuf;
5
6use skm_learn::SelectionMetrics;
7
8#[derive(Args)]
9pub struct ExportArgs {
10    /// Export format (prometheus, json)
11    #[arg(short, long, default_value = "prometheus")]
12    format: String,
13
14    /// Output file (defaults to stdout)
15    #[arg(short, long)]
16    output: Option<PathBuf>,
17}
18
19pub async fn export(args: ExportArgs) -> anyhow::Result<()> {
20    // Create empty metrics for demo
21    // In production, this would load from a running instance or database
22    let metrics = SelectionMetrics::new();
23
24    let output = match args.format.as_str() {
25        "prometheus" => metrics.to_prometheus(),
26        "json" => {
27            let summary = metrics.summary();
28            serde_json::to_string_pretty(&serde_json::json!({
29                "total_selections": summary.total_selections,
30                "total_timeouts": summary.total_timeouts,
31                "latency_p50_ms": summary.latency_p50,
32                "latency_p95_ms": summary.latency_p95,
33                "latency_p99_ms": summary.latency_p99,
34                "by_confidence": summary.by_confidence,
35                "by_strategy": summary.by_strategy
36            }))?
37        }
38        other => {
39            anyhow::bail!("Unknown format: {}. Use 'prometheus' or 'json'", other);
40        }
41    };
42
43    if let Some(path) = args.output {
44        std::fs::write(&path, &output)?;
45        println!("Exported to {:?}", path);
46    } else {
47        println!("{}", output);
48    }
49
50    Ok(())
51}