# retoor from datetime import datetime, timedelta, timezone from molodetz.database import count_audit, get_int_setting, prune_audit, prune_expired_sessions from molodetz.services.base import BaseService class HousekeepingService(BaseService): name = "housekeeping" title = "Housekeeping" description = "Clears expired sessions and prunes the audit log to the retention period." default_interval = 3600 min_interval = 60 metrics_interval = 300 def __init__(self): super().__init__() self.last_sessions = 0 self.last_audit = 0 async def run_once(self): self.last_sessions = prune_expired_sessions() days = max(1, get_int_setting("audit_retention_days", 365)) cutoff = (datetime.now(timezone.utc) - timedelta(days=days)).isoformat() self.last_audit = prune_audit(cutoff) self.log(f"sessions removed: {self.last_sessions}, audit rows pruned: {self.last_audit}") def collect_metrics(self): return { "cards": [ {"label": "Audit rows", "value": count_audit()}, {"label": "Last session cleanup", "value": self.last_sessions}, {"label": "Last audit prune", "value": self.last_audit}, ], "table": None, }