systemprompt_logging/repository/
mod.rs1use std::sync::Arc;
12
13use chrono::{DateTime, Utc};
14use sqlx::PgPool;
15use systemprompt_database::DbPool;
16use systemprompt_identifiers::{LogId, UserId};
17
18use crate::models::{LogEntry, LogFilter, LoggingError};
19
20pub mod analytics;
21mod operations;
22
23pub use analytics::{AnalyticsEvent, AnalyticsRepository};
24
25#[derive(Clone, Debug)]
26pub struct LoggingRepository {
27 pool: Arc<PgPool>,
28 write_pool: Arc<PgPool>,
29}
30
31impl LoggingRepository {
32 pub fn new(db: &DbPool) -> Result<Self, LoggingError> {
33 let pool = db.pool_arc()?;
34 let write_pool = db.write_pool_arc()?;
35 Ok(Self { pool, write_pool })
36 }
37
38 pub async fn log(&self, entry: LogEntry) -> Result<(), LoggingError> {
39 entry.validate()?;
40 operations::create_log(&self.write_pool, &entry).await
41 }
42
43 pub async fn get_recent_logs(&self, limit: i64) -> Result<Vec<LogEntry>, LoggingError> {
44 operations::list_logs(&self.pool, limit).await
45 }
46
47 pub async fn get_logs_by_module_patterns(
48 &self,
49 patterns: &[String],
50 limit: i64,
51 ) -> Result<Vec<LogEntry>, LoggingError> {
52 operations::list_logs_by_module_patterns(&self.pool, patterns, limit).await
53 }
54
55 pub async fn cleanup_old_logs(&self, older_than: DateTime<Utc>) -> Result<u64, LoggingError> {
56 operations::cleanup_logs_before(&self.write_pool, older_than).await
57 }
58
59 pub async fn count_logs_before(&self, cutoff: DateTime<Utc>) -> Result<u64, LoggingError> {
60 operations::count_logs_before(&self.pool, cutoff).await
61 }
62
63 pub async fn distinct_log_user_ids(&self) -> Result<Vec<UserId>, LoggingError> {
64 operations::distinct_log_user_ids(&self.pool).await
65 }
66
67 pub async fn delete_logs_for_users(&self, user_ids: &[UserId]) -> Result<u64, LoggingError> {
68 operations::delete_logs_for_users(&self.write_pool, user_ids).await
69 }
70
71 pub async fn count_logs_for_users(&self, user_ids: &[UserId]) -> Result<u64, LoggingError> {
72 operations::count_logs_for_users(&self.pool, user_ids).await
73 }
74
75 pub async fn clear_all_logs(&self) -> Result<u64, LoggingError> {
76 operations::clear_all_logs(&self.write_pool).await
77 }
78
79 pub async fn get_logs_paginated(
80 &self,
81 filter: &LogFilter,
82 ) -> Result<(Vec<LogEntry>, i64), LoggingError> {
83 operations::list_logs_paginated(&self.pool, filter).await
84 }
85
86 pub async fn get_by_id(&self, id: &LogId) -> Result<Option<LogEntry>, LoggingError> {
87 operations::get_log(&self.pool, id).await
88 }
89
90 pub async fn update_log_entry(
91 &self,
92 id: &LogId,
93 entry: &LogEntry,
94 ) -> Result<bool, LoggingError> {
95 operations::update_log(&self.write_pool, id, entry).await
96 }
97
98 pub async fn delete_log_entry(&self, id: &LogId) -> Result<bool, LoggingError> {
99 operations::delete_log(&self.write_pool, id).await
100 }
101
102 pub async fn delete_log_entries(&self, ids: &[LogId]) -> Result<u64, LoggingError> {
103 operations::delete_logs_multiple(&self.write_pool, ids).await
104 }
105}