Skip to content

Commit

Permalink
adds fallback sorting for target/referrer with same count (#246) (#250)
Browse files Browse the repository at this point in the history
Add the URL as fallback ordering criterion if two entries in the top
lists have the same count, so it will produce a stable output.

Co-authored-by: Stefan Kalscheuer <stefan@stklcode.de>
  • Loading branch information
zottto and stklcode committed Mar 20, 2023
1 parent 206f106 commit c0e175e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
8 changes: 4 additions & 4 deletions inc/class-statify-dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,15 +332,15 @@ private static function _select_data() {
if ( $today ) {
$data['target'] = $wpdb->get_results(
$wpdb->prepare(
"SELECT COUNT(`target`) as `count`, `target` as `url` FROM `$wpdb->statify` WHERE created = %s GROUP BY `target` ORDER BY `count` DESC LIMIT %d",
"SELECT COUNT(`target`) as `count`, `target` as `url` FROM `$wpdb->statify` WHERE created = %s GROUP BY `target` ORDER BY `count` DESC, `url` ASC LIMIT %d",
$current_date,
$limit
),
ARRAY_A
);
$data['referrer'] = $wpdb->get_results(
$wpdb->prepare(
"SELECT COUNT(`referrer`) as `count`, `referrer` as `url`, SUBSTRING_INDEX(SUBSTRING_INDEX(TRIM(LEADING 'www.' FROM(TRIM(LEADING 'https://' FROM TRIM(LEADING 'http://' FROM TRIM(`referrer`))))), '/', 1), ':', 1) as `host` FROM `$wpdb->statify` WHERE `referrer` != '' AND created = %s GROUP BY `host` ORDER BY `count` DESC LIMIT %d",
"SELECT COUNT(`referrer`) as `count`, `referrer` as `url`, SUBSTRING_INDEX(SUBSTRING_INDEX(TRIM(LEADING 'www.' FROM(TRIM(LEADING 'https://' FROM TRIM(LEADING 'http://' FROM TRIM(`referrer`))))), '/', 1), ':', 1) as `host` FROM `$wpdb->statify` WHERE `referrer` != '' AND created = %s GROUP BY `host` ORDER BY `count` DESC, `url` ASC LIMIT %d",
$current_date,
$limit
),
Expand All @@ -349,7 +349,7 @@ private static function _select_data() {
} else {
$data['target'] = $wpdb->get_results(
$wpdb->prepare(
"SELECT COUNT(`target`) as `count`, `target` as `url` FROM `$wpdb->statify` WHERE created > DATE_SUB(%s, INTERVAL %d DAY) GROUP BY `target` ORDER BY `count` DESC LIMIT %d",
"SELECT COUNT(`target`) as `count`, `target` as `url` FROM `$wpdb->statify` WHERE created > DATE_SUB(%s, INTERVAL %d DAY) GROUP BY `target` ORDER BY `count` DESC, `url` ASC LIMIT %d",
$current_date,
$days_show,
$limit
Expand All @@ -358,7 +358,7 @@ private static function _select_data() {
);
$data['referrer'] = $wpdb->get_results(
$wpdb->prepare(
"SELECT COUNT(`referrer`) as `count`, `referrer` as `url`, SUBSTRING_INDEX(SUBSTRING_INDEX(TRIM(LEADING 'www.' FROM(TRIM(LEADING 'https://' FROM TRIM(LEADING 'http://' FROM TRIM(`referrer`))))), '/', 1), ':', 1) as `host` FROM `$wpdb->statify` WHERE `referrer` != '' AND created > DATE_SUB(%s, INTERVAL %d DAY) GROUP BY `host` ORDER BY `count` DESC LIMIT %d",
"SELECT COUNT(`referrer`) as `count`, `referrer` as `url`, SUBSTRING_INDEX(SUBSTRING_INDEX(TRIM(LEADING 'www.' FROM(TRIM(LEADING 'https://' FROM TRIM(LEADING 'http://' FROM TRIM(`referrer`))))), '/', 1), ':', 1) as `host` FROM `$wpdb->statify` WHERE `referrer` != '' AND created > DATE_SUB(%s, INTERVAL %d DAY) GROUP BY `host` ORDER BY `count` DESC, `url` ASC LIMIT %d",
$current_date,
$days_show,
$limit
Expand Down
14 changes: 14 additions & 0 deletions tests/test-dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,5 +256,19 @@ public function test_get_stats() {
$this->insert_test_data( $date1->format( 'Y-m-d' ), 'https://example.com/', '/example/', 1 );
$stats4 = Statify_Dashboard::get_stats();
$this->assertEquals( $stats3, $stats4, 'Stats expected to be equal, is the transient cache active?' );

// Add more data and force reload. We now should see that fallback ordering by URL works.
$this->insert_test_data( $date1->format( 'Y-m-d' ), 'https://example.com/', '/', 1 );
$this->insert_test_data( $date1->format( 'Y-m-d' ), 'https://example.net/', '/', 1 );
$stats5 = Statify_Dashboard::get_stats( true );
$this->assertEquals( 18, $stats5['visit_totals']['since_beginning']['count'], 'Unexpected total since beginning' );
$this->assertEquals( 2, $stats5['referrer'][1]['count'], 'Unexpected 2nd referrer count' );
$this->assertEquals( 'example.com', $stats5['referrer'][1]['host'], 'Unexpected 2nd referrer hostname' );
$this->assertEquals( 2, $stats5['referrer'][2]['count'], 'Unexpected 3rd referrer count' );
$this->assertEquals( 'pluginkollektiv.org', $stats5['referrer'][2]['host'], 'Unexpected 3rd referrer hostname' );
$this->assertEquals( 6, $stats5['target'][0]['count'], 'Unexpected 1st target count' );
$this->assertEquals( '/', $stats5['target'][0]['url'], 'Unexpected 1st target url' );
$this->assertEquals( 6, $stats5['target'][1]['count'], 'Unexpected 2nd target count' );
$this->assertEquals( '/test/', $stats5['target'][1]['url'], 'Unexpected 2nd target url' );
}
}

0 comments on commit c0e175e

Please sign in to comment.