Skip to main content

systemprompt_logging/repository/
mod.rs

1//! Log persistence repository.
2//!
3//! [`LoggingRepository`] persists entries to the `logs` table and serves
4//! paginated reads, lookups, and age-based cleanup;
5//! [`AnalyticsRepository`] records analytics events. Read and write pools are
6//! held separately so reads never contend with the write path.
7//!
8//! Copyright (c) systemprompt.io — Business Source License 1.1.
9//! See <https://systemprompt.io> for licensing details.
10
11use 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;
22mod ownership;
23
24pub use analytics::{AnalyticsEvent, AnalyticsRepository};
25pub use ownership::LoggingOwnerReassignment;
26
27#[derive(Clone, Debug)]
28pub struct LoggingRepository {
29    pool: Arc<PgPool>,
30    write_pool: Arc<PgPool>,
31}
32
33impl LoggingRepository {
34    pub fn new(db: &DbPool) -> Self {
35        let pool = db.pool();
36        let write_pool = db.write_pool();
37        Self { pool, write_pool }
38    }
39
40    pub async fn log(&self, entry: LogEntry) -> Result<(), LoggingError> {
41        entry.validate()?;
42        operations::create_log(&self.write_pool, &entry).await
43    }
44
45    pub async fn insert_batch(&self, entries: &[LogEntry]) -> Result<(), LoggingError> {
46        operations::insert_log_batch(&self.write_pool, entries).await
47    }
48
49    pub async fn get_recent_logs(&self, limit: i64) -> Result<Vec<LogEntry>, LoggingError> {
50        operations::list_logs(&self.pool, limit).await
51    }
52
53    pub async fn get_logs_by_module_patterns(
54        &self,
55        patterns: &[String],
56        limit: i64,
57    ) -> Result<Vec<LogEntry>, LoggingError> {
58        operations::list_logs_by_module_patterns(&self.pool, patterns, limit).await
59    }
60
61    pub async fn cleanup_old_logs(&self, older_than: DateTime<Utc>) -> Result<u64, LoggingError> {
62        operations::cleanup_logs_before(&self.write_pool, older_than).await
63    }
64
65    pub async fn count_logs_before(&self, cutoff: DateTime<Utc>) -> Result<u64, LoggingError> {
66        operations::count_logs_before(&self.pool, cutoff).await
67    }
68
69    pub async fn distinct_log_user_ids(&self) -> Result<Vec<UserId>, LoggingError> {
70        operations::distinct_log_user_ids(&self.pool).await
71    }
72
73    pub async fn delete_logs_for_users(&self, user_ids: &[UserId]) -> Result<u64, LoggingError> {
74        operations::delete_logs_for_users(&self.write_pool, user_ids).await
75    }
76
77    pub async fn count_logs_for_users(&self, user_ids: &[UserId]) -> Result<u64, LoggingError> {
78        operations::count_logs_for_users(&self.pool, user_ids).await
79    }
80
81    pub async fn clear_all_logs(&self) -> Result<u64, LoggingError> {
82        operations::clear_all_logs(&self.write_pool).await
83    }
84
85    pub async fn get_logs_paginated(
86        &self,
87        filter: &LogFilter,
88    ) -> Result<(Vec<LogEntry>, i64), LoggingError> {
89        operations::list_logs_paginated(&self.pool, filter).await
90    }
91
92    pub async fn get_by_id(&self, id: &LogId) -> Result<Option<LogEntry>, LoggingError> {
93        operations::get_log(&self.pool, id).await
94    }
95
96    pub async fn update_log_entry(
97        &self,
98        id: &LogId,
99        entry: &LogEntry,
100    ) -> Result<bool, LoggingError> {
101        operations::update_log(&self.write_pool, id, entry).await
102    }
103
104    pub async fn delete_log_entry(&self, id: &LogId) -> Result<bool, LoggingError> {
105        operations::delete_log(&self.write_pool, id).await
106    }
107
108    pub async fn delete_log_entries(&self, ids: &[LogId]) -> Result<u64, LoggingError> {
109        operations::delete_logs_multiple(&self.write_pool, ids).await
110    }
111}