Skip to content

Commit

Permalink
speed up Stats::get_totals by not automatically including totals for …
Browse files Browse the repository at this point in the history
…previous period, but explicitly calling the method again where needed. Relates to #271
  • Loading branch information
dannyvankooten committed Nov 18, 2024
1 parent a64f3e8 commit 0c488eb
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 48 deletions.
4 changes: 0 additions & 4 deletions migrations/1.0.20-unlink-custom-endpoint.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
// What we should have done in version 1.0.20

// Unlink the custom endpoint file to ensure we get the latest logic for determining whether to use this
// Supporess warnings because it may not be there (and that is fine)
@unlink(ABSPATH . '/koko-analytics-collect.php');
if (file_exists(ABSPATH . '/koko-analytics-collect.php')) {
unlink(ABSPATH . '/koko-analytics-collect.php');
}

// Update option that says to use custom endpoint, this will be recalculated the next time logic for custom endpoint runs
update_option('koko_analytics_use_custom_endpoint', false);
4 changes: 4 additions & 0 deletions src/class-dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,17 @@ public function show(): void
$dateStart = $dateStart ?? $dateRange[0];
$dateEnd = isset($_GET['end_date']) ? create_local_datetime($_GET['end_date']) : $dateRange[1];
$dateEnd = $dateEnd ?? $dateRange[1];
$nextDates = $this->get_next_period($dateStart, $dateEnd, 1);
$prevDates = $this->get_next_period($dateStart, $dateEnd, -1);

$posts_offset = isset($_GET['posts']['offset']) ? absint($_GET['posts']['offset']) : 0;
$referrers_offset = isset($_GET['referrers']['offset']) ? absint($_GET['referrers']['offset']) : 0;
$posts_limit = isset($_GET['posts']['limit']) ? absint($_GET['posts']['limit']) : $items_per_page;
$referrers_limit = isset($_GET['referrers']['limit']) ? absint($_GET['referrers']['limit']) : $items_per_page;

$totals = $stats->get_totals($dateStart->format('Y-m-d'), $dateEnd->format('Y-m-d'), $page);
$totals_previous = $stats->get_totals($prevDates[0]->format('Y-m-d'), $prevDates[1]->format('Y-m-d'), $page);

$posts = $stats->get_posts($dateStart->format("Y-m-d"), $dateEnd->format('Y-m-d'), $posts_offset, $posts_limit);
$posts_count = $stats->count_posts($dateStart->format('Y-m-d'), $dateEnd->format('Y-m-d'));
$referrers = $stats->get_referrers($dateStart->format("Y-m-d"), $dateEnd->format('Y-m-d'), $referrers_offset, $referrers_limit);
Expand Down
49 changes: 19 additions & 30 deletions src/class-stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,58 +10,47 @@

class Stats
{
public function get_totals(string $start_date, string $end_date, int $page = 0): ?object
/**
* @return object {
* @type int visitors
* @type int pageviews
* }
*/
public function get_totals(string $start_date, string $end_date, int $page = 0, $include_previous = true): object
{
global $wpdb;

$previous_start_date = gmdate('Y-m-d', strtotime($start_date) - (strtotime($end_date . ' 23:59:59') - strtotime($start_date)));

$table = $wpdb->prefix . 'koko_analytics_site_stats';
$where_a = 's.date >= %s AND s.date <= %s';
$args_a = array($start_date, $end_date);
$where_b = 's.date >= %s AND s.date < %s';
$args_b = array($previous_start_date, $start_date);
$table = $wpdb->prefix . 'koko_analytics_site_stats s';
$where = 's.date >= %s AND s.date <= %s';
$args = array($start_date, $end_date);

if ($page > 0) {
$table = $wpdb->prefix . 'koko_analytics_post_stats';
$where_a .= ' AND s.id = %d';
$where_b .= ' AND s.id = %d';
$args_a[] = $page;
$args_b[] = $page;
$table = $wpdb->prefix . 'koko_analytics_post_stats s';
$where .= ' AND s.id = %d';
$args[] = $page;
}

$sql = $wpdb->prepare("SELECT
cur.*,
prev.visitors AS prev_visitors,
cur.visitors - prev.visitors AS visitors_change,
cur.pageviews - prev.pageviews AS pageviews_change,
cur.visitors / prev.visitors - 1 AS visitors_change_rel,
cur.pageviews / prev.pageviews - 1 AS pageviews_change_rel
FROM
(SELECT COALESCE(SUM(visitors), 0) AS visitors, COALESCE(SUM(pageviews), 0) AS pageviews FROM {$table} s WHERE $where_a) AS cur,
(SELECT COALESCE(SUM(visitors), 0) AS visitors, COALESCE(SUM(pageviews), 0) AS pageviews FROM {$table} s WHERE $where_b) AS prev;
", array_merge($args_a, $args_b));
$sql = $wpdb->prepare("
SELECT COALESCE(SUM(visitors), 0) AS visitors, COALESCE(SUM(pageviews), 0) AS pageviews
FROM {$table}
WHERE {$where}
", $args);
$result = $wpdb->get_row($sql);

// ensure we always return a valid object containing the keys we need
if (!$result) {
return (object) [
'pageviews' => 0,
'pageviews_change' => 0,
'pageviews_change_rel' => 0,
'visitors' => 0,
'visitors_change' => 0,
'visitors_change_rel' => 0,
];
}

// sometimes there are pageviews, but no counted visitors
// this happens when the cookie was valid over a period of 2 calendar days
// we can make this less obviously wrong by always specifying there was at least 1 visitors
// whenever we have any pageviews
if ($result && $result->pageviews > 0 && $result->visitors == 0) {
if ($result->visitors == 0 && $result->pageviews > 0) {
$result->visitors = 1;
$result->visitors_change += $result->visitors_change > 0 ? -1 : 1;
}

return $result;
Expand Down
35 changes: 23 additions & 12 deletions src/views/dashboard-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,42 +95,53 @@
</div>
<table id="ka-totals" class='ka-totals m'>
<tbody>
<tr class="<?php echo $totals->visitors_change > 0 ? 'ka-up' : ''; ?> <?php echo $totals->visitors_change < 0 ? 'ka-down' : ''; ?>">

<?php
/* Total visitors */
$diff = $totals->visitors - $totals_previous->visitors;
$change = $totals_previous->visitors === 0 ? 0 : $totals->visitors / $totals_previous->visitors - 1;
?>
<tr class="<?php echo $diff > 0 ? 'ka-up' : ''; ?> <?php echo $diff < 0 ? 'ka-down' : ''; ?>">
<th><?php echo esc_html__('Total visitors', 'koko-analytics'); ?></th>
<td class='ka-totals--amount'>
<span title="<?php echo esc_attr($totals->visitors); ?>"><?php echo number_format_i18n($totals->visitors); ?></span>
<span class="ka-totals--change">
<?php echo percent_format_i18n($totals->visitors_change_rel); ?>
<?php echo percent_format_i18n($change); ?>
</span>
</td>
<td class='ka-totals--subtext'>
<?php if ($totals->visitors_change != 0) {
?><span><?php echo number_format_i18n(abs($totals->visitors_change)); ?></span><?php
<?php if ($diff != 0) {
?><span><?php echo number_format_i18n(abs($diff)); ?></span><?php
} ?>
<?php if ($totals->visitors_change > 0) {
<?php if ($diff > 0) {
?> <span class="ka-totals--subtext-up"><?php echo esc_html__('more than previous period', 'koko-analytics'); ?></span><?php
} ?>
<?php if ($totals->visitors_change < 0) {
<?php if ($diff < 0) {
?><span class="ka-totals--subtext-down"><?php echo esc_html__('less than previous period', 'koko-analytics'); ?></span><?php
} ?>
</td>
</tr>
<tr class="<?php echo $totals->pageviews_change > 0 ? 'ka-up' : ''; ?> <?php echo $totals->pageviews_change < 0 ? 'ka-down' : ''; ?>">
<?php
/* Total pageviews */
$diff = $totals->pageviews - $totals_previous->pageviews;
$change = $totals_previous->pageviews === 0 ? 0 : $totals->pageviews / $totals_previous->pageviews - 1;
?>
<tr class="<?php echo $diff > 0 ? 'ka-up' : ''; ?> <?php echo $diff < 0 ? 'ka-down' : ''; ?>">
<th><?php echo esc_html__('Total pageviews', 'koko-analytics'); ?></th>
<td class='ka-totals--amount'>
<span title="<?php echo esc_attr($totals->pageviews); ?>"><?php echo number_format_i18n($totals->pageviews); ?></span>
<span class="ka-totals--change">
<?php echo percent_format_i18n($totals->pageviews_change_rel); ?>
<?php echo percent_format_i18n($change); ?>
</span>
</td>
<td class='ka-totals--subtext'>
<?php if ($totals->pageviews_change != 0) {
?><span><?php echo number_format_i18n(abs($totals->pageviews_change)); ?></span><?php
<?php if ($diff != 0) {
?><span><?php echo number_format_i18n(abs($diff)); ?></span><?php
} ?>
<?php if ($totals->pageviews_change > 0) {
<?php if ($diff > 0) {
?><span class="ka-totals--subtext-up"><?php echo esc_html__('more than previous period', 'koko-analytics'); ?></span><?php
} ?>
<?php if ($totals->pageviews_change < 0) {
<?php if ($diff < 0) {
?><span class="ka-totals--subtext-down"><?php echo esc_html__('less than previous period', 'koko-analytics'); ?></span><?php
} ?>
</td>
Expand Down

0 comments on commit 0c488eb

Please sign in to comment.