Merge branch 'main' into refactor/timestamp-to-date
All checks were successful
CI / ci (pull_request) Successful in 20s

This commit is contained in:
2026-03-10 22:56:55 +00:00
4 changed files with 80 additions and 50 deletions

View File

@@ -4,6 +4,7 @@ from __future__ import annotations
import logging
import time
from datetime import datetime
from typing import TYPE_CHECKING, Any, Protocol, runtime_checkable
if TYPE_CHECKING:
@@ -12,6 +13,21 @@ if TYPE_CHECKING:
logger = logging.getLogger(__name__)
def _extract_ts(n: dict[str, Any]) -> float:
"""Return the creation timestamp of a notification as epoch seconds."""
raw = n.get("timestamp_creacion", n.get("timestampCreacion", 0))
if isinstance(raw, (int, float)):
return float(raw)
if isinstance(raw, datetime):
return raw.timestamp()
if isinstance(raw, str):
try:
return float(raw)
except ValueError:
return 0.0
return 0.0
@runtime_checkable
class NotificationBackend(Protocol):
"""Backend-agnostic interface for notification storage."""
@@ -88,13 +104,7 @@ class FirestoreNotificationBackend:
cutoff = time.time() - (self._window_hours * 3600)
def _ts(n: dict[str, Any]) -> Any:
return n.get(
"timestamp_creacion",
n.get("timestampCreacion", 0),
)
recent = [n for n in all_notifications if _ts(n) >= cutoff]
recent = [n for n in all_notifications if _extract_ts(n) >= cutoff]
if not recent:
logger.info(
@@ -104,7 +114,7 @@ class FirestoreNotificationBackend:
)
return []
recent.sort(key=_ts, reverse=True)
recent.sort(key=_extract_ts, reverse=True)
result = recent[: self._max_to_notify]
@@ -187,13 +197,7 @@ class RedisNotificationBackend:
cutoff = time.time() - (self._window_hours * 3600)
def _ts(n: dict[str, Any]) -> Any:
return n.get(
"timestamp_creacion",
n.get("timestampCreacion", 0),
)
recent = [n for n in all_notifications if _ts(n) >= cutoff]
recent = [n for n in all_notifications if _extract_ts(n) >= cutoff]
if not recent:
logger.info(
@@ -203,7 +207,7 @@ class RedisNotificationBackend:
)
return []
recent.sort(key=_ts, reverse=True)
recent.sort(key=_extract_ts, reverse=True)
result = recent[: self._max_to_notify]