Stats doesn't roll over to the new day at the site's midnight

未关闭
#26,412 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
38/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
ios, swift
领域
backend, mobile

调研方向

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.

由索引模型根据 Issue 内容生成。

描述

[Type] Bug Stats

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.

  • StatsViewModel builds its preset range in init and only changes it when the user does.
  • TodayCardViewModel works out "today" again only when its dateRange is assigned. The dashboard card also does this in refreshIfNeeded(), but only when it appears, and only when no load is running.
  • StatsService decides whether to cache a response forever or for 30 seconds after the response arrives (intervalContainsCurrentDate in getSiteStats and getTopListData). 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.currentDateText formats Date() 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.significantTimeChangeNotification and 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 for now and keep the comparison setting. Navigating clears preset, so ranges the user moved to stay put, and so do custom ranges.
  • Let update(preset:) / updating(preset:) take now: 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:

  • StatsViewModel and StatsTodayCardController both tell the Today card about a new day directly. The card works out "today" again and reloads, even if a load is running. The current isLoading check in refreshIfNeeded() 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
主要语言
Swift
星标
3.9k
派生
1.2k
平均合并
1 天 23 小时
30 天内合并 PR
136

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

wordpress-mobile/WordPress-iOS 的其他 Issue

查看 wordpress-mobile/WordPress-iOS 的全部 Issue

相似的 Issue

更多 Swift Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。