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 costs;
16mod engagement;
17mod events;
18mod fingerprint;
19mod overview;
20mod requests;
21mod session;
22mod tools;
23mod traffic;
24
25pub use agents::AgentAnalyticsRepository;
26pub use cli_sessions::CliSessionAnalyticsRepository;
27pub use content_analytics::ContentAnalyticsRepository;
28pub use conversations::ConversationAnalyticsRepository;
29pub use costs::CostAnalyticsRepository;
30pub use engagement::EngagementRepository;
31pub use events::AnalyticsEventsRepository;
32pub use fingerprint::{
33    ABUSE_THRESHOLD_FOR_BAN, FingerprintRepository, HIGH_REQUEST_THRESHOLD, HIGH_VELOCITY_RPM,
34    MAX_SESSIONS_PER_FINGERPRINT, SUSTAINED_VELOCITY_MINUTES,
35};
36pub use overview::OverviewAnalyticsRepository;
37pub use requests::RequestAnalyticsRepository;
38pub use session::{
39    CreateSessionParams, SessionBehavioralData, SessionMigrationResult, SessionRecord,
40    SessionRepository,
41};
42pub use tools::ToolAnalyticsRepository;
43pub use tools::list_queries::ToolListParams;
44pub use traffic::{NavigationQuery, PageQuery, TrafficAnalyticsRepository};
45
46use crate::error::Result;
47use systemprompt_database::DbPool;
48
49#[derive(Debug, Clone)]
50pub struct AnalyticsRepositories {
51    pub sessions: SessionRepository,
52    pub costs: CostAnalyticsRepository,
53    pub engagement: EngagementRepository,
54    pub events: AnalyticsEventsRepository,
55}
56
57impl AnalyticsRepositories {
58    pub fn new(
59        db: &DbPool,
60        sessions: systemprompt_traits::DynSessionStore,
61        event_sink: systemprompt_traits::DynAnalyticsEventStore,
62        content: systemprompt_traits::DynContentCatalogStats,
63    ) -> Result<Self> {
64        Ok(Self {
65            sessions: SessionRepository::new(
66                db,
67                sessions,
68                std::sync::Arc::clone(&event_sink),
69                content,
70            )?,
71            costs: CostAnalyticsRepository::new(db)?,
72            engagement: EngagementRepository::new(db)?,
73            events: AnalyticsEventsRepository::new(event_sink),
74        })
75    }
76}