Skip to content

Stop Stats cards reloading when the date range hasn't changed - #26410

Merged
jkmassel merged 3 commits into
trunkfrom
jkmassel/stats-skip-unchanged-date-range
Nov 24, 2026
Merged

jkmassel merged 3 commits into
trunkfrom
jkmassel/stats-skip-unchanged-date-range

Conversation

@jkmassel

Copy link
Copy Markdown
Contributor

Fixes a race where the Stats Traffic screen cancels the Today card's and top-list cards' first load and starts it again, so they appear one request later than they should.

What happens

When the chart card finishes loading before the Today card, the Today card's request is cancelled and sent again. The Today card then appears a full request later than the chart. Top-list cards (Posts & Pages, Referrers, …) are hit the same way.

It depends on which request finishes first, so it doesn't happen every time.

Root Cause

StatsViewModel passes its date range selection to every card each time dateRange is assigned, even when the value hasn't changed. TodayCardViewModel and TopListViewModel reload on every assignment, and a reload cancels the load in flight.

Three things assign an unchanged selection:

  1. The chart's first load. After loading, ChartCardViewModel sets selectedBarDate (nil → nil). The $selectedBarDate.dropFirst().removeDuplicates() pipeline lets that first value through, because removeDuplicates() has nothing earlier to compare it with. handleChartBarSelection then sets subrange = nil, which is already nil.
  2. A range change with a bar selected. The chart keeps the bar highlight until its new data arrives, then clears it, which sends the unchanged selection again.
  3. A Today card tap or a subrange arrow. The chart sends the new subrange back through selectedBarDate, so one tap starts the Today load twice.

Traced on a simulator, with a temporary 2 s delay added to StatsService.getSiteStats to make the timing visible:

Time (s) Event
163.82 Today card starts loading
166.11 Chart data arrives
166.11 StatsViewModel sends the unchanged selection to every card (subrange=nil)
166.17 Today card's first request cancelled, second one starts
168.18 Today card data arrives, 2 s (one request) after the chart

Fix

StatsViewModel.dateRange's didSet now returns early when the new value equals the old one.

Nothing depends on sending an unchanged selection again:

  • Pull-to-refresh reloads each card directly.
  • New cards get the range when they're created.
  • Picking or clearing a bar, changing the comparison period, and promoting a subrange are real changes, so they still go through.

The Today card still reloads when you pick a bar, even though its data doesn't depend on the bar. That's pre-existing and unchanged here.

Test plan

  • New StatsViewModelDateRangeTests with a stub service that holds requests until the test releases them, covering all three triggers. They fail on trunk (Today: 2 requests cancelled, 4 made instead of 2; top list: 8 made instead of 2; the Today tap starts 2 loads) and pass with the fix: 150 of 150 runs.
  • Full JetpackStatsTests suite passes: 243 tests.
  • Checked in the app (Debug, iOS 27.2 simulator, temporary 2 s request delay, 3 runs). After the chart loads, no selection update goes out, the Today card loads once, and nothing is cancelled. In one run the chart finished first and the Today card still completed its original request 45 ms later. Before the fix, that case delayed the Today card by 2 s.

StatsViewModel passed its date range selection to every card whenever
it was assigned, even when the value hadn't changed. The Today and
top-list cards reload on every assignment, so an unchanged assignment
cancelled any load still in flight and started it again, one request
later.

The chart triggered this itself: after each load it sets selectedBarDate
(usually nil to nil), and the selection pipeline sends that on as a new
subrange. It also happened after a range change with a bar selected,
and on a Today card tap, which started the Today load twice.

Only pass the selection on when it actually changes.

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

AI found an edge case of using Stats at midnight. I don't think it's a big deal, and I'll leave it to you to decide whether to fix it or not.

The new guard can leave the Today card showing yesterday’s totals when its initial load crosses site midnight. Previously, the chart finishing first triggered Today to recompute its date and reload. The guard now drops that update because the selected range is unchanged, allowing Today’s old request to finish. A simulator test reproduces this on head and passes on base.

@jkmassel

Copy link
Copy Markdown
Contributor Author

From Claude: Made a note to come back to this in #26412

@jkmassel
jkmassel added this pull request to the merge queue Nov 17, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to no response for status checks Nov 17, 2026
@jkmassel
jkmassel added this pull request to the merge queue Nov 17, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to no response for status checks Nov 17, 2026
@jkmassel
jkmassel added this pull request to the merge queue Nov 18, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to no response for status checks Nov 18, 2026
@wpmobilebot wpmobilebot modified the milestones: 27.5, 27.6 Nov 20, 2026
@wpmobilebot

Copy link
Copy Markdown
Contributor

Version 27.5 has now entered code-freeze, so the milestone of this PR has been updated to 27.6.

@jkmassel
jkmassel added this pull request to the merge queue Nov 24, 2026
@jkmassel
jkmassel removed this pull request from the merge queue due to a manual request Nov 24, 2026
@jkmassel
jkmassel added this pull request to the merge queue Nov 24, 2026
Merged via the queue into trunk with commit bb2d7cf Nov 24, 2026
21 checks passed
@jkmassel
jkmassel deleted the jkmassel/stats-skip-unchanged-date-range branch November 24, 2026 01:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants