Files
bvandeusenandClaude Opus 5.5 9fdd67a675
CI / lint (push) Successful in 2s
CI / integration (push) Successful in 47s
CI / unit (push) Successful in 1m10s
CI / publish (push) Successful in 55s
feat(retention): every growing table is pruned; the audit log has its own window; charts read the raw window from Settings (#5155)
- prune_by_age(column) registers a time-series column with the hourly cleanup;
  Traefik (metrics, access summaries, global stats) and UniFi (WAN, client,
  WLAN, DPI, events, alarms, speedtests) register theirs in setup().
- Alert events join monitor results and Ansible runs on data.retention_days,
  read from Settings each run; Ansible run log files older than it are deleted.
- audit.retention_days (365) keeps the audit log longer, set on Settings > General.
- metric_series.points reads metrics.retention.raw_days, so the chart's
  raw/rollup seam is where the rollup put it (RAW_DAYS removed).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 21:05:22 -04:00

45 lines
1.6 KiB
Python

"""Unit: what the hourly cleanup prunes (#5155)."""
import os
from datetime import datetime, timedelta, timezone
from steward.core import cleanup
def test_run_logs_older_than_the_window_are_pruned(tmp_path):
now = datetime.now(timezone.utc)
old, new, other = tmp_path / "old.log", tmp_path / "new.log", tmp_path / "notes.txt"
for f in (old, new, other):
f.write_text("x")
aged = (now - timedelta(days=100)).timestamp()
os.utime(old, (aged, aged))
os.utime(other, (aged, aged))
assert cleanup.prune_run_logs(str(tmp_path), now - timedelta(days=90)) == 1
assert sorted(p.name for p in tmp_path.iterdir()) == ["new.log", "notes.txt"]
def test_no_run_log_directory_prunes_nothing(tmp_path):
assert cleanup.prune_run_logs(str(tmp_path / "missing"), datetime.now(timezone.utc)) == 0
def test_core_time_series_are_on_the_data_window():
from steward.models.alerts import AlertEvent
from steward.models.ansible import AnsibleRun
from steward.models.audit import AuditEvent
from steward.models.monitors import MonitorResult
tables = {c.class_.__tablename__ for c in cleanup._AGED}
assert {MonitorResult.__tablename__, AnsibleRun.__tablename__,
AlertEvent.__tablename__} <= tables
# The audit log has its own, longer window, not the data one.
assert AuditEvent.__tablename__ not in tables
def test_a_column_registers_once(monkeypatch):
from steward.models.monitors import MonitorResult
monkeypatch.setattr(cleanup, "_AGED", [])
cleanup.prune_by_age(MonitorResult.checked_at)
cleanup.prune_by_age(MonitorResult.checked_at) # a plugin set up twice
assert len(cleanup._AGED) == 1