From: Michael Tremer Date: Wed, 31 Dec 2025 14:37:57 +0000 (+0000) Subject: lists: Allow to go backwards in history X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=11757bab82cab97da1eaf9893d4579ff37ecf396;p=dbl.git lists: Allow to go backwards in history Signed-off-by: Michael Tremer --- diff --git a/src/dnsbl/api/lists.py b/src/dnsbl/api/lists.py index b33319f..a2231a7 100644 --- a/src/dnsbl/api/lists.py +++ b/src/dnsbl/api/lists.py @@ -18,6 +18,7 @@ # # ############################################################################### +import datetime import fastapi import pydantic import typing @@ -57,15 +58,17 @@ def get_list(list = fastapi.Depends(get_list_from_path)) -> lists.List: @router.get("/{list}/history") def get_list_history( list = fastapi.Depends(get_list_from_path), + before: datetime.datetime = None, limit: int = 10, ): # Return the history as a dictionary - return { - e.ts : { + return [ + { + "ts" : e.ts, "domains_added" : e.domains_added, "domains_removed" : e.domains_removed, - } for e in list.get_history(limit=limit) - } + } for e in list.get_history(before=before, limit=limit) + ] @router.get("/{list}/sources") def get_list_sources(list = fastapi.Depends(get_list_from_path)) -> typing.List[sources.Source]: diff --git a/src/dnsbl/lists.py b/src/dnsbl/lists.py index c8635c7..f8584cf 100644 --- a/src/dnsbl/lists.py +++ b/src/dnsbl/lists.py @@ -414,7 +414,7 @@ class List(sqlmodel.SQLModel, database.BackendMixin, table=True): # History - def get_history(self, limit=None): + def get_history(self, before=None, limit=None): """ Fetches the history of the list """ @@ -442,7 +442,6 @@ class List(sqlmodel.SQLModel, database.BackendMixin, table=True): domains.Domain.added_at, ) .limit(limit) - .cte("domains_added") ) # Collect all domains that have been removed @@ -470,9 +469,22 @@ class List(sqlmodel.SQLModel, database.BackendMixin, table=True): domains.Domain.removed_at, ) .limit(limit) - .cte("domains_removed") ) + # Filter if we want data before a certain point in time + if before: + domains_added = domains_added.where( + domains.Domain.added_at < before, + ) + + domains_removed = domains_removed.where( + domains.Domain.removed_at < before, + ) + + # Create CTEs + domains_added = domains_added.cte("domains_added") + domains_removed = domains_removed.cte("domains_removed") + # Union timestamps timestamps = ( sqlmodel