Skip to content

fix(wakatime): serve today and yesterday stats from summaries - #20969

Merged
chenmingwei23 merged 2 commits into
mainfrom
replace/19394
Oct 16, 2026
Merged

chenmingwei23 merged 2 commits into
mainfrom
replace/19394

Conversation

@iamwhatever

Copy link
Copy Markdown
Contributor

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)

  • Symptom: /api/wakatime/stats?range=today returns 502 upstream_unavailable.
  • Root cause: the handler allowlists today and yesterday, but WakaTime's /users/current/stats/:range endpoint answers 400 for both. It only accepts ranges like last_7_days. fetch_stats turns the 400 into WakaTimeUnavailableError, which the handler reports as a 502.
  • Change: for today and yesterday, WakaTimeClient.fetch_stats now calls /users/current/summaries?range=Today|Yesterday and converts the day into the stats fields the view reads (total_seconds, daily_average, languages, projects, and the *_including_other_language totals). Keeping this in the client means the handler and the frontend do not change. It also mirrors the stats endpoint's split, where total_seconds leaves out the "Other" language and the *_including_other_language fields include it. An upstream failure still raises, so an outage is never shown as a false zero.
  • Shared helpers, so the fix adds no second copy: sum_seconds_by_name in 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_days is the only strict summaries fetch, used by both fetch_summaries and 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: today calls the summaries endpoint with range=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: yesterday maps to range=Yesterday, and an empty day gives zero totals.
  • test_fetch_stats_today_raises_on_server_error: an upstream 500 still raises WakaTimeUnavailableError.

The first two tests fail on main and pass with this change.
All tests in test/test_wakatime_*.py and 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:

  • On main: stats/today and stats/yesterday return 400, while last_7_days and last_30_days return 200.
  • With this change: fetch_stats("today"), fetch_stats("yesterday") and fetch_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

  • At most two commits (one is the norm), with a Conventional Commits title (feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable)
  • No secrets, credentials, or internal references in the diff

Contribution License Agreement

Contributions are accepted under the project's Apache-2.0 license; see the Licensing section of CONTRIBUTING.md.

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.
@github-actions github-actions Bot added the readiness: checking Automated validation is still running label Oct 15, 2026
@github-actions

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5) — ⏭️ skipped

Revision 65e45b004755f72e949c09c4c1425c9485da32c8 touches no user-facing surface (no changes under website/ or committed screenshots), so the UX review was skipped. Advisory — does not block merge.

@github-actions

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5) — ✅ PASS

Design-level review of 65e45b004755f72e949c09c4c1425c9485da32c8 — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

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 .github/scripts/pr-description-capture.sh over the same inputs; if it differs, the description or that evidence changed after this verdict was formed and any finding drawn from either is unproven.

@github-actions

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5) — 🟡 CONCERNS

Premise-level review of 65e45b004755f72e949c09c4c1425c9485da32c8 — why this exists and whether the shipped surface is the smallest honest version. Updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

First-Principles-Verdict: CONCERNS

The single-day path adds three *_including_other_language fields. Nothing reads them, yet the description says "the view reads" them.

Not justified as shipped

  • Item 3 — zero consumers: the description says it "converts the day into the stats fields the view reads (… the *_including_other_language totals)". WakaTimeTab.tsx reads only total_seconds, daily_average, languages and projects (lines 107–110). I grepped including_other in src/ and website/src: 0 consumers outside client.py.
    Subtraction: delete total_seconds_including_other_language and daily_average_including_other_language from the _stats_from_summaries return.
    Clears when: the two keys are removed, or a reader of them is shown.
  • Item 4 — inherited (symmetry): "Mirror that split so a day reads the same way as any other range." The live check in the description shows only that the calls succeed. It does not show that the stats endpoint's total_seconds leaves out "Other".
    Clears when: a live stats response shows total_seconds leaving out the "Other" language.

What this change ships

Inventory (7 items) — 5 justified

Intent: WakaTime users clicking Today/Yesterday see their stats instead of a false "key rejected" error — FIX.

  1. Today range shows real stats instead of a 502 error — justified
  2. Yesterday range shows real stats instead of a 502 error — justified
  3. Today/Yesterday responses gain three including-"Other" total/average fields — zero consumers
  4. Today/Yesterday headline total subtracts the "Other" language — inherited, mirrors stats endpoint unverified
  5. Billable-hours export now uses the shared per-name fold — justified
  6. Strict summaries fetch extracted, shared with the single-day path — justified
  7. New public sum_seconds_by_name in the WakaTime client (2 callers) — justified

[FIRST-PRINCIPLES-REVIEWED] 65e45b0

Description read

[DESCRIPTION-READ] b258233363fa6a2e56da62978c68fef067c65097d774072bf9543c344cda90b7

That sha256 names the PR description this verdict read. Recompute it with .github/scripts/pr-description-capture.sh; if it differs, the description changed after this verdict was formed and any finding drawn from the description is unproven.

@github-actions

Copy link
Copy Markdown
Contributor

Opus 5.5 Review — ✅ no blocking findings

Reviewed 65e45b004755f72e949c09c4c1425c9485da32c8 — this comment is updated in place on each push.

Review details

No findings.

[OPUS-REVIEWED] 65e45b0

Verdict parsed from the review's SHA-scoped output markers for commit 65e45b004755f72e949c09c4c1425c9485da32c8.

False positive or not applicable? A repository writer can comment:
/ai-review override fable 65e45b004755f72e949c09c4c1425c9485da32c8: <one-sentence reason>

@github-actions

Copy link
Copy Markdown
Contributor

GPT 6.1 Review — ✅ no blocking findings

GPT 6.1 completed its review of 65e45b004755f72e949c09c4c1425c9485da32c8 and found no blocking issues.

This comment is updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] 65e45b0

False positive or not applicable? A repository writer can comment:
/ai-review override gpt 65e45b004755f72e949c09c4c1425c9485da32c8: <one-sentence reason>

@iamwhatever
iamwhatever marked this pull request as ready for review October 15, 2026 22:54
@iamwhatever
iamwhatever requested review from a team and KiroCrewCaptain as code owners October 15, 2026 22:54
@github-actions github-actions Bot added readiness: passed Eligible automated validation passed for the current revision and removed readiness: checking Automated validation is still running labels Oct 15, 2026

@chenmingwei23 chenmingwei23 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving: readiness: passed, all lanes green, no outstanding change requests.

@chenmingwei23
chenmingwei23 merged commit 65bc96f into main Oct 16, 2026
105 checks passed
@chenmingwei23
chenmingwei23 deleted the replace/19394 branch October 16, 2026 01:38
@github-actions github-actions Bot removed the readiness: passed Eligible automated validation passed for the current revision label Oct 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WakaTime Today/Yesterday ranges fail with HTTP 400 and show "API key rejected"

3 participants