Skip to main content

camel_cli/commands/
journal.rs

1//! `camel journal inspect` — offline inspection of a redb runtime journal file.
2
3use camel_core::{JournalInspectFilter, RedbRuntimeEventJournal};
4
5#[derive(clap::Args)]
6pub struct JournalInspectArgs {
7    /// Path to the `.db` journal file.
8    pub path: std::path::PathBuf,
9
10    /// Show only the last N events (default: 100).
11    #[arg(long, default_value = "100")]
12    pub limit: usize,
13
14    /// Filter to a specific route_id.
15    #[arg(long)]
16    pub route: Option<String>,
17
18    /// Output format: table or json.
19    #[arg(long, default_value = "table")]
20    pub format: OutputFormat,
21}
22
23#[derive(Clone, clap::ValueEnum)]
24pub enum OutputFormat {
25    Table,
26    Json,
27}
28
29pub async fn run_inspect(args: JournalInspectArgs) {
30    let filter = JournalInspectFilter {
31        route_id: args.route.clone(),
32        limit: args.limit,
33    };
34
35    let entries = match RedbRuntimeEventJournal::inspect(args.path.clone(), filter).await {
36        Ok(e) => e,
37        Err(err) => {
38            eprintln!("error: {err}");
39            std::process::exit(1);
40        }
41    };
42
43    match args.format {
44        OutputFormat::Table => {
45            println!(
46                "{:<8}  {:<26}  {:<24}  ROUTE_ID",
47                "SEQ", "TIMESTAMP", "EVENT"
48            );
49            println!("{}", "-".repeat(80));
50            if entries.is_empty() {
51                println!("(no events)");
52                return;
53            }
54            for entry in &entries {
55                let ts = chrono::DateTime::from_timestamp_millis(entry.timestamp_ms)
56                    .map(|dt| dt.format("%Y-%m-%dT%H:%M:%S%.3fZ").to_string())
57                    .unwrap_or_else(|| "?".to_string());
58                let (event_name, route_id) = event_parts(&entry.event);
59                println!(
60                    "{:>08}  {:<26}  {:<24}  {}",
61                    entry.seq, ts, event_name, route_id
62                );
63            }
64        }
65        OutputFormat::Json => {
66            let json = serde_json::to_string_pretty(&entries).unwrap_or_else(|e| {
67                eprintln!("error: json serialize: {e}");
68                std::process::exit(1);
69            });
70            println!("{json}");
71        }
72    }
73}
74
75fn event_parts(event: &camel_core::RuntimeEvent) -> (&'static str, &str) {
76    match event {
77        camel_core::RuntimeEvent::RouteRegistered { route_id } => ("RouteRegistered", route_id),
78        camel_core::RuntimeEvent::RouteStartRequested { route_id } => {
79            ("RouteStartRequested", route_id)
80        }
81        camel_core::RuntimeEvent::RouteStarted { route_id } => ("RouteStarted", route_id),
82        camel_core::RuntimeEvent::RouteFailed { route_id, .. } => ("RouteFailed", route_id),
83        camel_core::RuntimeEvent::RouteStopped { route_id } => ("RouteStopped", route_id),
84        camel_core::RuntimeEvent::RouteSuspended { route_id } => ("RouteSuspended", route_id),
85        camel_core::RuntimeEvent::RouteResumed { route_id } => ("RouteResumed", route_id),
86        camel_core::RuntimeEvent::RouteReloaded { route_id } => ("RouteReloaded", route_id),
87        camel_core::RuntimeEvent::RouteRemoved { route_id } => ("RouteRemoved", route_id),
88    }
89}