omi icon indicating copy to clipboard operation
omi copied to clipboard

[Bounty proposal] fix(backend): focus-stats with no date sums the user's whole history ($50 proposed)

Open VIxIon0de opened this issue 2 months ago • 1 comments

Following the contribution guide's invitation to propose paid work — @kodjima33, @josancamon19, would a US$50 bounty be appropriate for this? Proposed amount, not an existing award. Fix and tests are already up as #15279.

Symptom

GET /v1/focus-stats without a date reports every focus session the account has ever recorded, labelled as a single day.

Cause

get_focus_stats (backend/database/focus_sessions.py) passes its optional date straight through:

sessions = get_focus_sessions(uid, date=date, limit=5000, offset=0)

and get_focus_sessions applies its created_at window only when a date is actually given:

if date:
    day_start = datetime.strptime(date, '%Y-%m-%d').replace(tzinfo=timezone.utc)
    day_end = day_start + timedelta(days=1)
    query = query.where(...)

So date=None means no time filter at all. The function returns the 5000 most recent sessions across all time, sums their durations, and stamps the result with today:

'date': date or datetime.now(timezone.utc).strftime('%Y-%m-%d'),

date is optional on the route (backend/routers/focus_sessions.py), so this is the default read, not an edge case. An account holding two months of sessions sees those two months' focused_minutes, distracted_minutes, session_count and top_distractions presented as today's figures. Because the totals are plausible numbers under a correct-looking date, nothing in the response signals the value is wrong.

Not a duplicate of #15161

#15161 / #15162 fix the timezone in which the day window and the label are computed. That is a real and separate defect. It does not cover this one: in that PR get_focus_sessions still applies the filter only if date, so a dateless request stays unfiltered and get_focus_stats still aggregates the full history. The two changes compose.

Precedent

get_daily_score (backend/database/action_items.py) is the same shape of endpoint and resolves a missing date instead of forwarding it:

if date:
    day = datetime.strptime(date, '%Y-%m-%d').replace(tzinfo=tz)
else:
    day = datetime.now(tz).replace(hour=0, minute=0, second=0, microsecond=0)

focus-stats should behave the same way.

Fix

Resolve the day once, query that day, and report the day queried. Three regression tests in backend/tests/unit/test_focus_stats_day_scope.py; two of the three fail against current main and pass with the change.

PR: #15279

VIxIon0de avatar Sep 20 '26 20:09 VIxIon0de

Fixed by #15279. get_focus_stats scopes a missing date to one day.

aryanorastar avatar Sep 25 '26 03:09 aryanorastar