Repository navigation
fix(wakatime): serve today and yesterday stats from summaries - #20969
Conversation
The coding-activity view offers a Today range, and the stats handler allowlists today and yesterday, but WakaTime's stats endpoint answers 400 for both. The handler then reports a 502 upstream_unavailable, so the Today button always shows "WakaTime is unreachable or the API key was rejected" even with a valid key. fetch_stats now asks the summaries endpoint for those two ranges and folds the day into the stats fields the view reads, keeping the stats split between total_seconds (without Other) and the *_including_other_language totals.
The today/yesterday stats path repeated the export handler's per-name summaries sum and fetch_summaries' "no data list -> raise" check. Both now go through one helper each: sum_seconds_by_name in the client, used by the stats fold and _rows_by_project, and _fetch_summary_days, used by fetch_summaries and the single-day stats path.
UX Review (Opus 5.5) — ⏭️ skippedRevision |
Design Review (Opus 5.5) — ✅ PASSDesign-level review of Design-Verdict: PASS Nothing to check. [DESIGN-REVIEWED] 65e45b0 Description read[DESCRIPTION-READ] e412c8d91626396b68caff27e63e2e674bf819c971789a04b4d9c9a05983ab17 That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with |
First Principles Review (Opus 5.5) — 🟡 CONCERNSPremise-level review of First-Principles-Verdict: CONCERNS The single-day path adds three Not justified as shipped
What this change shipsInventory (7 items) — 5 justifiedIntent: WakaTime users clicking Today/Yesterday see their stats instead of a false "key rejected" error — FIX.
[FIRST-PRINCIPLES-REVIEWED] 65e45b0 Description read[DESCRIPTION-READ] b258233363fa6a2e56da62978c68fef067c65097d774072bf9543c344cda90b7 That sha256 names the PR description this verdict read. Recompute it with |
Opus 5.5 Review — ✅ no blocking findingsReviewed Verdict parsed from the review's SHA-scoped output markers for commit False positive or not applicable? A repository writer can comment: |
GPT 6.1 Review — ✅ no blocking findingsGPT 6.1 completed its review of This comment is updated in place on each push. Review detailsNo findings. False positive or not applicable? A repository writer can comment: |
chenmingwei23
left a comment
There was a problem hiding this comment.
Approving: readiness: passed, all lanes green, no outstanding change requests.
Problem / Motivation
Goal: The WakaTime view's Today range shows today's coding stats instead of an "unreachable or API key rejected" error.
Clicking Today in the WakaTime coding-activity view always shows "WakaTime is unreachable or the API key was rejected", even with a valid key.
The gateway log shows
WakaTime API GET /users/current/stats/today failed: http=400.Why it matters
Every WakaTime user who clicks Today sees an error.
The error message points them at their API key or network, so they go looking for a problem that does not exist.
Not a goal
What changed (motivation → approach → change)
/api/wakatime/stats?range=todayreturns 502upstream_unavailable.todayandyesterday, but WakaTime's/users/current/stats/:rangeendpoint answers 400 for both. It only accepts ranges likelast_7_days.fetch_statsturns the 400 intoWakaTimeUnavailableError, which the handler reports as a 502.todayandyesterday,WakaTimeClient.fetch_statsnow calls/users/current/summaries?range=Today|Yesterdayand converts the day into the stats fields the view reads (total_seconds,daily_average,languages,projects, and the*_including_other_languagetotals). Keeping this in the client means the handler and the frontend do not change. It also mirrors the stats endpoint's split, wheretotal_secondsleaves out the "Other" language and the*_including_other_languagefields include it. An upstream failure still raises, so an outage is never shown as a false zero.sum_seconds_by_namein the client is now the only per-name summaries fold, used by both the stats fold and the export handler's_rows_by_project._fetch_summary_daysis the only strict summaries fetch, used by bothfetch_summariesand the single-day stats path.Backwards compatibility
Compatible: the endpoint accepts the same ranges, and for every range that worked before it returns the same response. Only the two ranges that always failed now succeed.
Removes nothing: no path, key, validator or public function is removed or narrowed.
Tests
test_fetch_stats_today_uses_summaries_not_stats:todaycalls the summaries endpoint withrange=Today, and the result has the correct totals with and without "Other", plus ranked languages and projects.test_fetch_stats_yesterday_maps_to_summaries_range:yesterdaymaps torange=Yesterday, and an empty day gives zero totals.test_fetch_stats_today_raises_on_server_error: an upstream 500 still raisesWakaTimeUnavailableError.The first two tests fail on
mainand pass with this change.All tests in
test/test_wakatime_*.pyand the other wakatime-touching test files pass on the fresh base (1272 passed).black, isort, flake8 and mypy are clean on the changed files.
Manual verification
Run against the live WakaTime API with a real key:
main:stats/todayandstats/yesterdayreturn 400, whilelast_7_daysandlast_30_daysreturn 200.fetch_stats("today"),fetch_stats("yesterday")andfetch_stats("last_7_days")all succeed.Related Issues
Fixes #20967
Replaces #19394
Original fix and tests by @Inthuson in #19394; this same-repo PR carries their two commits unchanged (authorship preserved) on a fresh base.
Pattern harvest
Rule candidate: review-prompt
Pattern: an allowlist of upstream API parameters that was never checked against the upstream endpoint's documented values.
Checklist
feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)Contribution License Agreement
Contributions are accepted under the project's Apache-2.0 license; see the Licensing section of CONTRIBUTING.md.