Browse Source

Added Feature - Statistics

main
PupperWoff 7 years ago
committed by discomrade
parent
commit
2b05fd12f4
  1. 1
      composer.json
  2. 29
      inc/config.php
  3. 43
      inc/mod/pages.php
  4. 118
      inc/statistics.php
  5. 10
      js/chartist/chartist.min.js
  6. 3
      mod.php
  7. 1
      stylesheets/chartist/chartist.min.css
  8. 3
      templates/mod/dashboard.html
  9. 77
      templates/mod/statistics.html
  10. 85
      tools/public_statistics_cli.php

1
composer.json

@ -29,6 +29,7 @@
"inc/announcements.php",
"inc/archive.php",
"inc/shadow-delete.php",
"inc/statistics.php",
"inc/functions.php"
]
},

29
inc/config.php

@ -1801,6 +1801,8 @@
$config['mod']['ban_appeals'] = MOD;
// View the recent posts page
$config['mod']['recent'] = MOD;
// View site statistics
$config['mod']['view_statistics'] = MOD;
// Create pages
$config['mod']['edit_pages'] = MOD;
$config['pages_max'] = 10;
@ -1949,6 +1951,33 @@
// Please set this setting in your board/config.php, not globally.
$config['nntpchan']['group'] = false; // eg. 'overchan.test'
/*
* ====================
* Public Statistics settings
* ====================
*/
// The public stats page is not generated automatically.
// It is recommended you create a cron job to run
// tools/public_statistics_cli.php statistics.html
// set to the times you want it updated, such as once every hour or once every day.
// Alternatively, if your imageboard has low traffic, you can make it run from a Theme
// defined to update every time rebuildThemes('post') is run.
// Set to true if all boards are to be shown
// Set to false to use $config['boards'] to select boards to show in stats
// Set to array of boards if only boards listed in this array are to be shown
$config['public_stats']['boards'] = true;
// Include hourly stats in public stats
// (need to run tools/public_statistics_cli.php once an hour to update data)
$config['public_stats']['hourly'] = false;
// Include the current hour in public stats.
$config['public_stats']['realtime'] = false;
// The format string passed to strftime() for displaying dates.
// http://www.php.net/manual/en/function.strftime.php
//$config['public_stat']['date'] = '%m/%d/%y (%a) %l %P';
$config['public_stats']['date'] = '<time datetime="%Y-%m-%dT%H:%M:%SZ">%Y-%m-%dT%H:%M:%S</time>';
/*
* ====================
* Other/uncategorized

43
inc/mod/pages.php

@ -3257,6 +3257,49 @@ function mod_report_dismiss($id, $all = false) {
}
}
function mod_view_statistics() {
global $config, $pdo;
if(!hasPermission($config['mod']['view_statistics']))
error($config['error']['noaccess']);
// 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),
'statistics_24h' => $statistics_hour,
'statistics_week_labels' => Statistics::get_stat_week_labels($this_week),
'statistics_week' => Statistics::get_stat_week_jsdata($this_week),
'statistics_week_past' => Statistics::get_stat_week_jsdata($prev_week)
));
}
function mod_view_board_statistics($boardName) {
global $config, $pdo;
if(!hasPermission($config['mod']['view_statistics']))
error($config['error']['noaccess']);
if (!openBoard($boardName))
error($config['error']['noboard']);
// 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),
'statistics_24h' => $statistics_hour,
'statistics_week_labels' => Statistics::get_stat_week_labels($this_week),
'statistics_week' => Statistics::get_stat_week_jsdata($this_week),
'statistics_week_past' => Statistics::get_stat_week_jsdata($prev_week)
));
}
function mod_config($board_config = false) {
global $config, $mod, $board;

118
inc/statistics.php

@ -0,0 +1,118 @@
<?php
class Statistics {
static public function get_stat_24h($boardName = false, $realtime = true, $boards = false) {
global $config, $pdo;
$query = "";
if(!$boardName) {
// Get list of all boards
if($boards === false)
$boards = listBoards();
// Get post count by hour for the last day
$query = "SELECT SUM(count) AS count, hour FROM (";
foreach ($boards as $board) {
if($realtime)
$query .= sprintf("SELECT COUNT(*) AS count, HOUR(FROM_UNIXTIME(time)) AS hour FROM posts_%s WHERE DATE(FROM_UNIXTIME(time)) = CURDATE() GROUP BY hour UNION ALL ", $board['uri']);
else
$query .= sprintf("SELECT COUNT(*) AS count, HOUR(FROM_UNIXTIME(time)) AS hour FROM posts_%s WHERE DATE(FROM_UNIXTIME(time)) = DATE(NOW() - INTERVAL 1 HOUR) AND HOUR(FROM_UNIXTIME(time)) <= HOUR(NOW() - INTERVAL 1 HOUR) GROUP BY hour UNION ALL ", $board['uri']);
}
// Remove the last "UNION ALL" seperator and complete the query
$query = preg_replace('/UNION ALL $/', ') AS deriv_all GROUP BY hour ORDER BY hour ASC', $query);
} else {
if($realtime)
$query = sprintf("SELECT COUNT(*) AS count, HOUR(FROM_UNIXTIME(time)) AS hour FROM posts_%s WHERE DATE(FROM_UNIXTIME(time)) = CURDATE() GROUP BY hour", $boardName);
else
$query = sprintf("SELECT COUNT(*) AS count, HOUR(FROM_UNIXTIME(time)) AS hour FROM posts_%s WHERE DATE(FROM_UNIXTIME(time)) = DATE(NOW() - INTERVAL 1 HOUR) AND HOUR(FROM_UNIXTIME(time)) <= HOUR(NOW() - INTERVAL 1 HOUR) GROUP BY hour", $boardName);
}
$query = query($query) or error(db_error($query));
$query_result = $query->fetchAll(PDO::FETCH_ASSOC);
// Get 24h array over post count
$statistics_hour = array_fill(0,24,0);
foreach ($query_result as &$hour_data) {
$statistics_hour[$hour_data['hour']] = $hour_data['count'];
}
// Set last variables to 'null' for JavaScript
$last_hour = end($query_result)['hour'];
if($last_hour != 23)
for($i=$last_hour+1; $i<24; $i++)
$statistics_hour[$i] = 'null';
// Make string for JS
$statistics_hour = implode(",", $statistics_hour);
return $statistics_hour;
}
static public function get_stat_week($previous_week = false, $boardName = false, $realtime = true, $hour_realtime = true, $boards = false) {
global $config, $pdo;
$query = "";
if(!$boardName) {
// Get list of all boards
if($boards === false)
$boards = listBoards();
// Get post count by hour for the last week
$query = "SELECT SUM(count) AS count, day FROM (";
foreach ($boards as $board) {
if($previous_week) {
if($realtime)
$query .= sprintf("SELECT COUNT(*) AS count, WEEKDAY(FROM_UNIXTIME(time)) AS day FROM posts_%s WHERE YEARWEEK(FROM_UNIXTIME(time), 1) = YEARWEEK(DATE_SUB(NOW(), INTERVAL 1 WEEK), 1) GROUP BY day UNION ALL ", $board['uri']);
else if($hour_realtime)
$query .= sprintf("SELECT COUNT(*) AS count, WEEKDAY(FROM_UNIXTIME(time)) AS day FROM posts_%s WHERE YEARWEEK(FROM_UNIXTIME(time), 1) = YEARWEEK(DATE_SUB(NOW() - INTERVAL 1 HOUR, INTERVAL 1 WEEK), 1) GROUP BY day UNION ALL ", $board['uri']);
else
$query .= sprintf("SELECT COUNT(*) AS count, WEEKDAY(FROM_UNIXTIME(time)) AS day FROM posts_%s WHERE YEARWEEK(FROM_UNIXTIME(time), 1) = YEARWEEK(DATE_SUB(NOW() - INTERVAL 1 DAY, INTERVAL 1 WEEK), 1) GROUP BY day UNION ALL ", $board['uri']);
} else {
if($realtime)
$query .= sprintf("SELECT COUNT(*) AS count, WEEKDAY(FROM_UNIXTIME(time)) AS day FROM posts_%s WHERE YEARWEEK(FROM_UNIXTIME(time), 1) = YEARWEEK(NOW(), 1) GROUP BY day UNION ALL ", $board['uri']);
else if($hour_realtime)
$query .= sprintf("SELECT COUNT(*) AS count, WEEKDAY(FROM_UNIXTIME(time)) AS day FROM posts_%s WHERE YEARWEEK(FROM_UNIXTIME(time), 1) = YEARWEEK(NOW() - INTERVAL 1 HOUR, 1) AND ( (DATE(FROM_UNIXTIME(time)) = DATE(NOW() - INTERVAL 1 HOUR) AND HOUR(FROM_UNIXTIME(time)) <= HOUR(NOW() - INTERVAL 1 HOUR)) OR (DATE(FROM_UNIXTIME(time)) < DATE(NOW() - INTERVAL 1 HOUR)) ) GROUP BY day UNION ALL ", $board['uri']);
else
$query .= sprintf("SELECT COUNT(*) AS count, WEEKDAY(FROM_UNIXTIME(time)) AS day FROM posts_%s WHERE YEARWEEK(FROM_UNIXTIME(time), 1) = YEARWEEK(NOW() - INTERVAL 1 DAY, 1) AND WEEKDAY(FROM_UNIXTIME(time)) <= WEEKDAY(NOW() - INTERVAL 1 DAY) GROUP BY day UNION ALL ", $board['uri']);
}
}
// Remove the last "UNION ALL" seperator and complete the query
$query = preg_replace('/UNION ALL $/', ') AS deriv_all GROUP BY day ORDER BY day ASC', $query);
} else {
if($previous_week) {
if($realtime)
$query = sprintf("SELECT COUNT(*) AS count, WEEKDAY(FROM_UNIXTIME(time)) AS day FROM posts_%s WHERE YEARWEEK(FROM_UNIXTIME(time), 1) = YEARWEEK(DATE_SUB(NOW(), INTERVAL 1 WEEK), 1) GROUP BY day", $boardName);
else if($hour_realtime)
$query = sprintf("SELECT COUNT(*) AS count, WEEKDAY(FROM_UNIXTIME(time)) AS day FROM posts_%s WHERE YEARWEEK(FROM_UNIXTIME(time), 1) = YEARWEEK(DATE_SUB(NOW() - INTERVAL 1 HOUR, INTERVAL 1 WEEK), 1) GROUP BY day", $boardName);
else
$query = sprintf("SELECT COUNT(*) AS count, WEEKDAY(FROM_UNIXTIME(time)) AS day FROM posts_%s WHERE YEARWEEK(FROM_UNIXTIME(time), 1) = YEARWEEK(DATE_SUB(NOW() - INTERVAL 1 DAY, INTERVAL 1 WEEK), 1) GROUP BY day", $boardName);
} else {
if($realtime)
$query .= sprintf("SELECT COUNT(*) AS count, WEEKDAY(FROM_UNIXTIME(time)) AS day FROM posts_%s WHERE YEARWEEK(FROM_UNIXTIME(time), 1) = YEARWEEK(NOW(), 1) GROUP BY day", $boardName);
else if($hour_realtime)
$query .= sprintf("SELECT COUNT(*) AS count, WEEKDAY(FROM_UNIXTIME(time)) AS day FROM posts_%s WHERE YEARWEEK(FROM_UNIXTIME(time), 1) = YEARWEEK(NOW() - INTERVAL 1 HOUR, 1) AND ( (DATE(FROM_UNIXTIME(time)) = DATE(NOW() - INTERVAL 1 HOUR) AND HOUR(FROM_UNIXTIME(time)) <= HOUR(NOW() - INTERVAL 1 HOUR)) OR (DATE(FROM_UNIXTIME(time)) < DATE(NOW() - INTERVAL 1 HOUR)) ) GROUP BY day", $boardName);
else
$query .= sprintf("SELECT COUNT(*) AS count, WEEKDAY(FROM_UNIXTIME(time)) AS day FROM posts_%s WHERE YEARWEEK(FROM_UNIXTIME(time), 1) = YEARWEEK(NOW() - INTERVAL 1 DAY, 1) AND WEEKDAY(FROM_UNIXTIME(time)) <= WEEKDAY(NOW() - INTERVAL 1 DAY) GROUP BY day", $boardName);
}
}
$query = query($query) or error(db_error($query));
$query_result = $query->fetchAll(PDO::FETCH_ASSOC);
// Get week array over post count
$statistics_week = array_fill(0,7,0);
foreach ($query_result as &$day_data) {
$statistics_week[$day_data['day']] = $day_data['count'];
}
return $statistics_week;
}
static public function get_stat_week_labels($week_data) {
return sprintf("'Monday\\n(%d)', 'Tuesday\\n(%d)', 'Wednesday\\n(%d)', 'Thursday\\n(%d)', 'Friday\\n(%d)', 'Saturday\\n(%d)', 'Sunday\\n(%d)'", $week_data[0], $week_data[1], $week_data[2], $week_data[3], $week_data[4], $week_data[5], $week_data[6]);
}
static public function get_stat_week_jsdata($week_data) {
// Make string for JS
return implode(",", $week_data);
}
}
?>

10
js/chartist/chartist.min.js

File diff suppressed because one or more lines are too long

3
mod.php

@ -86,6 +86,9 @@ $pages = array(
'/(\%b)/shadow_delete/(\d+)' => 'secure_POST shadow_delete_post', // permanent delete shadow deleted post
'/(\%b)/shadow_purge/(\d+)' => 'secure_POST shadow_purge', // permanent delete all shadow deleted post that have timed out
'/statistics' => 'view_statistics', // view site post statistics
'/(\%b)/statistics' => 'view_board_statistics', // view post statistics for a given board
'/(\%b)/warning(&delete)?/(\d+)' => 'secure_POST warning_post', // issue warning for post
'/(\%b)/ban(&delete)?/(\d+)' => 'secure_POST ban_post', // ban poster
'/(\%b)/move/(\d+)' => 'secure_POST move', // move thread

1
stylesheets/chartist/chartist.min.css

File diff suppressed because one or more lines are too long

3
templates/mod/dashboard.html

@ -115,6 +115,9 @@
{% if mod|hasPermission(config.mod.recent) %}
<li><a href="?/shadow_recent_post/25">{% trans 'Recent shadow deleted posts' %}</a></li>
{% endif %}
{% if mod|hasPermission(config.mod.view_statistics) %}
<li><a href="?/statistics">{% trans 'Statistics' %}</a></li>
{% endif %}
{% if mod|hasPermission(config.mod.rebuild) %}
<li><a href="?/rebuild">{% trans 'Rebuild' %}</a></li>
{% endif %}

77
templates/mod/statistics.html

@ -0,0 +1,77 @@
<script src='{{ root }}main.js'></script>
<script src='{{ root }}js/jquery.min.js'></script>
<script src='{{ root }}js/mobile-style.js'></script>
<script src="{{ root }}js/chartist/chartist.min.js"></script>
<link rel='stylesheet' href='{{ root }}stylesheets/chartist/chartist.min.css'>
{% if (mod) or (public_hourly) %}
<h2>Hourly Statistics</h2>
<section>
<div class="ct-chart" id="hourly"></div>
</section>
{% endif %}
<h2>Weekly Statistics (for this and previous week)</h2>
<section>
<div class="ct-chart" id="week"></div>
</section>
<h2>View Board Specific Stats</h2>
<section>
{% if mod %}
<a href="?/statistics">[Full Statistics]</a>
{% for board in boards %}
<a href="?/{{board.uri}}/statistics">[{{board.uri}}]</a>
{% endfor %}
{% else %}
<a href="{{ config.root }}/{{ stat_filename }}">[Full Statistics]</a>
{% for board in boards %}
<a href="{{ config.root }}/{{ board.uri }}/{{ stat_filename }}">[{{board.uri}}]</a>
{% endfor %}
{% endif %}
</section>
<script type="text/javascript">{% raw %}
{% endraw %}{% if (mod) or (public_hourly) %}{% raw %}
var data_24h = {
labels: [ "AM", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11",
"PM", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11" ],
series: [
[{% endraw %}{{ statistics_24h }}{% raw %}]
]
};
var options_24h = {
width: 800,
height: 300
};
new Chartist.Line('#hourly', data_24h, options_24h);
{% endraw %}{% endif %}{% raw %}
var data_week = {
labels: [{% endraw %}{{ statistics_week_labels }}{% raw %}],
series: [
[{% endraw %}{{ statistics_week_past }}{% raw %}],
[{% endraw %}{{ statistics_week }}{% raw %}]
]
};
var options_week = {
width: 800,
height: 300,
seriesBarDistance: 10,
reverseData: true,
horizontalBars: true,
axisY: {
offset: 70
}
};
new Chartist.Bar('#week', data_week, options_week);
{% endraw %}</script>

85
tools/public_statistics_cli.php

@ -0,0 +1,85 @@
<?php
// A script to create public statistics file
require dirname(__FILE__) . '/inc/cli.php';
if (!isset ($argv[1])) {
die("Usage: tools/public_statistics_cli.php stat_file\n");
}
$stat_file = $argv[1];
// Build list of boards listed at top of page (visible boards)
$board_list = listBoards(false);
$boards = array();
if($config['public_stats']['boards'] === true) {
$boards = $board_list;
} else if($config['public_stats']['boards'] === false) {
foreach($board_list as $board) {
if(in_array_r($board['uri'], $config['boards'], true))
$boards[] = $board;
}
} else if(is_array($config['public_stats']['boards'])) {
foreach($board_list as $board) {
if(in_array($board['uri'], $config['public_stats']['boards'], true))
$boards[] = $board;
}
} else {
error("Board list config is corrupt.");
}
if(count($boards) == 0) {
error("No boards to show stats for.");
}
// Write main stats file
file_write($config['dir']['home'] . $stat_file, statpage(false, $boards, $stat_file));
// Write stats file for each board
foreach($boards as $board) {
file_write($config['dir']['home'] . sprintf($config['board_path'], $board['uri']) . $stat_file, statpage($board['uri'], $boards, $stat_file));
}
echo("done\n");
// Build statistics page
function statpage($board = false, $boards, $stat_file) {
global $config;
// Get statistics from db
$statistics_hour = $config['public_stats']['hourly']?Statistics::get_stat_24h($board, $config['public_stats']['realtime'], $boards):false;
$this_week = Statistics::get_stat_week(false, $board, $config['public_stats']['realtime'], $config['public_stats']['hourly'], $boards);
$prev_week = Statistics::get_stat_week(true, $board, $config['public_stats']['realtime'], $config['public_stats']['hourly'], $boards);
return Element('page.html', array(
'config' => $config,
'mod' => false,
'hide_dashboard_link' => true,
'title' => _("Statistics") . ($board?" for /" . $board . "/":""),
'subtitle' => _("Last Updated : ") . gmstrftime($config['public_stats']['date']),
'nojavascript' => true,
'boardlist' => createBoardlist(false),
'body' => Element('mod/statistics.html', array(
'root' => $config['root'],
'mod' => false,
'boards' => $boards,
'stat_filename' => $stat_file,
'public_hourly' => $config['public_stats']['hourly'],
'statistics_24h' => $statistics_hour,
'statistics_week_labels' => Statistics::get_stat_week_labels($this_week),
'statistics_week' => Statistics::get_stat_week_jsdata($this_week),
'statistics_week_past' => Statistics::get_stat_week_jsdata($prev_week)
))
));
}
function in_array_r($needle, $haystack, $strict = false) {
foreach ($haystack as $item) {
if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
return true;
}
}
return false;
}
?>
Loading…
Cancel
Save