From: Michael Tremer Date: Wed, 31 Dec 2025 15:42:19 +0000 (+0000) Subject: lists: Split whitelisted domains on history X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=53b9c5c4e004e2427c5c7ea58c3ea3a0a8c57512;p=dbl.git lists: Split whitelisted domains on history Signed-off-by: Michael Tremer --- diff --git a/src/dnsbl/api/lists.py b/src/dnsbl/api/lists.py index a2231a7..7664314 100644 --- a/src/dnsbl/api/lists.py +++ b/src/dnsbl/api/lists.py @@ -65,7 +65,8 @@ def get_list_history( return [ { "ts" : e.ts, - "domains_added" : e.domains_added, + "domains_blocked" : e.domains_blocked, + "domains_allowed" : e.domains_allowed, "domains_removed" : e.domains_removed, } for e in list.get_history(before=before, limit=limit) ] diff --git a/src/dnsbl/lists.py b/src/dnsbl/lists.py index 328562a..99c29ec 100644 --- a/src/dnsbl/lists.py +++ b/src/dnsbl/lists.py @@ -483,7 +483,7 @@ class List(sqlmodel.SQLModel, database.BackendMixin, table=True): Fetches the history of the list """ # Collect all domains that have been added - domains_added = ( + domains_blocked = ( sqlmodel .select( domains.Domain.added_at.label("ts"), @@ -498,6 +498,34 @@ class List(sqlmodel.SQLModel, database.BackendMixin, table=True): ) .where( domains.Domain.list == self, + domains.Domain.block == True, + ) + .order_by( + domains.Domain.added_at.desc(), + ) + .group_by( + domains.Domain.added_at, + ) + .limit(limit) + ) + + # Collect all domains that have been allowed + domains_allowed = ( + sqlmodel + .select( + domains.Domain.added_at.label("ts"), + + # Aggregate all domains + sqlmodel.func.array_agg( + sqlalchemy.dialects.postgresql.aggregate_order_by( + domains.Domain.name, + domains.Domain.name.asc(), + ), + ).label("names"), + ) + .where( + domains.Domain.list == self, + domains.Domain.block == False, ) .order_by( domains.Domain.added_at.desc(), @@ -537,7 +565,11 @@ class List(sqlmodel.SQLModel, database.BackendMixin, table=True): # Filter if we want data before a certain point in time if before: - domains_added = domains_added.where( + domains_blocked = domains_blocked.where( + domains.Domain.added_at < before, + ) + + domains_allowed = domains_allowed.where( domains.Domain.added_at < before, ) @@ -546,7 +578,8 @@ class List(sqlmodel.SQLModel, database.BackendMixin, table=True): ) # Create CTEs - domains_added = domains_added.cte("domains_added") + domains_blocked = domains_blocked.cte("domains_blocked") + domains_allowed = domains_allowed.cte("domains_allowed") domains_removed = domains_removed.cte("domains_removed") # Union timestamps @@ -554,7 +587,10 @@ class List(sqlmodel.SQLModel, database.BackendMixin, table=True): sqlmodel .union( sqlmodel.select( - domains_added.c.ts, + domains_blocked.c.ts, + ), + sqlmodel.select( + domains_allowed.c.ts, ), sqlmodel.select( domains_removed.c.ts, @@ -568,11 +604,17 @@ class List(sqlmodel.SQLModel, database.BackendMixin, table=True): .select( timestamps.c.ts, - # Fetch all added domains + # Fetch all blocked domains sqlmodel.func.coalesce( - domains_added.c.names, + domains_blocked.c.names, sqlmodel.text("'{}'"), - ).label("domains_added"), + ).label("domains_blocked"), + + # Fetch all allowed domains + sqlmodel.func.coalesce( + domains_allowed.c.names, + sqlmodel.text("'{}'"), + ).label("domains_allowed"), # Fetch all removed domains sqlmodel.func.coalesce( @@ -581,8 +623,12 @@ class List(sqlmodel.SQLModel, database.BackendMixin, table=True): ).label("domains_removed"), ) .outerjoin( - domains_added, - domains_added.c.ts == timestamps.c.ts, + domains_blocked, + domains_blocked.c.ts == timestamps.c.ts, + ) + .outerjoin( + domains_allowed, + domains_allowed.c.ts == timestamps.c.ts, ) .outerjoin( domains_removed,