use rustango::core::SqlValue;
pub const DEFAULT_RETENTION_DAYS: i64 = 365;
pub async fn retention_days(pool: &rustango::sql::Pool) -> i64 {
match crate::site_setting::get(pool, super::SETTINGS_SCOPE).await {
Ok(Some(s)) => s
.value_json
.get("retention_days")
.and_then(serde_json::Value::as_i64)
.filter(|d| *d > 0)
.unwrap_or(DEFAULT_RETENTION_DAYS),
_ => DEFAULT_RETENTION_DAYS,
}
}
pub async fn prune(pool: &rustango::sql::Pool, days: i64) -> Result<(), rustango::sql::ExecError> {
if days <= 0 {
return Ok(());
}
let cutoff = chrono::Utc::now() - chrono::Duration::days(days);
let d = pool.dialect();
let sql = format!(
"DELETE FROM {} WHERE {} < {}",
d.quote_ident("cms_analytics_event"),
d.quote_ident("created_at"),
d.placeholder(1),
);
rustango::sql::raw_execute_pool(pool, &sql, vec![SqlValue::DateTime(cutoff)]).await?;
Ok(())
}
#[cfg(all(test, feature = "sqlite"))]
mod tests {
use super::*;
use crate::analytics::model::AnalyticsEvent;
use rustango::sql::{Auto, FetcherPool as _};
fn event(path: &str, created_at: chrono::DateTime<chrono::Utc>) -> AnalyticsEvent {
AnalyticsEvent {
id: Auto::Unset,
visitor_id: "v".into(),
session_id: "s".into(),
event_type: "pageview".into(),
path: path.into(),
referrer_host: String::new(),
country: String::new(),
device: String::new(),
browser: String::new(),
os: String::new(),
screen_w: 0,
screen_h: 0,
duration_ms: 0,
max_scroll_pct: 0,
is_bounce: false,
locale: String::new(),
created_at,
}
}
#[tokio::test]
async fn prune_deletes_right_up_to_the_cutoff() {
let pool = rustango::sql::Pool::connect("sqlite::memory:")
.await
.expect("pool");
crate::analytics::model::ensure_table(&pool).await.expect("table");
let cutoff = chrono::Utc::now() - chrono::Duration::days(1);
for (path, at) in [
("/old", cutoff - chrono::Duration::minutes(5)),
("/new", cutoff + chrono::Duration::minutes(5)),
] {
event(path, at).insert_pool(&pool).await.expect("event");
}
prune(&pool, 1).await.expect("prune");
let left: Vec<String> = AnalyticsEvent::objects()
.fetch(&pool)
.await
.expect("fetch")
.into_iter()
.map(|e| e.path)
.collect();
assert_eq!(left, ["/new"]);
}
}