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"),
)
.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(),
# 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,
)
)
# 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
sqlmodel
.union(
sqlmodel.select(
- domains_added.c.ts,
+ domains_blocked.c.ts,
+ ),
+ sqlmodel.select(
+ domains_allowed.c.ts,
),
sqlmodel.select(
domains_removed.c.ts,
.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(
).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,