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#[derive(Debug, Clone)]
56pub struct AnalyticsRepositories {
57    pub sessions: SessionRepository,
58    pub costs: CostAnalyticsRepository,
59    pub engagement: EngagementRepository,
60    pub events: AnalyticsEventsRepository,
61}
62
63impl AnalyticsRepositories {
64    pub fn new(
65        db: &DbPool,
66        sessions: systemprompt_traits::DynSessionStore,
67        event_sink: systemprompt_traits::DynAnalyticsEventStore,
68        content: systemprompt_traits::DynContentCatalogStats,
69    ) -> Result<Self> {
70        Ok(Self {
71            sessions: SessionRepository::new(
72                db,
73                sessions,
74                std::sync::Arc::clone(&event_sink),
75                content,
76            )?,
77            costs: CostAnalyticsRepository::new(db)?,
78            engagement: EngagementRepository::new(db)?,
79            events: AnalyticsEventsRepository::new(db, event_sink)?,
80        })
81    }
82}