feat: data retention cleanup task
This commit is contained in:
@@ -0,0 +1,33 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
import logging
|
||||||
|
from datetime import datetime, timedelta, timezone
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
|
from sqlalchemy import delete
|
||||||
|
|
||||||
|
from fablednetmon.models.monitors import DnsResult, PingResult
|
||||||
|
from fablednetmon.models.metrics import PluginMetric
|
||||||
|
|
||||||
|
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),
|
||||||
|
]:
|
||||||
|
result = await session.execute(
|
||||||
|
delete(model).where(ts_col < cutoff)
|
||||||
|
)
|
||||||
|
if result.rowcount:
|
||||||
|
logger.info(f"Pruned {result.rowcount} rows from {model.__tablename__}")
|
||||||
Reference in New Issue
Block a user