systemprompt-analytics 0.53.0

Analytics for systemprompt.io AI governance infrastructure. Session, agent, tool, and microdollar-precision cost attribution across the MCP governance pipeline.
Documentation
//! Browser-fingerprint reputation tracking for abuse detection.
//!
//! [`FingerprintRepository`] upserts and scores `fingerprint_reputation`
//! rows — session counts, request velocity, flags, and reputation score —
//! used to detect and ban abusive clients. Read queries live in `queries`,
//! state changes in `mutations`; the threshold constants here define the
//! abuse-detection policy.
//!
//! Copyright (c) systemprompt.io — Business Source License 1.1.
//! See <https://systemprompt.io> for licensing details.

mod mutations;
mod queries;

use std::sync::Arc;

use crate::Result;
use sqlx::PgPool;
use systemprompt_database::DbPool;

pub const MAX_SESSIONS_PER_FINGERPRINT: i32 = 5;
pub const HIGH_REQUEST_THRESHOLD: i64 = 100;
pub const HIGH_VELOCITY_RPM: f32 = 10.0;
pub const SUSTAINED_VELOCITY_MINUTES: i32 = 60;
pub const ABUSE_THRESHOLD_FOR_BAN: i32 = 3;

#[derive(Clone)]
pub struct FingerprintRepository {
    pool: Arc<PgPool>,
    write_pool: Arc<PgPool>,
    sessions: systemprompt_traits::DynSessionStore,
}

impl FingerprintRepository {
    pub fn new(db: &DbPool, sessions: systemprompt_traits::DynSessionStore) -> Result<Self> {
        let pool = db.pool_arc()?;
        let write_pool = db.write_pool_arc()?;
        Ok(Self {
            pool,
            write_pool,
            sessions,
        })
    }
}

impl std::fmt::Debug for FingerprintRepository {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("FingerprintRepository")
            .finish_non_exhaustive()
    }
}