Skip to main content

systemprompt_analytics/repository/session/
mod.rs

1//! `SessionRepository` — repository surface over `user_sessions`. Internal
2//! submodules: `queries` (read), `mutations` (write), `behavioral` (bot
3//! detection writes), `behavioral_queries` (bot detection reads), `types`
4//! (DTO/row structs).
5
6mod behavioral;
7mod behavioral_queries;
8mod mutations;
9mod queries;
10mod types;
11
12use std::sync::Arc;
13
14use crate::Result;
15use chrono::{DateTime, Utc};
16use sqlx::PgPool;
17use systemprompt_database::DbPool;
18use systemprompt_identifiers::{SessionId, UserId};
19
20use crate::models::AnalyticsSession;
21
22pub use types::{
23    CreateSessionParams, SessionBehavioralData, SessionMigrationResult, SessionRecord,
24};
25
26#[derive(Clone, Debug)]
27pub struct SessionRepository {
28    pool: Arc<PgPool>,
29    write_pool: Arc<PgPool>,
30}
31
32impl SessionRepository {
33    pub fn new(db: &DbPool) -> Result<Self> {
34        let pool = db.pool_arc()?;
35        let write_pool = db.write_pool_arc()?;
36        Ok(Self { pool, write_pool })
37    }
38
39    pub async fn find_by_id(&self, session_id: &SessionId) -> Result<Option<AnalyticsSession>> {
40        queries::find_by_id(&self.pool, session_id).await
41    }
42
43    pub async fn find_by_fingerprint(
44        &self,
45        fingerprint_hash: &str,
46        user_id: &UserId,
47    ) -> Result<Option<AnalyticsSession>> {
48        queries::find_by_fingerprint(&self.pool, fingerprint_hash, user_id).await
49    }
50
51    pub async fn list_active_by_user(&self, user_id: &UserId) -> Result<Vec<AnalyticsSession>> {
52        queries::list_active_by_user(&self.pool, user_id).await
53    }
54
55    pub async fn update_activity(&self, session_id: &SessionId) -> Result<()> {
56        mutations::update_activity(&self.write_pool, session_id).await
57    }
58
59    pub async fn increment_request_count(&self, session_id: &SessionId) -> Result<()> {
60        mutations::increment_request_count(&self.write_pool, session_id).await
61    }
62
63    pub async fn increment_task_count(&self, session_id: &SessionId) -> Result<()> {
64        mutations::increment_task_count(&self.write_pool, session_id).await
65    }
66
67    pub async fn increment_ai_request_count(&self, session_id: &SessionId) -> Result<()> {
68        mutations::increment_ai_request_count(&self.write_pool, session_id).await
69    }
70
71    pub async fn increment_message_count(&self, session_id: &SessionId) -> Result<()> {
72        mutations::increment_message_count(&self.write_pool, session_id).await
73    }
74
75    pub async fn end_session(&self, session_id: &SessionId) -> Result<()> {
76        mutations::end_session(&self.write_pool, session_id).await
77    }
78
79    pub async fn mark_as_scanner(&self, session_id: &SessionId) -> Result<()> {
80        mutations::mark_as_scanner(&self.write_pool, session_id).await
81    }
82
83    pub async fn mark_converted(&self, session_id: &SessionId) -> Result<()> {
84        mutations::mark_converted(&self.write_pool, session_id).await
85    }
86
87    pub async fn mark_as_behavioral_bot(&self, session_id: &SessionId, reason: &str) -> Result<()> {
88        behavioral::mark_as_behavioral_bot(&self.write_pool, session_id, reason).await
89    }
90
91    pub async fn check_and_mark_behavioral_bot(
92        &self,
93        session_id: &SessionId,
94        request_count_threshold: i32,
95    ) -> Result<bool> {
96        behavioral::check_and_mark_behavioral_bot(
97            &self.write_pool,
98            session_id,
99            request_count_threshold,
100        )
101        .await
102    }
103
104    pub async fn cleanup_inactive(&self, inactive_hours: i32) -> Result<u64> {
105        mutations::cleanup_inactive(&self.write_pool, inactive_hours).await
106    }
107
108    pub async fn migrate_user_sessions(
109        &self,
110        old_user_id: &UserId,
111        new_user_id: &UserId,
112    ) -> Result<u64> {
113        mutations::migrate_user_sessions(&self.write_pool, old_user_id, new_user_id).await
114    }
115
116    pub async fn create_session(&self, params: &CreateSessionParams<'_>) -> Result<()> {
117        mutations::create_session(&self.write_pool, params).await
118    }
119
120    pub async fn find_recent_by_fingerprint(
121        &self,
122        fingerprint_hash: &str,
123        max_age_seconds: i64,
124    ) -> Result<Option<SessionRecord>> {
125        queries::find_recent_by_fingerprint(&self.pool, fingerprint_hash, max_age_seconds).await
126    }
127
128    pub async fn exists(&self, session_id: &SessionId) -> Result<bool> {
129        queries::exists(&self.pool, session_id).await
130    }
131
132    pub async fn increment_ai_usage(
133        &self,
134        session_id: &SessionId,
135        tokens: i32,
136        cost_microdollars: i64,
137    ) -> Result<()> {
138        mutations::increment_ai_usage(&self.write_pool, session_id, tokens, cost_microdollars).await
139    }
140
141    pub async fn update_behavioral_detection(
142        &self,
143        session_id: &SessionId,
144        score: i32,
145        is_behavioral_bot: bool,
146        reason: Option<&str>,
147    ) -> Result<()> {
148        behavioral::update_behavioral_detection(
149            &self.write_pool,
150            session_id,
151            score,
152            is_behavioral_bot,
153            reason,
154        )
155        .await
156    }
157
158    pub async fn escalate_throttle(&self, session_id: &SessionId, new_level: i32) -> Result<()> {
159        mutations::escalate_throttle(&self.write_pool, session_id, new_level).await
160    }
161
162    pub async fn get_throttle_level(&self, session_id: &SessionId) -> Result<i32> {
163        queries::get_throttle_level(&self.pool, session_id).await
164    }
165
166    pub async fn count_sessions_by_fingerprint(
167        &self,
168        fingerprint_hash: &str,
169        window_hours: i64,
170    ) -> Result<i64> {
171        behavioral_queries::count_sessions_by_fingerprint(
172            &self.pool,
173            fingerprint_hash,
174            window_hours,
175        )
176        .await
177    }
178
179    pub async fn get_endpoint_sequence(&self, session_id: &SessionId) -> Result<Vec<String>> {
180        behavioral_queries::get_endpoint_sequence(&self.pool, session_id).await
181    }
182
183    pub async fn get_request_timestamps(
184        &self,
185        session_id: &SessionId,
186    ) -> Result<Vec<DateTime<Utc>>> {
187        behavioral_queries::get_request_timestamps(&self.pool, session_id).await
188    }
189
190    pub async fn get_total_content_pages(&self) -> Result<i64> {
191        queries::get_total_content_pages(&self.pool).await
192    }
193
194    pub async fn get_session_for_behavioral_analysis(
195        &self,
196        session_id: &SessionId,
197    ) -> Result<Option<SessionBehavioralData>> {
198        behavioral_queries::get_session_for_behavioral_analysis(&self.pool, session_id).await
199    }
200
201    pub async fn has_analytics_events(&self, session_id: &SessionId) -> Result<bool> {
202        behavioral_queries::has_analytics_events(&self.pool, session_id).await
203    }
204
205    pub async fn count_unique_ips_by_fingerprint(
206        &self,
207        fingerprint_hash: &str,
208        window_days: i64,
209    ) -> Result<i64> {
210        behavioral_queries::count_unique_ips_by_fingerprint(
211            &self.pool,
212            fingerprint_hash,
213            window_days,
214        )
215        .await
216    }
217
218    pub async fn count_engagement_events_by_fingerprint(
219        &self,
220        fingerprint_hash: &str,
221        window_days: i64,
222    ) -> Result<i64> {
223        behavioral_queries::count_engagement_events_by_fingerprint(
224            &self.pool,
225            fingerprint_hash,
226            window_days,
227        )
228        .await
229    }
230
231    pub async fn get_session_starts_by_fingerprint(
232        &self,
233        fingerprint_hash: &str,
234        window_days: i64,
235    ) -> Result<Vec<DateTime<Utc>>> {
236        behavioral_queries::get_session_starts_by_fingerprint(
237            &self.pool,
238            fingerprint_hash,
239            window_days,
240        )
241        .await
242    }
243
244    pub async fn get_session_velocity(
245        &self,
246        session_id: &SessionId,
247    ) -> Result<(Option<i64>, Option<i64>)> {
248        behavioral_queries::get_session_velocity(&self.pool, session_id).await
249    }
250}