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

Abierto
#26,412 1 comentario 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
5/5
Tiempo estimado
Más de una semana
Aptitud para principiantes
38/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
ios, swift
Área
backend, mobile

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

[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
Lenguaje dominante
Swift
Estrellas
3.9k
Forks
1.2k
Merge medio
1 d 23 h
PR fusionados (30 d)
136

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de wordpress-mobile/WordPress-iOS

Todos los issues de wordpress-mobile/WordPress-iOS

Issues similares

Más issues de Swift

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.