Browse Source

Add PPH and IPs stats table (doesn't require JS)

main
discomrade 2 years ago
parent
commit
7324135025
  1. 10
      inc/mod/pages.php
  2. 67
      inc/statistics.php
  3. 52
      templates/mod/statistics.html
  4. 1
      tools/public_statistics_cli.php

10
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),

67
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);
}
}
?>

52
templates/mod/statistics.html

@ -5,6 +5,58 @@
<script src="{{ root }}js/chartist/chartist.min.js"></script>
<link rel='stylesheet' href='{{ root }}stylesheets/chartist/chartist.min.css'>
{% if statistics_table %}
<table style="margin:auto; padding: 20px 0px;">
<thead>
<tr>
<th style="padding: 0px 5px;">{% trans "Board" %}</th>
<th style="padding: 0px 5px;">{% trans "PPH" %}</th>
<th style="padding: 0px 5px;">{% trans "IPs last hour" %}</th>
<th style="padding: 0px 5px;">{% trans "IPs last day" %}</th>
<th style="padding: 0px 5px;">{% trans "IPs last week" %}</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<span>{% trans "Total" %}</span>
</td>
<td style="text-align: center;">
<span>{{ statistics_table.pph }}</span>
</td>
<td style="text-align: center;">
<span>{{ statistics_table.hourly_ips }}</span>
</td>
<td style="text-align: center;">
<span>{{ statistics_table.daily_ips }}</span>
</td>
<td style="text-align: center;">
<span>{{ statistics_table.weekly_ips }}</span>
</td>
</tr>
{% for boardStat in statistics_table.boards %}
<tr>
<td>
<span>{{ boardStat.title }}</span>
</td>
<td style="text-align: center;">
<span>{{ boardStat.pph }}</span>
</td>
<td style="text-align: center;">
<span>{{ boardStat.hourly_ips }}</span>
</td>
<td style="text-align: center;">
<span>{{ boardStat.daily_ips }}</span>
</td>
<td style="text-align: center;">
<span>{{ boardStat.weekly_ips }}</span>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
<noscript>The following are JavaScript graphs.</noscript>
{% if (mod) or (public_hourly) %}
<h2>Hourly Statistics</h2>
<section>

1
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),

Loading…
Cancel
Save