Skip to content
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

Stats: Add stats list to redux tree #5743

Merged
merged 4 commits into from
Jun 6, 2016
Merged

Conversation

timmyc
Copy link
Contributor

@timmyc timmyc commented Jun 2, 2016

For #5046 this branch seeks to create a new stats state sub-tree to accommodate the data that is currently being managed by the legacy stats-list. This branch builds upon some of the concepts from @jblz 's prior PR on this topic ( #2310 ) but also incorporates approaches taken in the Terms state tree as well.

Stats List State Structure

Since the stats list houses a variety of data from various endpionts, I have opted to segment the state tree of items by siteId and the statsType - which is essentially the method name used on wpcom.js to call out to a particular stats endpoint ( i.e. statsStreak, statsCountryViews etc ).

statsTypes for a givent siteId are then keyed by a stringified-query key that allows for persisting the various payloads we use in stats for various time periods and start/end data combinations. In a similar fashion, the requesting sub-tree keeps track of what API requests are in-flight.

{
    items: {
      `siteId`: {
        `statType`: {
          `queryKey`: {} //API payload data
        }
      }
    },
    requesting: {
      `siteId`: {
        `statType`: {
          `queryKey`: `boolean`
        }
      }
    }
}

Selectors

In the stats-list all API payloads are passed through the stats-parser prior to being stored in localStorage. This allows us to massage the data and format it in such a way to be easily rendered by the stats components.

Some ways to handle this with redux would be to do a similar massage of the data in a component ( which felt odd to me ), or I took the approach here of creating a specific memoized selector like what exists in stats-list. In this branch I implemented getParsedStreakDataForQuery that displays this approach. This feels like a good solution to me due to the ease of testing, and the added value of memoizing the result for speed wins. But please chime in with any opinions here - or alternate approaches.

How To Test

This branch does not modify any existing behavior, it is just setting the stage for further redux greatness in calypso stats. Please refer to the tests in this branch to see the functionality and ensure the tests pass: npm run test-client client/state/stats

TODO

After this branch is in place a query component could be created, and my first area to migrate will be the stats post streak component on the insights page.

@mtias
Copy link
Member

mtias commented Jun 2, 2016

Ref: #5046

@timmyc timmyc added [Feature] Stats Everything related to our analytics product at /stats/ [Status] In Progress State labels Jun 2, 2016
@timmyc timmyc self-assigned this Jun 2, 2016
@timmyc timmyc force-pushed the update/stats/add-streak-redux branch 2 times, most recently from 5346e74 to 3d0fbdc Compare June 2, 2016 19:23
siteId,
query
} );

Copy link
Contributor Author

@timmyc timmyc Jun 2, 2016

Choose a reason for hiding this comment

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

Currently in the stats-list we have support for undocumented endpoints here, and instead of copying that, I'd rather migrate those endpoints to wpcom.js as we update all of stats to use redux.

Copy link
Contributor

Choose a reason for hiding this comment

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

Currently in the stats-list we have support for undocumented endpoints here, and instead of copying that, I'd rather migrate those endpoints to wpcom.js as we update all of stats to use redux.

Would this be a blocker for merging this pull request? Or move forward with it despite being unusable with the assumption that it'll be added to wpcom.js?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not a blocker at the moment. One of the undocumented endpoints is not even in use in Calypso. statsInsights is - but there is much work to do between here and there updating stats controller and components to get things working with Redux.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@timmyc timmyc force-pushed the update/stats/add-streak-redux branch from 3d0fbdc to e75cd4f Compare June 2, 2016 19:30
@timmyc timmyc added [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. and removed [Status] In Progress labels Jun 2, 2016
@timmyc
Copy link
Contributor Author

timmyc commented Jun 2, 2016

/cc @aduth would love your feedback on this one when you have some time!

} );

return wpcom.site( siteId )[ statType ]( query ).then( data => {
const payload = data;
Copy link
Contributor

Choose a reason for hiding this comment

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

Was there a reason for creating a separate variable, since the delete call on the line below mutates the original data anyways. Doesn't seem like it'd be an issue to mutate though, but could alternatively consider pulling in Lodash omit to strip the headers property.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call, I will go with omit

*/
export const getParsedStreakDataForQuery = createSelector(
( state, siteId, query ) => {
const response = { days: {}, best: { day: 0, percent: 0 }, 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.

I wonder if this would be a good opportunity to split the data we're constructing here into more granular selectors that can be composed together or used in more specific circumstances, e.g. getPostsStatsPerWeekday, getBestStatsWeekday, getPostsStatsWeekdayPercent, getStatsPostsWeekdayMax. Might also help readability of the code in splitting this into well-described functions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I like this approach, and I think currently the only bit of data actually used on the component would be getPostsStatsStreak and getStatsPostsStreakMax.

I did notice you don't have ForQuery in any of those - too verbose?

Copy link
Contributor

Choose a reason for hiding this comment

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

I did notice you don't have ForQuery in any of those - too verbose?

Didn't leave the suffix out intentionally. I don't think I'd mind either way, depending on whether it helps clarify its meaning.

@aduth aduth added [Status] Needs Author Reply and removed [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. labels Jun 6, 2016
@timmyc
Copy link
Contributor Author

timmyc commented Jun 6, 2016

@aduth in e8be85d I updated the test constants to use snake case and broke out the streak data selectors in to two explicit methods: getSiteStatsMaxPostsByDay, getSiteStatsPostStreakData. I'm keeping getSiteStats... as a prefix on all of these stats list selectors, but opting out of the ForQuery on some of these longer names.

@timmyc timmyc 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 Author Reply labels Jun 6, 2016
@aduth
Copy link
Contributor

aduth commented Jun 6, 2016

This looks good to me 👍

@aduth aduth added [Status] Ready to Merge and removed [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. labels Jun 6, 2016
@timmyc timmyc merged commit a7077a2 into master Jun 6, 2016
@mtias mtias deleted the update/stats/add-streak-redux branch June 8, 2016 12:13
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/ State
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants