From 732413502587c94945caa1f482460f9d8e5ac007 Mon Sep 17 00:00:00 2001 From: discomrade Date: Tue, 1 Feb 2022 03:31:40 -0100 Subject: [PATCH] Add PPH and IPs stats table (doesn't require JS) --- inc/mod/pages.php | 10 ++++- inc/statistics.php | 67 +++++++++++++++++++++++++++++++++ templates/mod/statistics.html | 52 +++++++++++++++++++++++++ tools/public_statistics_cli.php | 1 + 4 files changed, 128 insertions(+), 2 deletions(-) diff --git a/inc/mod/pages.php b/inc/mod/pages.php index bc7f6eb4..00f73ca1 100644 --- a/inc/mod/pages.php +++ b/inc/mod/pages.php @@ -3261,13 +3261,16 @@ function mod_view_statistics() { if(!hasPermission($config['mod']['view_statistics'])) error($config['error']['noaccess']); + $boards = listBoards(false); + // Get statistics from db $statistics_hour = Statistics::get_stat_24h(); $this_week = Statistics::get_stat_week(); $prev_week = Statistics::get_stat_week(true); mod_page(_('Statistics'), 'mod/statistics.html', array( - 'boards' => listBoards(false), + 'boards' => $boards, + 'statistics_table' => Statistics::getPostStatistics($boards), 'statistics_24h' => $statistics_hour, 'statistics_week_labels' => Statistics::get_stat_week_labels($this_week), 'statistics_week' => Statistics::get_stat_week_jsdata($this_week), @@ -3284,13 +3287,16 @@ function mod_view_board_statistics($boardName) { if (!openBoard($boardName)) error($config['error']['noboard']); + $boards = listBoards(false); + // Get statistics from db $statistics_hour = Statistics::get_stat_24h($boardName); $this_week = Statistics::get_stat_week(false, $boardName); $prev_week = Statistics::get_stat_week(true, $boardName); mod_page(_('Statistics for ') . $boardName, 'mod/statistics.html', array( - 'boards' => listBoards(false), + 'boards' => $boards, + 'statistics_table' => Statistics::getPostStatistics($boards), 'statistics_24h' => $statistics_hour, 'statistics_week_labels' => Statistics::get_stat_week_labels($this_week), 'statistics_week' => Statistics::get_stat_week_jsdata($this_week), diff --git a/inc/statistics.php b/inc/statistics.php index 30e26273..33aa9996 100644 --- a/inc/statistics.php +++ b/inc/statistics.php @@ -114,5 +114,72 @@ class Statistics { // Make string for JS return implode(",", $week_data); } + + static public function getPostStatistics($boards) { + global $config; + + if (!isset($config['boards'])) { + return null; + } + + $HOUR = 3600; + $DAY = $HOUR * 24; + $WEEK = $DAY * 7; + + $stats = []; + $hourly = []; + $daily = []; + $weekly = []; + + foreach ($boards as $board) { + if (!array_key_exists('uri', $board)) { + // board doesn't exist. + continue; + } + $_board = getBoardInfo($board['uri']); + if (!$_board) { + // board doesn't exist. + continue; + } + + $boardStat['title'] = $_board['title']; + + $boardStat['hourly_ips'] = Statistics::countUniqueIps($hourly, $HOUR, $_board); + $boardStat['daily_ips'] = Statistics::countUniqueIps($daily, $DAY, $_board); + $boardStat['weekly_ips'] = Statistics::countUniqueIps($weekly, $WEEK, $_board); + + $pph_query = query( + sprintf("SELECT COUNT(*) AS count FROM ``posts_%s`` WHERE time > %d", + $_board['uri'], + time()-3600) + ) or error(db_error()); + + $boardStat['pph'] = $pph_query->fetch()['count']; + + $stats['boards'][] = $boardStat; + } + + $stats['hourly_ips'] = count($hourly); + $stats['daily_ips'] = count($daily); + $stats['weekly_ips'] = count($weekly); + $stats['pph'] = array_sum(array_column($stats['boards'], 'pph')); + + return $stats; + } + + static private function countUniqueIps(&$markAsCounted, $timespan, $_board) { + $unique_query = query( + sprintf("SELECT DISTINCT ip FROM ``posts_%s`` WHERE time > %d", + $_board['uri'], + time()-$timespan) + ) or error(db_error()); + $uniqueIps = $unique_query->fetchAll(); + foreach ($uniqueIps as $_k => $row) { + $markAsCounted[$row['ip']] = true; + } + + return count($uniqueIps); + } } + ?> \ No newline at end of file diff --git a/templates/mod/statistics.html b/templates/mod/statistics.html index 699eae9d..798ef32e 100644 --- a/templates/mod/statistics.html +++ b/templates/mod/statistics.html @@ -5,6 +5,58 @@ +{% if statistics_table %} + + + + + + + + + + + + + + + + + + + {% for boardStat in statistics_table.boards %} + + + + + + + + {% endfor %} + +
{% trans "Board" %}{% trans "PPH" %}{% trans "IPs last hour" %}{% trans "IPs last day" %}{% trans "IPs last week" %}
+ {% trans "Total" %} + + {{ statistics_table.pph }} + + {{ statistics_table.hourly_ips }} + + {{ statistics_table.daily_ips }} + + {{ statistics_table.weekly_ips }} +
+ {{ boardStat.title }} + + {{ boardStat.pph }} + + {{ boardStat.hourly_ips }} + + {{ boardStat.daily_ips }} + + {{ boardStat.weekly_ips }} +
+{% endif %} + {% if (mod) or (public_hourly) %}

Hourly Statistics

diff --git a/tools/public_statistics_cli.php b/tools/public_statistics_cli.php index 86dd2cd1..885ccfc8 100644 --- a/tools/public_statistics_cli.php +++ b/tools/public_statistics_cli.php @@ -65,6 +65,7 @@ function statpage($board = false, $boards, $stat_file) { 'boards' => $boards, 'stat_filename' => $stat_file, 'public_hourly' => $config['public_stats']['hourly'], + 'statistics_table' => Statistics::getPostStatistics($boards), 'statistics_24h' => $statistics_hour, 'statistics_week_labels' => Statistics::get_stat_week_labels($this_week), 'statistics_week' => Statistics::get_stat_week_jsdata($this_week),