diff options
| author | Christian Cleberg <[email protected]> | 2026-04-11 18:54:01 -0500 |
|---|---|---|
| committer | Christian Cleberg <[email protected]> | 2026-04-11 18:54:01 -0500 |
| commit | 90e61ed3e8e765ff5557bc59a6a7b141124dba01 (patch) | |
| tree | 8d76a215116c7f8ae9c31170c03226af6bccf225 /src/srht_contrib/utils/retention.py | |
| parent | 01c1b50541b0dc1d42cbdaa90052f6b94ceba20c (diff) | |
| download | hutch-stats-90e61ed3e8e765ff5557bc59a6a7b141124dba01.tar.gz hutch-stats-90e61ed3e8e765ff5557bc59a6a7b141124dba01.tar.bz2 hutch-stats-90e61ed3e8e765ff5557bc59a6a7b141124dba01.zip | |
refactor: retain and backfill only the last year of activity
Diffstat (limited to 'src/srht_contrib/utils/retention.py')
| -rw-r--r-- | src/srht_contrib/utils/retention.py | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/src/srht_contrib/utils/retention.py b/src/srht_contrib/utils/retention.py new file mode 100644 index 0000000..5018b32 --- /dev/null +++ b/src/srht_contrib/utils/retention.py @@ -0,0 +1,17 @@ +from __future__ import annotations + +from datetime import UTC, datetime, timedelta + +from sqlalchemy import delete +from sqlalchemy.orm import Session + +from srht_contrib.models import ContributionEvent + + +RETENTION_DAYS = 365 + + +def prune_contribution_events(db: Session, *, now: datetime | None = None) -> int: + cutoff = (now or datetime.now(tz=UTC)) - timedelta(days=RETENTION_DAYS) + result = db.execute(delete(ContributionEvent).where(ContributionEvent.occurred_at < cutoff)) + return int(result.rowcount or 0) |
