94a35da86e
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
36 lines
1.3 KiB
Python
36 lines
1.3 KiB
Python
from __future__ import annotations
|
|
import logging
|
|
from datetime import datetime, timedelta, timezone
|
|
from typing import TYPE_CHECKING
|
|
|
|
from sqlalchemy import delete
|
|
|
|
from roundtable.models.monitors import DnsResult, PingResult
|
|
from roundtable.models.metrics import PluginMetric
|
|
from roundtable.models.ansible import AnsibleRun
|
|
|
|
if TYPE_CHECKING:
|
|
from quart import Quart
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
async def run_cleanup(app: "Quart") -> None:
|
|
"""Delete rows older than DATA_RETENTION_DAYS from time-series tables."""
|
|
retention_days: int = app.config.get("DATA_RETENTION_DAYS", 90)
|
|
cutoff = datetime.now(timezone.utc) - timedelta(days=retention_days)
|
|
|
|
async with app.db_sessionmaker() as session:
|
|
async with session.begin():
|
|
for model, ts_col in [
|
|
(PingResult, PingResult.probed_at),
|
|
(DnsResult, DnsResult.resolved_at),
|
|
(PluginMetric, PluginMetric.recorded_at),
|
|
(AnsibleRun, AnsibleRun.started_at),
|
|
]:
|
|
result = await session.execute(
|
|
delete(model).where(ts_col < cutoff)
|
|
)
|
|
if result.rowcount:
|
|
logger.info(f"Pruned {result.rowcount} rows from {model.__tablename__}")
|