Skip to main content

systemprompt_analytics/repository/fingerprint/
mod.rs

1//! Browser-fingerprint reputation tracking for abuse detection.
2//!
3//! [`FingerprintRepository`] upserts and scores `fingerprint_reputation`
4//! rows — session counts, request velocity, flags, and reputation score —
5//! used to detect and ban abusive clients. Read queries live in `queries`,
6//! state changes in `mutations`; the threshold constants here define the
7//! abuse-detection policy.
8
9mod mutations;
10mod queries;
11
12use std::sync::Arc;
13
14use crate::Result;
15use sqlx::PgPool;
16use systemprompt_database::DbPool;
17
18pub const MAX_SESSIONS_PER_FINGERPRINT: i32 = 5;
19pub const HIGH_REQUEST_THRESHOLD: i64 = 100;
20pub const HIGH_VELOCITY_RPM: f32 = 10.0;
21pub const SUSTAINED_VELOCITY_MINUTES: i32 = 60;
22pub const ABUSE_THRESHOLD_FOR_BAN: i32 = 3;
23
24#[derive(Clone, Debug)]
25pub struct FingerprintRepository {
26    pool: Arc<PgPool>,
27    write_pool: Arc<PgPool>,
28}
29
30impl FingerprintRepository {
31    pub fn new(db: &DbPool) -> Result<Self> {
32        let pool = db.pool_arc()?;
33        let write_pool = db.write_pool_arc()?;
34        Ok(Self { pool, write_pool })
35    }
36}