Display stats in a human-friendly manner :D

This commit is contained in:
Starbeamrainbowlabs 2017-07-14 22:13:13 +01:00
parent 8c7b021865
commit 111a8b9707
5 changed files with 160 additions and 28 deletions

View File

@ -538,7 +538,15 @@ function human_filesize($bytes, $decimals = 2)
*/
function human_time_since($time)
{
$timediff = time() - $time;
return human_time(time() - $time);
}
/**
* Renders a given number of seconds as something that humans can understand more easily.
* @param int $seconds The number of seconds to render.
* @return string The rendered time.
*/
function human_time($seconds)
{
$tokens = array (
31536000 => 'year',
2592000 => 'month',
@ -549,8 +557,8 @@ function human_time_since($time)
1 => 'second'
);
foreach ($tokens as $unit => $text) {
if ($timediff < $unit) continue;
$numberOfUnits = floor($timediff / $unit);
if ($seconds < $unit) continue;
$numberOfUnits = floor($seconds / $unit);
return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'').' ago';
}
}
@ -4032,17 +4040,54 @@ register_module([
*/
/*
███████ ████████ █████ ████████ ███████
██ ██ ██ ██ ██ ██
███████ ██ ███████ ██ ███████
██ ██ ██ ██ ██ ██
███████ ██ ██ ██ ██ ███████
* ███████ ████████ █████ ████████ ███████
* ██ ██ ██ ██ ██ ██
* ███████ ██ ███████ ██ ███████
* ██ ██ ██ ██ ██ ██
* ███████ ██ ██ ██ ██ ███████
*/
add_action("stats", function() {
global $settings, $statistic_calculators;
$stats = stats_load();
$content = "<h1>Statistics</h1>";
$content .= "<p>This page contains a selection of statistics about $settings->sitename's content. They are updated automatically about every " . trim(str_replace(["ago", "1 "], [""], human_time($settings->stats_update_interval))) . ", although $settings->sitename's local friendly moderators may update it earlier (you can see their names at the bottom of every page).</p>\n";
$stat_scalar_values = [];
$stat_contents = [];
foreach($statistic_calculators as $stat_id => $stat_calculator) {
if(!empty($stat_calculator["render"]))
$stat_contents[$stat_calculator["name"]] = $stat_calculator["render"]($stats->$stat_id);
else
$stat_scalar_values[$stat_calculator["name"]] = $stats->$stat_id->value;
}
$content .= "<table class='stats-table'>\n";
$content .= "\t<tr><th>Statistic</th><th>Value</th></tr>\n\n";
foreach($stat_scalar_values as $scalar_name => $scalar_value) {
$content .= "\t<tr><td>$scalar_name</td><td>$scalar_value</td></tr>\n";
}
$content .= "</table>\n";
foreach($stat_contents as $stat_content_part)
$content .= "$stat_content_part\n";
exit(page_renderer::render_main("Statistics - $settings->sitename", $content));
});
██ ██ ██████ ██████ █████ ████████ ███████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██████ ██ ██ ███████ ██ █████
██ ██ ██ ██ ██ ██ ██ ██ ██
██████ ██ ██████ ██ ██ ██ ███████
/*
* ███████ ████████ █████ ████████ ███████
* ██ ██ ██ ██ ██ ██
* ███████ ██ ███████ ██ ███████
* ██ ██ ██ ██ ██ ██
* ███████ ██ ██ ██ ██ ███████
*
* ██ ██ ██████ ██████ █████ ████████ ███████
* ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
* ██ ██ ██████ ██ ██ ███████ ██ █████
* ██ ██ ██ ██ ██ ██ ██ ██ ██
* ██████ ██ ██████ ██ ██ ██ ███████
*/
add_action("stats-update", function() {
global $env, $paths, $settings;
@ -4087,6 +4132,17 @@ register_module([
$result->value = $pages;
$result->completed = true;
return $result;
},
"render" => function($stats_data) {
$result = "<h2>$stats_data->name</h2>\n";
$result .= "<ol class='stats-list longest-pages-list'>\n";
$i = 0;
foreach($stats_data->value as $pagename => $page_length) {
$result .= "\t<li class='stats-item long-page'>$pagename <em>(" . human_filesize($page_length) . ")</em></li>\n";
$i++;
}
$result .= "</ol>\n";
return $result;
}
]);
@ -6927,6 +6983,16 @@ register_module([
$result->value = $pages;
$result->completed = true;
return $result;
},
"render" => function($stats_data) {
$result = "<h2>$stats_data->name</h2>\n";
$result .= "<table class='wanted-pages'>\n";
$result .= "\t<tr><th>Page Name</th><th>Linking Pages</th></tr>\n";
foreach($stats_data->value as $pagename => $linking_pages) {
$result .= "\t<tr><td>$pagename</td><td>$linking_pages</td></tr>\n";
}
$result .= "</table>\n";
return $result;
}
]);

View File

@ -181,7 +181,15 @@ function human_filesize($bytes, $decimals = 2)
*/
function human_time_since($time)
{
$timediff = time() - $time;
return human_time(time() - $time);
}
/**
* Renders a given number of seconds as something that humans can understand more easily.
* @param int $seconds The number of seconds to render.
* @return string The rendered time.
*/
function human_time($seconds)
{
$tokens = array (
31536000 => 'year',
2592000 => 'month',
@ -192,8 +200,8 @@ function human_time_since($time)
1 => 'second'
);
foreach ($tokens as $unit => $text) {
if ($timediff < $unit) continue;
$numberOfUnits = floor($timediff / $unit);
if ($seconds < $unit) continue;
$numberOfUnits = floor($seconds / $unit);
return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'').' ago';
}
}

View File

@ -113,7 +113,7 @@
"author": "Starbeamrainbowlabs",
"description": "An extensible statistics calculation system. Comes with a range of built-in statistics, but can be extended by other modules too.",
"id": "feature-stats",
"lastupdate": 1500064069,
"lastupdate": 1500066611,
"optional": false
},
{
@ -266,7 +266,7 @@
"author": "Emanuil Rusev & Starbeamrainbowlabs",
"description": "An upgraded (now default!) parser based on Emanuil Rusev's Parsedown Extra PHP library (https:\/\/github.com\/erusev\/parsedown-extra), which is licensed MIT. Please be careful, as this module adds some weight to your installation, and also *requires* write access to the disk on first load.",
"id": "parser-parsedown",
"lastupdate": 1500063269,
"lastupdate": 1500065639,
"optional": false
}
]

View File

@ -17,17 +17,54 @@ register_module([
*/
/*
███████ ████████ █████ ████████ ███████
██ ██ ██ ██ ██ ██
███████ ██ ███████ ██ ███████
██ ██ ██ ██ ██ ██
███████ ██ ██ ██ ██ ███████
* ███████ ████████ █████ ████████ ███████
* ██ ██ ██ ██ ██ ██
* ███████ ██ ███████ ██ ███████
* ██ ██ ██ ██ ██ ██
* ███████ ██ ██ ██ ██ ███████
*/
add_action("stats", function() {
global $settings, $statistic_calculators;
$stats = stats_load();
$content = "<h1>Statistics</h1>";
$content .= "<p>This page contains a selection of statistics about $settings->sitename's content. They are updated automatically about every " . trim(str_replace(["ago", "1 "], [""], human_time($settings->stats_update_interval))) . ", although $settings->sitename's local friendly moderators may update it earlier (you can see their names at the bottom of every page).</p>\n";
$stat_scalar_values = [];
$stat_contents = [];
foreach($statistic_calculators as $stat_id => $stat_calculator) {
if(!empty($stat_calculator["render"]))
$stat_contents[$stat_calculator["name"]] = $stat_calculator["render"]($stats->$stat_id);
else
$stat_scalar_values[$stat_calculator["name"]] = $stats->$stat_id->value;
}
$content .= "<table class='stats-table'>\n";
$content .= "\t<tr><th>Statistic</th><th>Value</th></tr>\n\n";
foreach($stat_scalar_values as $scalar_name => $scalar_value) {
$content .= "\t<tr><td>$scalar_name</td><td>$scalar_value</td></tr>\n";
}
$content .= "</table>\n";
foreach($stat_contents as $stat_content_part)
$content .= "$stat_content_part\n";
exit(page_renderer::render_main("Statistics - $settings->sitename", $content));
});
██ ██ ██████ ██████ █████ ████████ ███████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██████ ██ ██ ███████ ██ █████
██ ██ ██ ██ ██ ██ ██ ██ ██
██████ ██ ██████ ██ ██ ██ ███████
/*
* ███████ ████████ █████ ████████ ███████
* ██ ██ ██ ██ ██ ██
* ███████ ██ ███████ ██ ███████
* ██ ██ ██ ██ ██ ██
* ███████ ██ ██ ██ ██ ███████
*
* ██ ██ ██████ ██████ █████ ████████ ███████
* ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
* ██ ██ ██████ ██ ██ ███████ ██ █████
* ██ ██ ██ ██ ██ ██ ██ ██ ██
* ██████ ██ ██████ ██ ██ ██ ███████
*/
add_action("stats-update", function() {
global $env, $paths, $settings;
@ -72,6 +109,17 @@ register_module([
$result->value = $pages;
$result->completed = true;
return $result;
},
"render" => function($stats_data) {
$result = "<h2>$stats_data->name</h2>\n";
$result .= "<ol class='stats-list longest-pages-list'>\n";
$i = 0;
foreach($stats_data->value as $pagename => $page_length) {
$result .= "\t<li class='stats-item long-page'>$pagename <em>(" . human_filesize($page_length) . ")</em></li>\n";
$i++;
}
$result .= "</ol>\n";
return $result;
}
]);

View File

@ -67,6 +67,16 @@ register_module([
$result->value = $pages;
$result->completed = true;
return $result;
},
"render" => function($stats_data) {
$result = "<h2>$stats_data->name</h2>\n";
$result .= "<table class='wanted-pages'>\n";
$result .= "\t<tr><th>Page Name</th><th>Linking Pages</th></tr>\n";
foreach($stats_data->value as $pagename => $linking_pages) {
$result .= "\t<tr><td>$pagename</td><td>$linking_pages</td></tr>\n";
}
$result .= "</table>\n";
return $result;
}
]);