Skip to content

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

Closed
Inthuson wants to merge 2 commits into
kirodotdev:mainfrom
Inthuson:fix/wakatime-today-range
Closed

Inthuson wants to merge 2 commits into
kirodotdev:mainfrom
Inthuson:fix/wakatime-today-range

Conversation

@Inthuson

@Inthuson Inthuson commented Oct 12, 2026 •

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 65 tests in test/test_wakatime_*.py pass.
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

N/A - no existing issue found for this bug.

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.
@Inthuson
Inthuson requested review from a team and KiroCrewCaptain as code owners October 12, 2026 11:05
@Inthuson
Inthuson requested a review from iamwhatever October 12, 2026 11:05
@github-actions github-actions Bot added fork Pull request from a fork (external contributor) readiness: action required A blocking check or review needs attention labels Oct 12, 2026
@github-actions

github-actions Bot commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5, fork) — ⏭️ skipped

Revision dd3b0ce61e53648430ddd147739389e61f339f66 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

github-actions Bot commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5, fork) — ✅ PASS

Design-level review of dd3b0ce61e53648430ddd147739389e61f339f66 via the fork AI-review pipeline — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

Design-Verdict: PASS

Nothing to check.

[DESIGN-REVIEWED] dd3b0ce

Description read

[DESCRIPTION-READ] 3101ace13ce0d01365ffe1d3803e85137a7c502ac25e77ce3bd464bac9b05a7b

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

github-actions Bot commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5, fork) — 🟡 CONCERNS

Premise-level review of dd3b0ce61e53648430ddd147739389e61f339f66 via the fork AI-review pipeline — 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 two *_including_other_language fields the single-day fold now builds have no reader: the view uses total_seconds, daily_average, languages and projects only.

Not justified as shipped

  • Item 3 — zero consumers: the description says the fold returns "the stats fields the view reads (… and the *_including_other_language totals)". A grep for including_other_language outside static/dist finds 0 readers. It appears only in this PR's test. WakaTimeTab.tsx reads total_seconds, daily_average, languages and projects. The handler passes stats through without reading these fields.
    Subtraction: delete the total_seconds_including_other_language and daily_average_including_other_language keys from _stats_from_summaries. total_with_other stays, because the fold needs it to work out total_seconds.
    Clears when: the two keys are removed, or a non-test reader is counted.
  • Item 2 — rides along: "yesterday" has no button in the view (RANGES in WakaTimeTab.tsx lists no yesterday). It is one map entry, and the base handler already accepts that range.

What this change ships

Inventory (5 items) — 2 justified

Intent: a WakaTime user clicks Today and sees today's coding stats instead of a false "unreachable / key rejected" error. FIX.

  1. Today in the WakaTime view shows the day's totals, languages and projects instead of a 502 error — justified
  2. range=yesterday on /api/wakatime/stats now succeeds instead of returning a 502 — rides along; nothing in the view uses it
  3. The stats for a single day carry the two *_including_other_language totals — zero consumers
  4. The CSV export's per-project sum moves into the shared sum_seconds_by_name helper — rides along; it removes a duplicate fold
  5. A server error on the Today path still raises instead of showing zeros — justified

[FIRST-PRINCIPLES-REVIEWED] dd3b0ce

Description read

[DESCRIPTION-READ] ea64226ca45964f6d0bb5d2539c0d0f543094b6f0d095af5fbccacd24c1e5d90

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

github-actions Bot commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5.5 Review (fork) — ✅ no blocking findings

Reviewed dd3b0ce61e53648430ddd147739389e61f339f66 via the fork AI-review pipeline; updated in place on each push.

Review details

No findings.

[OPUS-REVIEWED] dd3b0ce

@github-actions

github-actions Bot commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

GPT 6.1 Review (fork) — ✅ no blocking findings

Reviewed dd3b0ce61e53648430ddd147739389e61f339f66 via the fork AI-review pipeline; updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] dd3b0ce

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.
@Inthuson

Copy link
Copy Markdown
Contributor Author

Replying to the First Principles review on 9243969. dd3b0ce addresses item 5.

  • Item 5 (duplicate fold and duplicate raise): fixed in dd3b0ce. sum_seconds_by_name in wakatime/client.py is now the only per-name summaries fold. The stats fold and handlers/wakatime.py:_rows_by_project both use it. _fetch_summary_days is now the only strict summaries fetch. fetch_summaries and the single-day stats path both use it.
  • Item 4 (the Other split is unverified): WakaTime's stats API docs define total_seconds as "total coding activity, excluding "Other" language" and total_seconds_including_other_language as "total coding activity". daily_average is split the same way. A summary day's grand_total has no such split, so subtracting the "Other" language is what makes Today mean the same thing as every other range. The same docs list the valid stats ranges (last_7_days, last_30_days, last_6_months, last_year, all_time, YYYY, YYYY-MM), which confirms that today and yesterday get a 400.
  • Item 3 (*_including_other_language has no readers): correct for main today. The reader is open PR fix(wakatime): headline reads the include-other coding total #18668, which switches the headline to these fields so that it matches WakaTime.com. Without them, the Today range would fall back to the total that leaves out "Other" once that PR lands. I kept them so the two PRs work together, but I'm happy to drop them if maintainers would rather fix(wakatime): headline reads the include-other coding total #18668 add them itself.

@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Oct 12, 2026
@iamwhatever

Copy link
Copy Markdown
Contributor

Thank you @Inthuson for finding this bug and writing the fix and tests. To get it past the main-side CI reds on this fork branch, we carried your two commits unchanged (your authorship kept) onto a fresh base in same-repo PR #20969, which is now green and ready for review. It also links the new tracking issue #20967. Closing this one in favour of #20969; your branch is left as is. Thanks again!

@github-actions github-actions Bot removed the readiness: action required A blocking check or review needs attention label Oct 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fork Pull request from a fork (external contributor)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants