Skip to main content

systemprompt_cli/commands/analytics/
mod.rs

1//! Top-level `analytics` command group spanning all metric domains.
2//!
3//! [`AnalyticsCommands`] routes to the per-domain subcommand trees (overview,
4//! conversations, agents, tools, requests, sessions, content, traffic, costs).
5//! Each variant dispatches to its module's `execute`; the `_with_db` path
6//! reuses a shared [`DatabaseContext`] pool instead of opening a new
7//! `AppContext` per command. The [`shared`] module holds the time-range and
8//! output formatting helpers used across all domains.
9
10pub mod agents;
11pub mod content;
12pub mod conversations;
13pub mod costs;
14pub mod overview;
15pub mod requests;
16pub mod sessions;
17pub mod shared;
18pub mod tools;
19pub mod traffic;
20
21use anyhow::Result;
22use clap::Subcommand;
23use systemprompt_runtime::DatabaseContext;
24
25use crate::CliConfig;
26use crate::shared::render_result;
27
28#[derive(Debug, Subcommand)]
29pub enum AnalyticsCommands {
30    #[command(about = "Dashboard overview of all analytics")]
31    Overview(overview::OverviewArgs),
32
33    #[command(subcommand, about = "Conversation analytics")]
34    Conversations(conversations::ConversationsCommands),
35
36    #[command(subcommand, about = "Agent performance analytics")]
37    Agents(agents::AgentsCommands),
38
39    #[command(subcommand, about = "Tool usage analytics")]
40    Tools(tools::ToolsCommands),
41
42    #[command(subcommand, about = "AI request analytics")]
43    Requests(requests::RequestsCommands),
44
45    #[command(subcommand, about = "Session analytics")]
46    Sessions(sessions::SessionsCommands),
47
48    #[command(subcommand, about = "Content performance analytics")]
49    Content(content::ContentCommands),
50
51    #[command(subcommand, about = "Traffic analytics")]
52    Traffic(traffic::TrafficCommands),
53
54    #[command(subcommand, about = "Cost analytics")]
55    Costs(costs::CostsCommands),
56}
57
58pub async fn execute(command: AnalyticsCommands, config: &CliConfig) -> Result<()> {
59    match command {
60        AnalyticsCommands::Overview(args) => {
61            let result = overview::execute(args, config).await?;
62            render_result(&result);
63            Ok(())
64        },
65        AnalyticsCommands::Conversations(cmd) => conversations::execute(cmd, config).await,
66        AnalyticsCommands::Agents(cmd) => agents::execute(cmd, config).await,
67        AnalyticsCommands::Tools(cmd) => tools::execute(cmd, config).await,
68        AnalyticsCommands::Requests(cmd) => requests::execute(cmd, config).await,
69        AnalyticsCommands::Sessions(cmd) => sessions::execute(cmd, config).await,
70        AnalyticsCommands::Content(cmd) => content::execute(cmd, config).await,
71        AnalyticsCommands::Traffic(cmd) => traffic::execute(cmd, config).await,
72        AnalyticsCommands::Costs(cmd) => costs::execute(cmd, config).await,
73    }
74}
75
76pub async fn execute_with_db(
77    command: AnalyticsCommands,
78    db_ctx: &DatabaseContext,
79    config: &CliConfig,
80) -> Result<()> {
81    match command {
82        AnalyticsCommands::Overview(args) => {
83            let result = overview::execute_with_pool(args, db_ctx, config).await?;
84            render_result(&result);
85            Ok(())
86        },
87        AnalyticsCommands::Conversations(cmd) => {
88            conversations::execute_with_pool(cmd, db_ctx, config).await
89        },
90        AnalyticsCommands::Agents(cmd) => agents::execute_with_pool(cmd, db_ctx, config).await,
91        AnalyticsCommands::Tools(cmd) => tools::execute_with_pool(cmd, db_ctx, config).await,
92        AnalyticsCommands::Requests(cmd) => requests::execute_with_pool(cmd, db_ctx, config).await,
93        AnalyticsCommands::Sessions(cmd) => sessions::execute_with_pool(cmd, db_ctx, config).await,
94        AnalyticsCommands::Content(cmd) => content::execute_with_pool(cmd, db_ctx, config).await,
95        AnalyticsCommands::Traffic(cmd) => traffic::execute_with_pool(cmd, db_ctx, config).await,
96        AnalyticsCommands::Costs(cmd) => costs::execute_with_pool(cmd, db_ctx, config).await,
97    }
98}