Skip to main content

systemprompt_analytics/repository/
mod.rs

1//! Repository layer.
2//!
3//! Typed `*Repository` structs that wrap `DbPool` and expose compile-time-
4//! verified `sqlx::query!` calls for every analytics aggregation, mutation,
5//! and lookup. Public re-exports below form the only supported entry points;
6//! internal submodules are private to the crate.
7//!
8//! Copyright (c) systemprompt.io — Business Source License 1.1.
9//! See <https://systemprompt.io> for licensing details.
10
11mod agents;
12mod cli_sessions;
13mod content_analytics;
14mod conversations;
15mod core_stats;
16mod costs;
17mod engagement;
18mod events;
19mod fingerprint;
20mod funnel;
21mod overview;
22mod queries;
23mod requests;
24mod session;
25mod tools;
26mod traffic;
27
28pub use agents::AgentAnalyticsRepository;
29pub use cli_sessions::CliSessionAnalyticsRepository;
30pub use content_analytics::ContentAnalyticsRepository;
31pub use conversations::ConversationAnalyticsRepository;
32pub use core_stats::CoreStatsRepository;
33pub use costs::CostAnalyticsRepository;
34pub use engagement::{EngagementRepository, SessionEngagementSummary};
35pub use events::{AnalyticsEventsRepository, StoredAnalyticsEvent};
36pub use fingerprint::{
37    ABUSE_THRESHOLD_FOR_BAN, FingerprintRepository, HIGH_REQUEST_THRESHOLD, HIGH_VELOCITY_RPM,
38    MAX_SESSIONS_PER_FINGERPRINT, SUSTAINED_VELOCITY_MINUTES,
39};
40pub use funnel::FunnelRepository;
41pub use overview::OverviewAnalyticsRepository;
42pub use queries::{AnalyticsQueryRepository, ProviderUsage};
43pub use requests::RequestAnalyticsRepository;
44pub use session::{
45    CreateSessionParams, SessionBehavioralData, SessionMigrationResult, SessionRecord,
46    SessionRepository,
47};
48pub use tools::ToolAnalyticsRepository;
49pub use tools::list_queries::ToolListParams;
50pub use traffic::{NavigationQuery, PageQuery, TrafficAnalyticsRepository};
51
52use crate::error::Result;
53use systemprompt_database::DbPool;
54
55/// Bundle of the analytics repositories consumed outside this crate,
56/// constructed once at a composition root and cloned by consumers.
57///
58/// `FingerprintRepository` is deliberately absent: it degrades to `None` at
59/// boot when its table is unavailable, so the composition root owns that
60/// fallibility separately.
61#[derive(Debug, Clone)]
62pub struct AnalyticsRepositories {
63    pub sessions: SessionRepository,
64    pub costs: CostAnalyticsRepository,
65    pub engagement: EngagementRepository,
66    pub events: AnalyticsEventsRepository,
67}
68
69impl AnalyticsRepositories {
70    pub fn new(db: &DbPool) -> Result<Self> {
71        Ok(Self {
72            sessions: SessionRepository::new(db)?,
73            costs: CostAnalyticsRepository::new(db)?,
74            engagement: EngagementRepository::new(db)?,
75            events: AnalyticsEventsRepository::new(db)?,
76        })
77    }
78}