[PR #6259] fix: stats command to correctly handle --days 0 for current day statistics #11804

Closed
opened 2026-02-16 18:16:44 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/6259

State: closed
Merged: Yes


Summary

This PR fixes an issue where running opencode stats --days 0 would display "All Time" statistics instead of statistics for the current day. It also refactors the time calculation logic for better readability and testability.

Changes

  • Fixed days=0 logic: Previously, days=0 was treated as falsy, causing the cutoff time to default to 0 (Unix Epoch), which displayed the entire history. Now, days=0 explicitly sets the cutoff time to 00:00:00 of the current day.
  • Refactoring: Renamed DAYS_IN_SECOND to MS_IN_DAY and exported aggregateSessionStats to enable unit testing.
  • New Tests: Added unit tests to verify time filtering logic for today, specific day ranges, and default behavior.
    Behavior Comparison
  1. Before Fix: opencode stats --days 0
    Issue: 0 is treated as "undefined/all-time".
  • Result: Displays stats for the entire history of the project.
  1. Before/After (Unchanged): opencode stats --days 1
    Behavior: correctly calculates "Last 24 Hours".
  • Result: Displays stats for the rolling 24-hour window.
  1. After Fix: opencode stats --days 0
    Fix: 0 is treated explicitly as "Today".
  • Result: Displays stats only for sessions created since midnight today.

Verification

  • Ran bun dev stats --days 0 to verify it captures only today's sessions.
  • Ran bun dev stats --days 1 to verify it still captures the last 24h.
  • Validated via new unit tests covering days=0, days=N, and days=undefined scenarios.
**Original Pull Request:** https://github.com/anomalyco/opencode/pull/6259 **State:** closed **Merged:** Yes --- ### Summary This PR fixes an issue where running `opencode stats --days 0` would display "All Time" statistics instead of statistics for the current day. It also refactors the time calculation logic for better readability and testability. ### Changes - **Fixed `days=0` logic:** Previously, `days=0` was treated as falsy, causing the cutoff time to default to `0` (Unix Epoch), which displayed the entire history. Now, `days=0` explicitly sets the cutoff time to **00:00:00** of the current day. - **Refactoring:** Renamed `DAYS_IN_SECOND` to `MS_IN_DAY` and exported `aggregateSessionStats` to enable unit testing. - **New Tests:** Added unit tests to verify time filtering logic for today, specific day ranges, and default behavior. Behavior Comparison 1. Before Fix: `opencode stats --days 0` **Issue:** `0` is treated as "undefined/all-time". - **Result:** Displays stats for the entire history of the project. 2. Before/After (Unchanged): opencode stats --days 1 Behavior: correctly calculates "Last 24 Hours". - Result: Displays stats for the rolling 24-hour window. 3. After Fix: opencode stats --days 0 Fix: 0 is treated explicitly as "Today". - Result: Displays stats only for sessions created since midnight today. ### Verification - Ran bun dev stats --days 0 to verify it captures only today's sessions. - Ran bun dev stats --days 1 to verify it still captures the last 24h. - Validated via new unit tests covering days=0, days=N, and days=undefined scenarios.
yindo added the pull-request label 2026-02-16 18:16:44 -05:00
yindo closed this issue 2026-02-16 18:16:44 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#11804