Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 9 additions & 27 deletions client/my-sites/stats/post-trends/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import PropTypes from 'prop-types';
import React from 'react';
import { connect } from 'react-redux';
import classNames from 'classnames';
import { throttle } from 'lodash';
import { max, throttle, values } from 'lodash';
import i18n, { localize } from 'i18n-calypso';

/**
Expand All @@ -21,11 +21,7 @@ import SectionHeader from 'components/section-header';
import QuerySiteStats from 'components/data/query-site-stats';
import { getSiteOption } from 'state/sites/selectors';
import { getSelectedSiteId } from 'state/ui/selectors';
import {
getSiteStatsPostStreakData,
getSiteStatsMaxPostsByDay,
getSiteStatsTotalPostsForStreakQuery,
} from 'state/stats/lists/selectors';
import { getSiteStatsPostStreakData } from 'state/stats/lists/selectors';

class PostTrends extends React.Component {
static displayName = 'PostTrends';
Expand Down Expand Up @@ -62,14 +58,6 @@ class PostTrends extends React.Component {
window.removeEventListener( 'resize', this.resize );
}

shouldComponentUpdate( nextProps ) {
// only update if the total number of posts, or query.endDate has changed
return (
nextProps.totalPosts !== this.props.totalPosts ||
nextProps.query.endDate !== this.props.query.endDate
);
}

resize = () => {
var scrollProps = {},
node = this.refs.wrapper,
Expand Down Expand Up @@ -127,22 +115,18 @@ class PostTrends extends React.Component {
};

getMonthComponents = () => {
var i,
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!

const maxPosts = max( values( streakData ) );
const months = [];

for ( i = 11; i >= 0; i-- ) {
startDate = i18n
for ( let i = 11; i >= 0; i-- ) {
const startDate = i18n
.moment()
.subtract( i, 'months' )
.startOf( 'month' );
months.push(
<Month
key={ startDate.format( 'YYYYMM' ) }
startDate={ startDate }
streakData={ this.props.streakData }
max={ this.props.max }
/>
<Month key={ i } startDate={ startDate } streakData={ streakData } max={ maxPosts } />
);
}

Expand Down Expand Up @@ -221,8 +205,6 @@ const mapStateToProps = state => {

return {
streakData: getSiteStatsPostStreakData( state, siteId, query ),
max: getSiteStatsMaxPostsByDay( state, siteId, query ),
totalPosts: getSiteStatsTotalPostsForStreakQuery( state, siteId, query ),
query,
siteId,
};
Expand Down
49 changes: 1 addition & 48 deletions client/state/stats/lists/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* External dependencies
*/

import { get, reduce, isArray, map, flatten } from 'lodash';
import { get, isArray, map, flatten } from 'lodash';
import i18n from 'i18n-calypso';

/**
Expand Down Expand Up @@ -97,53 +97,6 @@ export const getSiteStatsPostStreakData = treeSelect(
}
);

/**
* Returns a number representing the most posts made during a day for a given query
*
* @param {Object} state Global state tree
* @param {Number} siteId Site ID
* @param {Object} query Stats query object
* @return {?Number} Max number of posts by day
*/
export const getSiteStatsMaxPostsByDay = treeSelect(
( state, siteId, query ) => [ getSiteStatsPostStreakData( state, siteId, query ) ],
( [ postStreakData ] ) =>
reduce( postStreakData, ( max, count ) => ( count > max ? count : max ), 0 ) || null,
{
getCacheKey: ( siteId, query ) => [ siteId, getSerializedStatsQuery( query ) ].join(),
}
);

/**
* Returns the total number of posts per streak data for a query
*
* @param {Object} state Global state tree
* @param {Number} siteId Site ID
* @param {Object} query Stats query object
* @return {?Number} Max number of posts by day
*/
export const getSiteStatsTotalPostsForStreakQuery = treeSelect(
( state, siteId, query ) => [ getSiteStatsPostStreakData( state, siteId, query ) ],
( [ postStreakData ] ) => reduce( postStreakData, ( sum, posts ) => sum + posts, 0 ),
{
getCacheKey: ( siteId, query ) => [ siteId, getSerializedStatsQuery( query ) ].join(),
}
);

/**
* Returns a number representing the posts made during a day for a given query
*
* @param {Object} state Global state tree
* @param {Number} siteId Site ID
* @param {Object} query Stats query object
* @param {String} date Date in YYYY-MM-DD format
* @return {?Number} Number of posts made on date
*/
export function getSiteStatsPostsCountByDay( state, siteId, query, date ) {
const data = getSiteStatsPostStreakData( state, siteId, query );
return data[ date ] || null;
}

/**
* Returns normalized stats data for a given query and stat type, or the un-normalized response
* from the API if no normalizer method for that stats type exists in ./utils
Expand Down
172 changes: 0 additions & 172 deletions client/state/stats/lists/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@ import { expect } from 'chai';
* Internal dependencies
*/
import {
getSiteStatsMaxPostsByDay,
getSiteStatsForQuery,
getSiteStatsPostStreakData,
getSiteStatsPostsCountByDay,
getSiteStatsTotalPostsForStreakQuery,
getSiteStatsNormalizedData,
isRequestingSiteStatsForQuery,
getSiteStatsCSVData,
Expand All @@ -24,8 +21,6 @@ import { userState } from 'state/selectors/test/fixtures/user-state';
describe( 'selectors', () => {
beforeEach( () => {
getSiteStatsPostStreakData.clearCache();
getSiteStatsMaxPostsByDay.clearCache();
getSiteStatsTotalPostsForStreakQuery.clearCache();
getSiteStatsNormalizedData.clearCache();
} );

Expand Down Expand Up @@ -364,173 +359,6 @@ describe( 'selectors', () => {
} );
} );

describe( 'getSiteStatsTotalPostsForStreakQuery()', () => {
test( 'should return null if no matching query results exist', () => {
const stats = getSiteStatsTotalPostsForStreakQuery(
{
stats: {
lists: {
items: {},
},
},
},
2916284,
{}
);

expect( stats ).to.eql( 0 );
} );

test( 'should properly correct number of total posts', () => {
const stats = getSiteStatsTotalPostsForStreakQuery(
{
stats: {
lists: {
items: {
2916284: {
statsStreak: {
'[["endDate","2016-06-01"],["startDate","2015-06-01"]]': {
data: {
1461961382: 1,
1464110402: 1,
1464110448: 1,
},
},
},
},
},
},
},
},
2916284,
{ startDate: '2015-06-01', endDate: '2016-06-01' }
);

expect( stats ).to.eql( 3 );
} );
} );

describe( 'getSiteStatsMaxPostsByDay()', () => {
test( 'should return null if no matching query results exist', () => {
const stats = getSiteStatsMaxPostsByDay(
{
stats: {
lists: {
items: {},
},
},
},
2916284,
{}
);

expect( stats ).to.be.null;
} );

test( 'should properly correct number of max posts grouped by day', () => {
const stats = getSiteStatsMaxPostsByDay(
{
stats: {
lists: {
items: {
2916284: {
statsStreak: {
'[["endDate","2016-06-01"],["startDate","2015-06-01"]]': {
data: {
1461889800: 1, // 2016-04-29 00:30:00 (UTC)
1461972600: 1, // 2016-04-29 23:30:00 (UTC)
1462059000: 1, // 2016-04-30 23:30:00 (UTC)
},
},
},
},
},
},
},
},
2916284,
{ startDate: '2015-06-01', endDate: '2016-06-01' }
);

expect( stats ).to.eql( 2 );
} );

test( 'should compare numerically rather than lexically', () => {
const stats = getSiteStatsMaxPostsByDay(
{
stats: {
lists: {
items: {
2916284: {
statsStreak: {
'[["endDate","2016-06-01"],["startDate","2015-06-01"]]': {
data: {
1461889800: 12, // 2016-04-29 00:30:00 (UTC)
1462059000: 2, // 2016-04-30 23:30:00 (UTC)
},
},
},
},
},
},
},
},
2916284,
{ startDate: '2015-06-01', endDate: '2016-06-01' }
);

expect( stats ).to.eql( 12 );
} );
} );

describe( 'getSiteStatsPostsCountByDay()', () => {
test( 'should return null if no matching query results exist', () => {
const stats = getSiteStatsPostsCountByDay(
{
stats: {
lists: {
items: {},
},
},
},
2916284,
{},
'2016-06-01'
);

expect( stats ).to.be.null;
} );

test( 'should properly correct number of max posts for a day', () => {
const stats = getSiteStatsPostsCountByDay(
{
stats: {
lists: {
items: {
2916284: {
statsStreak: {
'[["endDate","2016-06-01"],["startDate","2015-06-01"]]': {
data: {
1461889800: 1, // 2016-04-29 00:30:00 (UTC)
1461972600: 1, // 2016-04-29 23:30:00 (UTC)
1462059000: 1, // 2016-04-30 23:30:00 (UTC)
},
},
},
},
},
},
},
},
2916284,
{ startDate: '2015-06-01', endDate: '2016-06-01' },
'2016-04-29'
);

expect( stats ).to.eql( 2 );
} );
} );

describe( 'getSiteStatsNormalizedData()', () => {
test( 'should return null if no matching query results exist', () => {
const stats = getSiteStatsNormalizedData(
Expand Down