Stats doesn't roll over to the new day at the site's midnight
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Idoneità per principianti
- 38/100
Direzione di ricerca
Start with StatsViewModel, StatsDateRange, TodayCardViewModel, StatsService, and TodayCard.currentDateText, then trace how preset ranges, Today card loads, and cache expiry use the current date. Implement the site's time zone day monitor and the rollover and cache changes described in the issue, using an injectable clock for tests. Done means the listed unit tests pass and Stats updates across the site's midnight, including when a load is in progress or the app resumes.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
From Claude:
Stats doesn't notice when the site's day changes. If Stats is open at midnight in the site's time zone, it keeps showing the old day until the user picks a new range or pulls to refresh.
What happens
- The Today card shows yesterday's totals under today's date. This affects the card on the Traffic tab and the one on the My Site dashboard.
- Ranges like "Last 7 days" or "This month" stop moving. After midnight they still end yesterday, so the newest day never appears.
- A load that is running at midnight can be cached forever with yesterday's partial totals. This one lasts for the rest of the app session, not just until the next reload.
PR #26410 made one case easier to hit. Before it, the chart sent an unchanged selection to every card when it finished loading. That made the Today card work out "today" again, and if midnight had passed, it switched to the new day. The update was a bug (it restarted loads that were still running), and #26410 removed it. Now, if the Today card's first load is still running when the site's midnight passes, the card finishes with yesterday's numbers (reported by @crazytonyli in this review).
Root Cause
Each date range is worked out once from the current time, when it's created. Nothing in JetpackStats watches for the day to change.
StatsViewModelbuilds its preset range ininitand only changes it when the user does.TodayCardViewModelworks out "today" again only when itsdateRangeis assigned. The dashboard card also does this inrefreshIfNeeded(), but only when it appears, and only when no load is running.StatsServicedecides whether to cache a response forever or for 30 seconds after the response arrives (intervalContainsCurrentDateingetSiteStatsandgetTopListData). A request for "today" sent at 23:59:58 that comes back at 00:00:01 counts as a past day, so the cache keeps its partial totals with no expiry.TodayCard.currentDateTextformatsDate()in the device's time zone, so the header can show a different day from the data even without a rollover.
Proposed fix
1. Detect a new day in the site's time zone
Add a small @MainActor monitor that reports when the day changes in context.timeZone:
- Wait until the site's next midnight (
calendar.dateInterval(of: .day, for: now).end). - Also check after
UIApplication.significantTimeChangeNotificationand when the app returns to the foreground, because timers don't run while the app is suspended. - Report a change only when the site's day really differs from the last one it saw, so these extra checks don't cause extra reloads.
- Take the clock as a parameter so tests can control it.
NSCalendarDayChanged won't work, because it fires at the device's midnight.
2. Move preset ranges forward
Add StatsDateRange.rolledOver(now:):
- If the range came from a preset (
preset != nil), work it out again fornowand keep the comparison setting. Navigating clearspreset, so ranges the user moved to stay put, and so do custom ranges. - Let
update(preset:)/updating(preset:)takenow:instead of always using.now, so tests can control time.
StatsViewModel assigns the rolled-over selection when the monitor reports a new day. For preset ranges that's a real change, so the guard from #26410 lets it through and every card reloads.
3. Always roll the Today card over
The Today card always shows today, even when the main range doesn't move (for example, a custom range). So:
StatsViewModelandStatsTodayCardControllerboth tell the Today card about a new day directly. The card works out "today" again and reloads, even if a load is running. The currentisLoadingcheck inrefreshIfNeeded()would skip that.- When a load finishes, the card checks that the range it loaded still contains
currentDate(). If it doesn't, it reloads instead of showing the old data. This covers a load that started just before midnight.
4. Classify cache entries when the request is sent
In StatsService.getSiteStats and getTopListData, run intervalContainsCurrentDate before the request goes out. A response for an interval that was current when it was requested then gets the 30-second expiry, even if it arrives after midnight. Check getWordAdsStats (dateIsToday) for the same pattern.
5. Header date
Format TodayCard.currentDateText from the start of the loaded range, using the site's time zone.
Open questions
- Selected bar or subrange: keep it if it still falls inside the new range, otherwise clear it? (Suggested default.)
- Detail screens (
AuthorStatsView,UTMMetricStatsView,PostStatsView,StandaloneChartCard) keep their own copy of the range. Suggest leaving them out at first. They're usually open only briefly, and pull-to-refresh is enough there.
Test plan
- Unit tests with a clock the test controls:
- The monitor fires at the site's midnight, not the device's, and fires once per day.
-
rolledOver(now:)moves preset ranges and leaves navigated and custom ranges alone. - A Today card load running at midnight is replaced by a load for the new day, and yesterday's data never shows.
- A response for a current interval that arrives after midnight is cached with the 30-second expiry, not forever.
- On a simulator: set the test site's time zone so its midnight is two minutes away, leave Stats open on "Last 7 days", and check that the Today card, the chart and the top lists all move to the new day without any interaction. Repeat with the app in the background across midnight, and with a custom range selected.
Related issues
- #26410
- Lingua principale
- Swift
- Stelle
- 3.9k
- Fork
- 1.2k
- Merge medio
- 1g 23h
- PR unite (30g)
- 136
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di wordpress-mobile/WordPress-iOS
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
wordpress-mobile/WordPress-iOS#26423 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
[Pri] Low [Status] Ready for Dev [Type] Bug /Me
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
wordpress-mobile/WordPress-iOS#26279 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
[Pri] Low [Status] Ready for Dev [Type] Bug Share Extension
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
wordpress-mobile/WordPress-iOS#26276 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
[Pri] Low [Status] Ready for Dev [Type] Bug Blogging Prompts Notifications
Difficoltà 2/5 1-3 ore Idoneità per principianti 79/100
wordpress-mobile/WordPress-iOS#26256 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
[Status] Ready for Dev [Type] Bug Reader
Difficoltà 2/5 1-3 ore Idoneità per principianti 83/100
wordpress-mobile/WordPress-iOS#26254 · 2 commenti ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di wordpress-mobile/WordPress-iOS
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
-
🎉 Add RemoteCrabAperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 70/100
jaywcjlove/awesome-mac#4999 ·
I maintainer di solito rispondono entro 1 giorno
-
[Type] Bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
wordpress-mobile/GutenbergKit#940 ·
I maintainer di solito rispondono entro 1 giorno
-
pending-maintainer-response pending-triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
aws-amplify/amplify-swift#4423 ·
I maintainer di solito rispondono entro 2 giorni
-
tests
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
erkamyaman/unslop-anything#80 ·
I maintainer di solito rispondono entro 1 giorno