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

@@ -40,11 +40,7 @@ from app.models.ban import (
from app.models.ban import (
JailBanCount as JailBanCountModel,
)
from app.repositories import fail2ban_db_repo
from app.repositories.history_archive_repo import (
get_all_archived_history,
get_archived_history,
)
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
from app.utils.fail2ban_client import (
@@ -57,6 +53,7 @@ if TYPE_CHECKING:
import aiosqlite
from app.models.geo import GeoBatchLookup, GeoCacheLookup, GeoEnricher, GeoInfo
from app.repositories.protocols import HistoryArchiveRepository
log: structlog.stdlib.BoundLogger = structlog.get_logger()
@@ -432,6 +429,7 @@ async def list_bans(
app_db: aiosqlite.Connection | None = None,
geo_batch_lookup: GeoBatchLookup | None = None,
geo_enricher: GeoEnricher | None = None,
history_archive_repo: HistoryArchiveRepository = default_history_archive_repo,
origin: BanOrigin | None = None,
) -> DashboardBanListResponse:
"""Return a paginated list of bans within the selected time window.
@@ -482,7 +480,7 @@ async def list_bans(
if app_db is None:
raise ValueError("app_db must be provided when source is 'archive'")
rows, total = await get_archived_history(
rows, total = await history_archive_repo.get_archived_history(
db=app_db,
since=since,
origin=origin,
@@ -599,6 +597,7 @@ async def bans_by_country(
geo_cache_lookup: GeoCacheLookup | None = None,
geo_batch_lookup: GeoBatchLookup | None = None,
geo_enricher: GeoEnricher | None = None,
history_archive_repo: HistoryArchiveRepository = default_history_archive_repo,
app_db: aiosqlite.Connection | None = None,
origin: BanOrigin | None = None,
country_code: str | None = None,
@@ -648,7 +647,7 @@ async def bans_by_country(
if app_db is None:
raise ValueError("app_db must be provided when source is 'archive'")
all_rows = await get_all_archived_history(
all_rows = await history_archive_repo.get_all_archived_history(
db=app_db,
since=since,
origin=origin,
@@ -726,7 +725,7 @@ async def bans_by_country(
companion_rows: list[dict[str, object] | fail2ban_db_repo.BanRecord]
if country_code is None:
if source == "archive":
companion_rows, _ = await get_archived_history(
companion_rows, _ = await history_archive_repo.get_archived_history(
db=app_db,
since=since,
origin=origin,
@@ -751,7 +750,7 @@ async def bans_by_country(
if source == "archive":
if matched_ips:
companion_rows = await get_all_archived_history(
companion_rows = await history_archive_repo.get_all_archived_history(
db=app_db,
since=since,
origin=origin,
@@ -859,6 +858,7 @@ async def ban_trend(
range_: TimeRange,
*,
source: str = "fail2ban",
history_archive_repo: HistoryArchiveRepository = default_history_archive_repo,
app_db: aiosqlite.Connection | None = None,
origin: BanOrigin | None = None,
) -> BanTrendResponse:
@@ -899,7 +899,7 @@ async def ban_trend(
if app_db is None:
raise ValueError("app_db must be provided when source is 'archive'")
all_rows = await get_all_archived_history(
all_rows = await history_archive_repo.get_all_archived_history(
db=app_db,
since=since,
origin=origin,
@@ -966,6 +966,7 @@ async def bans_by_jail(
range_: TimeRange,
*,
source: str = "fail2ban",
history_archive_repo: HistoryArchiveRepository = default_history_archive_repo,
app_db: aiosqlite.Connection | None = None,
origin: BanOrigin | None = None,
) -> BansByJailResponse:
@@ -996,7 +997,7 @@ async def bans_by_jail(
if app_db is None:
raise ValueError("app_db must be provided when source is 'archive'")
all_rows = await get_all_archived_history(
all_rows = await history_archive_repo.get_all_archived_history(
db=app_db,
since=since,
origin=origin,