Add settings and history archive repository protocols and DI support

This commit is contained in:
2026-04-17 20:54:08 +02:00
parent 7055971163
commit db5b4cb77e
8 changed files with 137 additions and 27 deletions

View File

@@ -23,14 +23,14 @@ if TYPE_CHECKING:
import aiohttp
from app.models.geo import GeoEnricher, GeoInfo
from app.repositories.protocols import HistoryArchiveRepository
from app.models.history import (
HistoryBanItem,
HistoryListResponse,
IpDetailResponse,
IpTimelineEvent,
)
from app.repositories import fail2ban_db_repo
from app.repositories.history_archive_repo import archive_ban_event, get_max_timeofban
from app.repositories import fail2ban_db_repo, history_archive_repo as default_history_archive_repo
from app.services.fail2ban_metadata_service import default_fail2ban_metadata_service
from app.utils.fail2ban_db_utils import parse_data_json, ts_to_iso
@@ -88,14 +88,18 @@ _HISTORY_SYNC_PAGE_SIZE: int = 500
_HISTORY_SYNC_BACKFILL_WINDOW: int = 648000
async def _get_last_archive_ts(db: aiosqlite.Connection) -> int | None:
async def _get_last_archive_ts(
db: aiosqlite.Connection,
history_archive_repo: HistoryArchiveRepository = default_history_archive_repo,
) -> int | None:
"""Return the most recent archived ban timestamp, or ``None`` if empty."""
return await get_max_timeofban(db)
return await history_archive_repo.get_max_timeofban(db)
async def sync_from_fail2ban_db(
db: aiosqlite.Connection,
socket_path: str,
history_archive_repo: HistoryArchiveRepository = default_history_archive_repo,
) -> int:
"""Copy new records from the fail2ban DB into the BanGUI archive table.
@@ -106,7 +110,7 @@ async def sync_from_fail2ban_db(
Returns:
Number of fail2ban records scanned and archived.
"""
last_ts = await _get_last_archive_ts(db)
last_ts = await _get_last_archive_ts(db, history_archive_repo=history_archive_repo)
now_ts = int(datetime.now(tz=UTC).timestamp())
if last_ts is None:
@@ -129,7 +133,7 @@ async def sync_from_fail2ban_db(
break
for row in rows:
await archive_ban_event(
await history_archive_repo.archive_ban_event(
db=db,
jail=row.jail,
ip=row.ip,
@@ -167,6 +171,7 @@ async def list_history(
http_session: "aiohttp.ClientSession" | None = None,
geo_enricher: GeoEnricher | None = None,
db: aiosqlite.Connection | None = None,
history_archive_repo: HistoryArchiveRepository = default_history_archive_repo,
) -> HistoryListResponse:
"""Return a paginated list of historical ban records with optional filters.
@@ -214,9 +219,7 @@ async def list_history(
if db is None:
raise ValueError("db must be provided when source is 'archive'")
from app.repositories.history_archive_repo import get_archived_history
archived_rows, total = await get_archived_history(
archived_rows, total = await history_archive_repo.get_archived_history(
db=db,
since=since,
jail=jail,