Skip to content

Commit

Permalink
cast database results to int so we don't have to do it client-side
Browse files Browse the repository at this point in the history
  • Loading branch information
dannyvankooten committed May 17, 2021
1 parent 0439834 commit 6063d0a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
4 changes: 2 additions & 2 deletions assets/src/js/components/chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ export default class Chart extends Component {
continue
}

map[key].pageviews += parseInt(data[i].pageviews)
map[key].visitors += parseInt(data[i].visitors)
map[key].pageviews += data[i].pageviews
map[key].visitors += data[i].visitors

if (map[key].pageviews > yMax) {
yMax = map[key].pageviews
Expand Down
8 changes: 4 additions & 4 deletions assets/src/js/components/totals.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ export default class Totals extends Component {
}
}).then(data => {
data.forEach(r => {
visitors += parseInt(r.visitors)
pageviews += parseInt(r.pageviews)
visitors += r.visitors
pageviews += r.pageviews
})
}),

Expand All @@ -87,8 +87,8 @@ export default class Totals extends Component {
}
}).then(data => {
data.forEach(r => {
visitorsPrevious += parseInt(r.visitors)
pageviewsPrevious += parseInt(r.pageviews)
visitorsPrevious += r.visitors
pageviewsPrevious += r.pageviews
})
})
]).then(() => {
Expand Down
17 changes: 11 additions & 6 deletions src/class-rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ public function get_stats( \WP_REST_Request $request ) {
$end_date = isset( $params['end_date'] ) ? $params['end_date'] : gmdate( 'Y-m-d', time() + get_option( 'gmt_offset', 0 ) * HOUR_IN_SECONDS );
$sql = $wpdb->prepare( "SELECT date, visitors, pageviews FROM {$wpdb->prefix}koko_analytics_site_stats s WHERE s.date >= %s AND s.date <= %s", array( $start_date, $end_date ) );
$result = $wpdb->get_results( $sql );
return $result;
return is_array( $result ) ? array_map(function ( $row ) {
$row->pageviews = (int) $row->pageviews;
$row->visitors = (int) $row->visitors;
return $row;
}, $result) : $result;
}

public function get_posts( \WP_REST_Request $request ) {
Expand All @@ -131,11 +135,12 @@ public function get_posts( \WP_REST_Request $request ) {
}

// add permalink to each result
foreach ( $results as $i => $row ) {
$results[ $i ]->post_permalink = get_permalink( $row->id );
}

return $results;
return array_map( function( $row ) {
$row->post_permalink = get_permalink( $row->id );
$row->pageviews = (int) $row->pageviews;
$row->visitors = (int) $row->visitors;
return $row;
}, $results);
}

public function get_referrers( \WP_REST_Request $request ) {
Expand Down

0 comments on commit 6063d0a

Please sign in to comment.