-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[Stats Revamp] Changing "First day of week" results in incorrect numbers in Views & Visitors and Total Likes details #19829
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
StatsPeriodHelper().weekIncludingDate is used in the Stats to determine start and end of week, when week is Monday - Sunday. Use this method in the lastDayOfTheWeek to have a consistent start and end of week calculation and avoid bugs with different iOS firstDayOfWeek settings.
calculateEndDate and lastDayOfTheWeek duplicate the same code and comment. Use lastDayOfTheWeek in calculateEndDate.
You can test the changes in WordPress from this Pull Request by:
|
You can test the changes in Jetpack from this Pull Request by:
|
|
Updating the milestone to 21.6 |
twstokes
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This LGTM @staskus! 🚀
- The week ranges stayed consistent in the "Views & Visitors" as well as the "Total Likes" cards.
- I didn't spot any regressions in other areas of the code that call
calculateEndDate. I couldn't find any other paths that call it with theweekunit.
It's interesting that there was a lot of overlap between the deleted code and the function we're calling now.
Some items to consider:
⚠️ I'm not certain about are the-12hour offset in theDateComponentscall. Would that introduce an edge case for certain time zones?- The
lastDayOfTheWeekseems like a really straightforward function to unit test - it would be really nice to consider that since dates are so tricky. 💣 - 📅 There are some other solutions floating around that appear to be a cleaner way of getting the end of the week.
|
@twstokes, thank you for the review! 🙏
Yes, those were 2 identical pieces of code, so I 1) removed the duplication (by calling
That's an excellent question. I tried to track down and understand the reasoning. There's this comment that didn't spark too much confidence:
That's a great suggestion. It would be a good way to trigger cases where this |
This is not necessary with the current calculation approach and it produces wrong results with the current date is around the beginning or end of the week
|
@staskus I've taken a look at this and testing wise its fine. But I have a concern:
|
|
@sla8c, thanks for testing. It's a complicated issue, especially given the comments talking about corner case scenarios. I added a couple of tests for what I think are important scenarios: Different first days of the week and dates being close to the beginning and end of the week. When writing tests I couldn't find cases where this
I could return
I haven't tested a variety of time zones, just a variety of different times and the different first days of the week. What kind of testing scenarios would you have in mind?
I could do that just to be super-safe, although I couldn't find a single case where |
|
Update:
|
|
@staskus I agree that it is definitely a complicated issue Thanks for adding some tests and adding the FeatureFlag! I'm approving + in a future PR, it would be good if maybe we add some more different timezone tests and maybe we should mention this (different timezone testing) as part of the stats test plan. Thanks for tackling this issue! |
Time zones testing-wise are not playing a huge part, only the actual I updated the testing scenarios to include time zone tests. |
Description
Changing "First day of week" from Sunday to any other day in Language & Region, causes wrong numbers in Views & Visitors and Total Likes details pages after changing the date.
Stats views expect the week to be from Monday - Sunday but the method that calculates the date for this view uses the first day of week from settings.
Solution
The error happens after calling
StatsPeriodHelper.calculateEndDatefor a.weekperiod.After calculating the previous week, use the already created
StatsPeriodHelper().weekIncludingDateto calculate the end of the week. After this fix, the date calculation works as expected.Testing instructions
Enable "New Appearance for Stats" and "New Cards for Stats Insights" feature flags
Case 1 Views & Visitors:
Case 2 Total Likes:
Case 3 Date and time close to the beginning or end of the week
Regression Notes
Other parts of Stats that uses this calculation breaks
Manually testing old stats: Days / Weeks / Months and changing dates to make sure they continue working as expected.
None
PR submission checklist:
RELEASE-NOTES.txtif necessary.Images & Videos
Before the fix
First.Day.of.Week.-.before.fix.mp4
After the fix
First.Day.of.Week.-.after.fix.mov