2018-09-03 15:25:38 +00:00
|
|
|
/** @format */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2018-10-18 09:34:37 +00:00
|
|
|
import { find, forEach, isNull } from 'lodash';
|
2018-09-03 15:25:38 +00:00
|
|
|
|
2018-10-30 18:57:48 +00:00
|
|
|
/**
|
|
|
|
* WooCommerce dependencies
|
|
|
|
*/
|
|
|
|
import { appendTimestamp, getCurrentDates, getIntervalForQuery } from '@woocommerce/date';
|
|
|
|
|
2018-09-03 15:25:38 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { MAX_PER_PAGE } from 'store/constants';
|
2018-10-18 09:34:37 +00:00
|
|
|
import { getActiveFiltersFromQuery, getUrlKey } from 'components/filters/advanced/utils';
|
|
|
|
import { flatenFilters } from 'components/filters/filter/utils';
|
|
|
|
import * as couponsConfig from 'analytics/report/coupons/config';
|
|
|
|
import * as ordersConfig from 'analytics/report/orders/config';
|
|
|
|
import * as productsConfig from 'analytics/report/products/config';
|
|
|
|
|
|
|
|
const reportConfigs = {
|
|
|
|
coupons: couponsConfig,
|
|
|
|
orders: ordersConfig,
|
|
|
|
products: productsConfig,
|
|
|
|
};
|
|
|
|
|
|
|
|
export function getFilterQuery( endpoint, query ) {
|
2018-10-29 01:30:24 +00:00
|
|
|
if ( reportConfigs[ endpoint ] ) {
|
|
|
|
const { filters = [], advancedFilters = {} } = reportConfigs[ endpoint ];
|
|
|
|
return filters
|
|
|
|
.map( filter => getQueryFromConfig( filter, advancedFilters, query ) )
|
|
|
|
.reduce( ( result, configQuery ) => Object.assign( result, configQuery ), {} );
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
2018-10-18 09:34:37 +00:00
|
|
|
|
2018-10-29 01:30:24 +00:00
|
|
|
export function getQueryFromConfig( config, advancedFilters, query ) {
|
|
|
|
const queryValue = query[ config.param ];
|
|
|
|
|
|
|
|
if ( ! queryValue ) {
|
2018-10-18 09:34:37 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2018-10-29 01:30:24 +00:00
|
|
|
if ( 'advanced' === queryValue ) {
|
2018-10-18 09:34:37 +00:00
|
|
|
const activeFilters = getActiveFiltersFromQuery( query, advancedFilters.filters );
|
|
|
|
|
|
|
|
if ( activeFilters.length === 0 ) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return activeFilters.reduce(
|
|
|
|
( result, activeFilter ) => {
|
|
|
|
const { key, rule, value } = activeFilter;
|
|
|
|
result[ getUrlKey( key, rule ) ] = value;
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
{ match: query.match || 'all' }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-10-29 01:30:24 +00:00
|
|
|
const filter = find( flatenFilters( config.filters ), { value: queryValue } );
|
2018-10-18 09:34:37 +00:00
|
|
|
|
2018-10-29 01:30:24 +00:00
|
|
|
if ( ! filter ) {
|
2018-10-18 09:34:37 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2018-10-29 01:30:24 +00:00
|
|
|
if ( filter.settings && filter.settings.param ) {
|
|
|
|
const { param } = filter.settings;
|
2018-10-18 09:34:37 +00:00
|
|
|
|
|
|
|
if ( query[ param ] ) {
|
|
|
|
return {
|
|
|
|
[ param ]: query[ param ],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
2018-09-03 15:25:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if a report object is empty.
|
|
|
|
*
|
|
|
|
* @param {Object} report Report to check
|
|
|
|
* @return {Boolean} True if report is data is empty.
|
|
|
|
*/
|
|
|
|
export function isReportDataEmpty( report ) {
|
|
|
|
if ( ! report ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if ( ! report.data ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if ( ! report.data.totals || isNull( report.data.totals ) ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if ( ! report.data.intervals || 0 === report.data.intervals.length ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-17 23:06:33 +00:00
|
|
|
/**
|
|
|
|
* Constructs and returns a query associated with a Report data request.
|
|
|
|
*
|
2018-10-18 09:34:37 +00:00
|
|
|
* @param {String} endpoint Report API Endpoint
|
2018-10-17 23:06:33 +00:00
|
|
|
* @param {String} dataType 'primary' or 'secondary'.
|
|
|
|
* @param {Object} query query parameters in the url.
|
|
|
|
* @returns {Object} data request query parameters.
|
|
|
|
*/
|
2018-10-18 09:34:37 +00:00
|
|
|
function getRequestQuery( endpoint, dataType, query ) {
|
2018-10-17 23:02:31 +00:00
|
|
|
const datesFromQuery = getCurrentDates( query );
|
|
|
|
const interval = getIntervalForQuery( query );
|
2018-10-18 09:34:37 +00:00
|
|
|
const filterQuery = getFilterQuery( endpoint, query );
|
2018-10-17 23:02:31 +00:00
|
|
|
return {
|
|
|
|
order: 'asc',
|
|
|
|
interval,
|
|
|
|
per_page: MAX_PER_PAGE,
|
|
|
|
after: appendTimestamp( datesFromQuery[ dataType ].after, 'start' ),
|
|
|
|
before: appendTimestamp( datesFromQuery[ dataType ].before, 'end' ),
|
2018-10-18 09:34:37 +00:00
|
|
|
...filterQuery,
|
2018-10-17 23:02:31 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-10-11 18:45:01 +00:00
|
|
|
/**
|
|
|
|
* Returns summary number totals needed to render a report page.
|
|
|
|
*
|
|
|
|
* @param {String} endpoint Report API Endpoint
|
2018-10-17 23:02:31 +00:00
|
|
|
* @param {Object} query query parameters in the url
|
2018-10-11 18:45:01 +00:00
|
|
|
* @param {object} select Instance of @wordpress/select
|
|
|
|
* @return {Object} Object containing summary number responses.
|
|
|
|
*/
|
2018-10-17 23:02:31 +00:00
|
|
|
export function getSummaryNumbers( endpoint, query, select ) {
|
2018-10-11 18:45:01 +00:00
|
|
|
const { getReportStats, isReportStatsRequesting, isReportStatsError } = select( 'wc-admin' );
|
|
|
|
const response = {
|
|
|
|
isRequesting: false,
|
|
|
|
isError: false,
|
|
|
|
totals: {
|
|
|
|
primary: null,
|
|
|
|
secondary: null,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2018-10-18 09:34:37 +00:00
|
|
|
const primaryQuery = getRequestQuery( endpoint, 'primary', query );
|
2018-10-11 18:45:01 +00:00
|
|
|
const primary = getReportStats( endpoint, primaryQuery );
|
|
|
|
if ( isReportStatsRequesting( endpoint, primaryQuery ) ) {
|
|
|
|
return { ...response, isRequesting: true };
|
|
|
|
} else if ( isReportStatsError( endpoint, primaryQuery ) ) {
|
|
|
|
return { ...response, isError: true };
|
|
|
|
}
|
|
|
|
|
|
|
|
const primaryTotals = ( primary && primary.data && primary.data.totals ) || null;
|
|
|
|
|
2018-10-18 09:34:37 +00:00
|
|
|
const secondaryQuery = getRequestQuery( endpoint, 'secondary', query );
|
2018-10-11 18:45:01 +00:00
|
|
|
const secondary = getReportStats( endpoint, secondaryQuery );
|
|
|
|
if ( isReportStatsRequesting( endpoint, secondaryQuery ) ) {
|
|
|
|
return { ...response, isRequesting: true };
|
|
|
|
} else if ( isReportStatsError( endpoint, secondaryQuery ) ) {
|
|
|
|
return { ...response, isError: true };
|
|
|
|
}
|
|
|
|
|
|
|
|
const secondaryTotals = ( secondary && secondary.data && secondary.data.totals ) || null;
|
|
|
|
|
|
|
|
return { ...response, totals: { primary: primaryTotals, secondary: secondaryTotals } };
|
|
|
|
}
|
|
|
|
|
2018-09-03 15:25:38 +00:00
|
|
|
/**
|
|
|
|
* Returns all of the data needed to render a chart with summary numbers on a report page.
|
|
|
|
*
|
|
|
|
* @param {String} endpoint Report API Endpoint
|
2018-10-17 23:02:31 +00:00
|
|
|
* @param {String} dataType 'primary' or 'secondary'
|
|
|
|
* @param {Object} query query parameters in the url
|
2018-09-03 15:25:38 +00:00
|
|
|
* @param {object} select Instance of @wordpress/select
|
|
|
|
* @return {Object} Object containing API request information (response, fetching, and error details)
|
|
|
|
*/
|
2018-10-17 23:02:31 +00:00
|
|
|
export function getReportChartData( endpoint, dataType, query, select ) {
|
2018-09-03 15:25:38 +00:00
|
|
|
const { getReportStats, isReportStatsRequesting, isReportStatsError } = select( 'wc-admin' );
|
|
|
|
|
|
|
|
const response = {
|
Add loading indicators, error state, and EmptyContent to the revenue report. (#347, woocommerce/woocommerce-admin#348)
* Add loading indiciators for the revenue report.
* Improve accessibility, and fix up some documentation comments.
* Fix top border on mobile
* Add EmptyContent Component and revenue error/empty states. (https://github.com/woocommerce/woocommerce-admin/pull/348)
* Add EmptyContent Component and revenue error/empty states.
* Move relative image handling to ImageAsset, combine secondary and primary action rendering, add some missing isRequired proptypes, add empty error handling.
* Handle PR Feedback: Clean up button css, set a default for illustration, fix deprecation typo, some code cleanup.
2018-09-05 16:45:49 +00:00
|
|
|
isEmpty: false,
|
2018-09-03 15:25:38 +00:00
|
|
|
isError: false,
|
|
|
|
isRequesting: false,
|
|
|
|
data: {
|
|
|
|
totals: null,
|
|
|
|
intervals: [],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2018-10-18 09:34:37 +00:00
|
|
|
const requestQuery = getRequestQuery( endpoint, dataType, query );
|
2018-10-17 23:02:31 +00:00
|
|
|
const stats = getReportStats( endpoint, requestQuery );
|
2018-09-03 15:25:38 +00:00
|
|
|
|
2018-10-17 23:02:31 +00:00
|
|
|
if ( isReportStatsRequesting( endpoint, requestQuery ) ) {
|
2018-09-03 15:25:38 +00:00
|
|
|
return { ...response, isRequesting: true };
|
2018-10-17 23:02:31 +00:00
|
|
|
} else if ( isReportStatsError( endpoint, requestQuery ) ) {
|
2018-09-03 15:25:38 +00:00
|
|
|
return { ...response, isError: true };
|
Add loading indicators, error state, and EmptyContent to the revenue report. (#347, woocommerce/woocommerce-admin#348)
* Add loading indiciators for the revenue report.
* Improve accessibility, and fix up some documentation comments.
* Fix top border on mobile
* Add EmptyContent Component and revenue error/empty states. (https://github.com/woocommerce/woocommerce-admin/pull/348)
* Add EmptyContent Component and revenue error/empty states.
* Move relative image handling to ImageAsset, combine secondary and primary action rendering, add some missing isRequired proptypes, add empty error handling.
* Handle PR Feedback: Clean up button css, set a default for illustration, fix deprecation typo, some code cleanup.
2018-09-05 16:45:49 +00:00
|
|
|
} else if ( isReportDataEmpty( stats ) ) {
|
|
|
|
return { ...response, isEmpty: true };
|
2018-09-03 15:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const totals = ( stats && stats.data && stats.data.totals ) || null;
|
|
|
|
let intervals = ( stats && stats.data && stats.data.intervals ) || [];
|
|
|
|
|
|
|
|
// If we have more than 100 results for this time period,
|
|
|
|
// we need to make additional requests to complete the response.
|
|
|
|
if ( stats.totalResults > MAX_PER_PAGE ) {
|
|
|
|
let isFetching = true;
|
|
|
|
let isError = false;
|
|
|
|
const pagedData = [];
|
|
|
|
const totalPages = Math.ceil( stats.totalResults / MAX_PER_PAGE );
|
|
|
|
|
|
|
|
for ( let i = 2; i <= totalPages; i++ ) {
|
2018-10-17 23:02:31 +00:00
|
|
|
const nextQuery = { ...requestQuery, page: i };
|
|
|
|
const _data = getReportStats( endpoint, nextQuery );
|
|
|
|
if ( isReportStatsRequesting( endpoint, nextQuery ) ) {
|
2018-09-03 15:25:38 +00:00
|
|
|
continue;
|
|
|
|
}
|
2018-10-17 23:02:31 +00:00
|
|
|
if ( isReportStatsError( endpoint, nextQuery ) ) {
|
2018-09-03 15:25:38 +00:00
|
|
|
isError = true;
|
|
|
|
isFetching = false;
|
|
|
|
break;
|
|
|
|
}
|
2018-10-17 23:02:31 +00:00
|
|
|
if ( ! isReportStatsRequesting( endpoint, nextQuery ) ) {
|
2018-09-03 15:25:38 +00:00
|
|
|
pagedData.push( _data );
|
|
|
|
if ( i === totalPages ) {
|
|
|
|
isFetching = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( isFetching ) {
|
|
|
|
return { ...response, isRequesting: true };
|
|
|
|
} else if ( isError ) {
|
|
|
|
return { ...response, isError: true };
|
|
|
|
}
|
|
|
|
|
|
|
|
forEach( pagedData, function( _data ) {
|
|
|
|
intervals = intervals.concat( _data.data.intervals );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
return { ...response, data: { totals, intervals } };
|
|
|
|
}
|