25 lines
697 B
Python
25 lines
697 B
Python
from datetime import datetime, timedelta, timezone
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.models.log import OperationLog
|
|
|
|
|
|
class LogService:
|
|
def add(self, db: Session, message: str, commit: bool = True) -> None:
|
|
db.add(OperationLog(message=message))
|
|
if commit:
|
|
db.commit()
|
|
|
|
def delete_older_than(self, db: Session, days: int) -> int:
|
|
cutoff = datetime.now(timezone.utc).replace(tzinfo=None) - timedelta(days=days)
|
|
logs = db.query(OperationLog).filter(OperationLog.timestamp < cutoff).all()
|
|
count = len(logs)
|
|
for log in logs:
|
|
db.delete(log)
|
|
db.commit()
|
|
return count
|
|
|
|
|
|
log_service = LogService()
|