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//!
9//! Copyright (c) systemprompt.io — Business Source License 1.1.
10//! See <https://systemprompt.io> for licensing details.
11
12mod mutations;
13mod queries;
14
15use std::sync::Arc;
16
17use crate::Result;
18use sqlx::PgPool;
19use systemprompt_database::DbPool;
20
21pub const MAX_SESSIONS_PER_FINGERPRINT: i32 = 5;
22pub const HIGH_REQUEST_THRESHOLD: i64 = 100;
23pub const HIGH_VELOCITY_RPM: f32 = 10.0;
24pub const SUSTAINED_VELOCITY_MINUTES: i32 = 60;
25pub const ABUSE_THRESHOLD_FOR_BAN: i32 = 3;
26
27#[derive(Clone, Debug)]
28pub struct FingerprintRepository {
29    pool: Arc<PgPool>,
30    write_pool: Arc<PgPool>,
31}
32
33impl FingerprintRepository {
34    pub fn new(db: &DbPool) -> Result<Self> {
35        let pool = db.pool_arc()?;
36        let write_pool = db.write_pool_arc()?;
37        Ok(Self { pool, write_pool })
38    }
39}