fix: interpret naive detected_at as UTC when rendering alert timestamps#2305
fix: interpret naive detected_at as UTC when rendering alert timestamps#2305hiromi-dev wants to merge 1 commit into
Conversation
detected_at values loaded from the alerts table are naive datetimes stored in UTC. Passing them directly to datetime.astimezone() interprets them as local time, so on non-UTC hosts the alert "Time:" field showed the UTC value labeled with the local timezone, inconsistent with max_loaded_at in the same message which is correctly converted via convert_datetime_utc_str_to_timezone_str(). Attach UTC tzinfo to naive detected_at before converting; detected_at_utc is now always timezone-aware UTC. Timezone-aware inputs behave as before. Fixes elementary-data#2304 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
👋 @hiromi-dev |
📝 WalkthroughWalkthroughChangesAlert timezone handling
Estimated code review effort: 2 (Simple) | ~10 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@elementary/monitor/alerts/alert.py`:
- Around line 61-67: Update the group timestamp calculation in the logic
containing min(alert.detected_at or datetime.max ...) so missing timestamps use
a timezone-aware UTC fallback, or are excluded before computing min, preventing
comparisons between naive and aware datetimes. Preserve the existing behavior
for present timestamps and add a regression test covering a group with one
normalized alert and one missing timestamp.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: 20eeecac-516e-414b-9322-6620b4b0cbae
📒 Files selected for processing (2)
elementary/monitor/alerts/alert.pytests/unit/monitor/alerts/test_alert_models.py
| # detected_at is stored in UTC, so a naive datetime must be | ||
| # interpreted as UTC rather than as the machine's local time. | ||
| if detected_at.tzinfo is None: | ||
| detected_at = detected_at.replace(tzinfo=tz.tzutc()) | ||
| self.detected_at_utc = detected_at.astimezone(tz.tzutc()) | ||
| self.detected_at = detected_at.astimezone( | ||
| tz.gettz(timezone) if timezone else tz.tzlocal() |
There was a problem hiding this comment.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
Avoid mixing normalized timestamps with the group’s naive fallback.
After this change, a naive input produces an aware self.detected_at, but base_alerts_group.py:22-23 still evaluates min(alert.detected_at or datetime.max ...). A group containing one normalized alert and one missing timestamp now raises TypeError: can't compare offset-naive and offset-aware datetimes. Make that fallback timezone-aware (or exclude missing timestamps before min()) and add a mixed-group regression test.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@elementary/monitor/alerts/alert.py` around lines 61 - 67, Update the group
timestamp calculation in the logic containing min(alert.detected_at or
datetime.max ...) so missing timestamps use a timezone-aware UTC fallback, or
are excluded before computing min, preventing comparisons between naive and
aware datetimes. Preserve the existing behavior for present timestamps and add a
regression test covering a group with one normalized alert and one missing
timestamp.
Fixes #2304.
detected_atvalues loaded from the alerts table are naive datetimes stored in UTC.AlertModel.__init__passed them directly todatetime.astimezone(), which interprets naive datetimes as local time. On hosts whose timezone is not UTC this produced alert timestamps showing the UTC value labeled with the local timezone (e.g.Time: 2026-07-22 09:08:22 JSTfor an alert actually detected at 18:08:22 JST), inconsistent withmax_loaded_atin the same message which is correctly converted viaconvert_datetime_utc_str_to_timezone_str().Changes:
AlertModel.__init__: attach UTC tzinfo to naivedetected_atbefore converting;detected_at_utcis now always timezone-aware UTC. Timezone-aware inputs behave as before.timezoneconfig,detected_at_utc, and the source freshnessresult_descriptionend-to-end (pinned to a non-UTC local timezone viaTZso they fail without the fix regardless of CI timezone).Summary by CodeRabbit
Bug Fixes
Tests