Skip to content

Conversation

@jsnajdr
Copy link
Member

@jsnajdr jsnajdr commented Jan 18, 2018

While working on #21507 and getting familiar with the Stats code, I realized that there are few selectors that can be removed. They are either not used, or can be replaced with something better:

  • getSiteStatsTotalPostsForStreakQuery
  • getSiteStatsMaxPostsByDay
  • getSiteStatsPostsCountByDay

Read the commit messages for details.

How to test:
Go to /stats/insights/a8c.tv and play with the "Posting Activity" chart:

posting-activity-chart

When the width of the viewport is small, the chart should scroll and the scrolling arrows on the left and right site should switch between the "active" and "inactive" states. That verifies that the PostTrends component is rerendered when its state changes. That didn't work before, because its shouldComponentUpdate was broken and prevented the rerender.

Also, verify that the "dots" in the chart use the full spectrum of colors. That's what the getSiteStatsMaxPostsByDay selector used to help achieve and is now replaced by a computation directly in the render method.

It's only used in `PostTrends.shouldComponentUpdate` as an optimization attempt
to reduce rerenders. The changes in #21507 achieve that goal better, though: by
selector memoization and deep comparison of the `query` prop.

The `shouldComponentUpdate` and the associated selector is no longer needed. This
patch even solves a bug where the component wouldn't get rerendered when the
`state.canScrollLeft` and `state.canScrollRight` flags changed, and the scrolling
arrows on the "Posting Activity" chart wouldn't get the `is-active` CSS class.
@jsnajdr jsnajdr added [Feature] Stats Everything related to our analytics product at /stats/ [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. [Type] Performance labels Jan 18, 2018
@jsnajdr jsnajdr self-assigned this Jan 18, 2018
@jsnajdr jsnajdr requested review from lamosty, samouri and timmyc January 18, 2018 12:03
startDate;
const { streakData } = this.props;
// Compute maximum per-day post count from the streakData. It's used to scale the chart.
const maxPosts = reduce( streakData, ( max, count ) => ( count > max ? count : max ), 0 );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FFTI: could use _.max

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, converted to max( values( streakData ) ), as streakData is an object with shape { 'date1': count1, 'date2': count2, ... }.

months = [],
startDate;
const { streakData } = this.props;
// Compute maximum per-day post count from the streakData. It's used to scale the chart.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice comment!

@samouri
Copy link
Contributor

samouri commented Jan 18, 2018

Copy link
Contributor

@samouri samouri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I followed the testing instructions and everything worked as expected

Copy link
Contributor

@timmyc timmyc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testing out well for me, thanks for all the performance work you are doing in stats <3

@astralbodies
Copy link
Contributor

Unsetting & setting Needs Review label to force re-run of canaries with failing desktop canary now fixed.

@astralbodies astralbodies added [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. and removed [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. labels Jan 18, 2018
It computes the maximum per-day post count from `streakData`. This calculation is
better done in the `render` method of `PostTrends`. There it's performed only when
the component is rerendered, and not each time some irrelevant action is performed.

Another little drive-by fix is using month index as a `key`. That's completely sufficient,
no need to format date string.
@jsnajdr jsnajdr force-pushed the remove/stats-utils-unneeded-selectors branch from d1b5703 to 74788df Compare January 18, 2018 21:11
@jsnajdr jsnajdr merged commit b4797ec into master Jan 18, 2018
@jsnajdr jsnajdr deleted the remove/stats-utils-unneeded-selectors branch January 18, 2018 21:15
@sirreal sirreal removed the [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. label Mar 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Feature] Stats Everything related to our analytics product at /stats/ [Type] Performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants