Skip to main content

systemprompt_cli/commands/analytics/
projection.rs

1//! Reporting projection operations against the primary database.
2//!
3//! Copyright (c) systemprompt.io — Business Source License 1.1.
4//! See <https://systemprompt.io> for licensing details.
5
6use anyhow::Result;
7use clap::Subcommand;
8use systemprompt_runtime::reporting;
9
10use crate::context::CommandContext;
11use crate::shared::{CommandOutput, render_result};
12
13#[derive(Debug, Clone, Copy, Subcommand)]
14pub enum ProjectionCommands {
15    #[command(about = "Show projection generation and pending delivery backlog")]
16    Status,
17    #[command(about = "Atomically rebuild reporting tables from owner snapshots")]
18    Rebuild,
19    #[command(about = "Apply up to the requested number of pending reporting facts")]
20    Sync {
21        #[arg(long, default_value_t = 10000)]
22        limit: usize,
23    },
24}
25
26pub async fn execute(command: ProjectionCommands, ctx: &CommandContext) -> Result<()> {
27    let database = ctx.database().await?;
28    let pool = database.db_pool();
29    match command {
30        ProjectionCommands::Status => {},
31        ProjectionCommands::Rebuild => reporting::rebuild(pool).await?,
32        ProjectionCommands::Sync { limit } => {
33            reporting::process_pending(pool, limit).await?;
34        },
35    }
36    let status = reporting::status(pool).await?;
37    render_result(
38        &CommandOutput::card_value("Reporting projection", &status),
39        &ctx.cli,
40    );
41    Ok(())
42}