* Add missing intervals to product reports

* Fix failing tests for product stats API

Before and after query should be within today's date since missing intervals for other days will be filled in with remaining dates.
This commit is contained in:
Joshua T Flowers 2018-12-31 13:20:22 +08:00 committed by GitHub
parent 029ff9cc9c
commit 7bbdd78bde
2 changed files with 44 additions and 22 deletions

View File

@ -115,27 +115,29 @@ class WC_Admin_Reports_Products_Stats_Data_Store extends WC_Admin_Reports_Produc
$intervals_query = array(); $intervals_query = array();
$this->update_sql_query_params( $query_args, $totals_query, $intervals_query ); $this->update_sql_query_params( $query_args, $totals_query, $intervals_query );
$db_records_count = (int) $wpdb->get_var( $db_intervals = $wpdb->get_col(
"SELECT COUNT(*) FROM ( "SELECT
SELECT {$intervals_query['select_clause']} AS time_interval
{$intervals_query['select_clause']} AS time_interval FROM
FROM {$table_name}
{$table_name} {$intervals_query['from_clause']}
{$intervals_query['from_clause']} WHERE
WHERE 1=1
1=1 {$intervals_query['where_time_clause']}
{$intervals_query['where_time_clause']} {$intervals_query['where_clause']}
{$intervals_query['where_clause']} GROUP BY
GROUP BY time_interval"
time_interval ); // WPCS: cache ok, DB call ok, , unprepared SQL ok.
) AS t"
); // WPCS: cache ok, DB call ok, unprepared SQL ok.
$total_pages = (int) ceil( $db_records_count / $intervals_query['per_page'] ); $db_interval_count = count( $db_intervals );
$expected_interval_count = WC_Admin_Reports_Interval::intervals_between( $query_args['after'], $query_args['before'], $query_args['interval'] );
$total_pages = (int) ceil( $expected_interval_count / $intervals_query['per_page'] );
if ( $query_args['page'] < 1 || $query_args['page'] > $total_pages ) { if ( $query_args['page'] < 1 || $query_args['page'] > $total_pages ) {
return array(); return array();
} }
$this->update_intervals_sql_params( $intervals_query, $query_args, $db_interval_count, $expected_interval_count );
$totals = $wpdb->get_results( $totals = $wpdb->get_results(
"SELECT "SELECT
{$selections} {$selections}
@ -183,21 +185,41 @@ class WC_Admin_Reports_Products_Stats_Data_Store extends WC_Admin_Reports_Produc
$totals = (object) $this->cast_numbers( $totals[0] ); $totals = (object) $this->cast_numbers( $totals[0] );
$this->update_interval_boundary_dates( $query_args['after'], $query_args['before'], $query_args['interval'], $intervals );
$this->create_interval_subtotals( $intervals );
$data = (object) array( $data = (object) array(
'totals' => $totals, 'totals' => $totals,
'intervals' => $intervals, 'intervals' => $intervals,
'total' => $db_records_count, 'total' => $expected_interval_count,
'pages' => $total_pages, 'pages' => $total_pages,
'page_no' => (int) $query_args['page'], 'page_no' => (int) $query_args['page'],
); );
if ( WC_Admin_Reports_Interval::intervals_missing( $expected_interval_count, $db_interval_count, $intervals_query['per_page'], $query_args['page'], $query_args['order'], $query_args['orderby'], count( $intervals ) ) ) {
$this->fill_in_missing_intervals( $db_intervals, $query_args['adj_after'], $query_args['adj_before'], $query_args['interval'], $data );
$this->sort_intervals( $data, $query_args['orderby'], $query_args['order'] );
$this->remove_extra_records( $data, $query_args['page'], $intervals_query['per_page'], $db_interval_count, $expected_interval_count, $query_args['orderby'] );
} else {
$this->update_interval_boundary_dates( $query_args['after'], $query_args['before'], $query_args['interval'], $data->intervals );
}
$this->create_interval_subtotals( $data->intervals );
wp_cache_set( $cache_key, $data, $this->cache_group ); wp_cache_set( $cache_key, $data, $this->cache_group );
} }
return $data; return $data;
} }
/**
* Normalizes order_by clause to match to SQL query.
*
* @param string $order_by Order by option requeste by user.
* @return string
*/
protected function normalize_order_by( $order_by ) {
if ( 'date' === $order_by ) {
return 'time_interval';
}
return $order_by;
}
} }

View File

@ -74,8 +74,8 @@ class WC_Tests_API_Reports_Products_Stats extends WC_REST_Unit_Test_Case {
$request = new WP_REST_Request( 'GET', $this->endpoint ); $request = new WP_REST_Request( 'GET', $this->endpoint );
$request->set_query_params( $request->set_query_params(
array( array(
'before' => date( 'Y-m-d H:00:00', $time + DAY_IN_SECONDS ), 'before' => date( 'Y-m-d 23:59:59', $time ),
'after' => date( 'Y-m-d H:00:00', $time - DAY_IN_SECONDS ), 'after' => date( 'Y-m-d 00:00:00', $time ),
'interval' => 'day', 'interval' => 'day',
) )
); );