systemprompt_cli/commands/core/content/analytics/
clicks.rs1use crate::cli_settings::CliConfig;
2use crate::commands::core::content::types::{ClickRow, ClicksOutput};
3use crate::context::CommandContext;
4use crate::shared::CommandOutput;
5use anyhow::Result;
6use clap::Args;
7use systemprompt_content::LinkAnalyticsService;
8use systemprompt_database::DbPool;
9use systemprompt_identifiers::LinkId;
10
11#[derive(Debug, Args)]
12pub struct ClicksArgs {
13 #[arg(help = "Link ID")]
14 pub link_id: String,
15
16 #[arg(long, default_value = "20")]
17 pub limit: i64,
18
19 #[arg(long, default_value = "0")]
20 pub offset: i64,
21}
22
23pub async fn execute(args: ClicksArgs, ctx: &CommandContext) -> Result<CommandOutput> {
24 execute_with_pool(args, &ctx.db_pool().await?, &ctx.cli).await
25}
26
27pub async fn execute_with_pool(
28 args: ClicksArgs,
29 pool: &DbPool,
30 _config: &CliConfig,
31) -> Result<CommandOutput> {
32 let service = LinkAnalyticsService::new(pool)?;
33
34 let link_id = LinkId::new(args.link_id.clone());
35 let clicks = service
36 .get_link_clicks(&link_id, Some(args.limit), Some(args.offset))
37 .await?;
38
39 let total = clicks.len() as i64;
40 let click_rows: Vec<ClickRow> = clicks
41 .into_iter()
42 .filter_map(|click| {
43 Some(ClickRow {
44 click_id: click.id.to_string(),
45 session_id: click.session_id,
46 user_id: click.user_id,
47 clicked_at: click.clicked_at?,
48 referrer_page: click.referrer_page,
49 device_type: click.device_type,
50 country: click.country,
51 is_conversion: click.is_conversion.unwrap_or(false),
52 })
53 })
54 .collect();
55
56 let output = ClicksOutput {
57 link_id,
58 clicks: click_rows,
59 total,
60 };
61
62 Ok(CommandOutput::table_of(
63 vec![
64 "click_id",
65 "session_id",
66 "clicked_at",
67 "device_type",
68 "country",
69 ],
70 &output.clicks,
71 )
72 .with_title("Link Clicks"))
73}