diff --git a/data/sightings.py b/data/sightings.py index 3c52b6e..0eb85d8 100644 --- a/data/sightings.py +++ b/data/sightings.py @@ -902,7 +902,13 @@ def update_feeder(self, feeder_id: int, name: str | None = None, def record_refill(self, feeder_id: int | None, amount_oz: float, notes: str = "", timestamp: str | None = None) -> int: """Record a feeder refill. Returns the refill ID.""" - ts = timestamp or datetime.now(tz=_local_tz).isoformat() + if timestamp: + dt = datetime.fromisoformat(timestamp) + if dt.tzinfo is None: + dt = dt.replace(tzinfo=_local_tz) + ts = dt.isoformat() + else: + ts = datetime.now(tz=_local_tz).isoformat() with self._lock: conn = self._get_conn() try: @@ -937,7 +943,13 @@ def get_refills(self, days: int = 90, limit: int = 50) -> list[dict]: def record_production(self, amount_oz: float, sugar_ratio: str = "1:4", notes: str = "", timestamp: str | None = None) -> int: """Record nectar production. Returns the production ID.""" - ts = timestamp or datetime.now(tz=_local_tz).isoformat() + if timestamp: + dt = datetime.fromisoformat(timestamp) + if dt.tzinfo is None: + dt = dt.replace(tzinfo=_local_tz) + ts = dt.isoformat() + else: + ts = datetime.now(tz=_local_tz).isoformat() with self._lock: conn = self._get_conn() try: @@ -983,6 +995,8 @@ def get_feeder_stats(self, days: int = 30) -> dict: days_since_refill = None if last_refill: lr = datetime.fromisoformat(last_refill["timestamp"]) + if lr.tzinfo is None: + lr = lr.replace(tzinfo=_local_tz) days_since_refill = (datetime.now(tz=_local_tz) - lr).days # Total nectar produced in period diff --git a/web/dashboard.py b/web/dashboard.py index 994f2a8..6a2e653 100644 --- a/web/dashboard.py +++ b/web/dashboard.py @@ -491,8 +491,12 @@ def dashboard(): ) analytics = None if _monitor and _monitor.sightings_db: - from analytics.patterns import get_analytics_summary - analytics = get_analytics_summary(_monitor.sightings_db) + try: + from analytics.patterns import get_analytics_summary + analytics = get_analytics_summary(_monitor.sightings_db) + except Exception: + logger.exception("Failed to load analytics for dashboard") + analytics = None return render_template( "dashboard.html", status=_get_status(),