Add statistics page

- Closes https://github.com/friendica/friendica-directory/issues/2
This commit is contained in:
Hypolite Petovan 2018-11-18 10:38:43 -05:00
parent e8752c7631
commit ce075c80e6
5 changed files with 240 additions and 0 deletions

View file

@ -0,0 +1,161 @@
<?php
namespace Friendica\Directory\Controllers\Web;
use \Friendica\Directory\Content\Pager;
use PDO;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
/**
* @author Hypolite Petovan <mrpetovan@gmail.com>
*/
class Statistics extends BaseController
{
/**
* @var \Atlas\Pdo\Connection
*/
private $connection;
/**
* @var \Psr\SimpleCache\CacheInterface
*/
private $simplecache;
/**
* @var \Friendica\Directory\Views\PhpRenderer
*/
private $renderer;
public function __construct(
\Atlas\Pdo\Connection $atlas,
\Psr\SimpleCache\CacheInterface $simplecache,
\Friendica\Directory\Views\PhpRenderer $renderer
)
{
$this->connection = $atlas;
$this->simplecache = $simplecache;
$this->renderer = $renderer;
}
public function render(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args): array
{
$profilePollQueueCount = $this->connection->fetchValue('SELECT COUNT(*) FROM `profile_poll_queue`');
$profileCounts = $this->connection->fetchOne(
'SELECT
COUNT(*) AS `total`,
SUM(CASE WHEN `available` THEN 1 ELSE 0 END) AS `available`,
SUM(CASE WHEN `language` IS NOT NULL THEN 1 ELSE 0 END) AS `language`
FROM `profile`
WHERE NOT `hidden`');
$stmt = 'SELECT `language`, COUNT(*) AS `total`, COUNT(*) / :total AS `ratio`
FROM `profile`
WHERE `language` IS NOT NULL
AND `available`
AND NOT `hidden`
GROUP BY `language`
ORDER BY COUNT(*) DESC';
$profileLanguages = $this->connection->fetchAll($stmt, ['total' => $profileCounts['language']]);
$stable_version = $this->simplecache->get('stable_version');
if (!$stable_version) {
$stable_version = trim(file_get_contents('https://git.friendi.ca/friendica/friendica/raw/branch/master/VERSION'));
$this->simplecache->set('stable_version', $stable_version);
}
$dev_version = $this->simplecache->get('dev_version');
if (!$dev_version) {
$dev_version = trim(file_get_contents('https://git.friendi.ca/friendica/friendica/raw/branch/develop/VERSION'));
$this->simplecache->set('dev_version', $dev_version);
}
$serverPollQueueCount = $this->connection->fetchValue('SELECT COUNT(*) FROM `server_poll_queue`');
$serverCounts = $this->connection->fetchOne(
'SELECT
COUNT(*) AS `total`,
SUM(CASE WHEN `available` THEN 1 ELSE 0 END) AS `available`,
SUM(CASE WHEN `available` AND `language` IS NOT NULL THEN 1 ELSE 0 END) AS `language`,
SUM(CASE WHEN `available` AND `reg_policy` = "REGISTER_OPEN" THEN 1 ELSE 0 END) AS `open`,
SUM(CASE WHEN `available` AND `version` IS NOT NULL THEN 1 ELSE 0 END) AS `version`,
SUM(CASE WHEN `available` AND `version` = :dev_version THEN 1 ELSE 0 END) AS `dev_version`,
SUM(CASE WHEN `available` AND `version` = :stable_version THEN 1 ELSE 0 END) AS `stable_version`,
SUM(CASE WHEN `available` AND `version` != :dev_version AND `version` != :stable_version THEN 1 ELSE 0 END) AS `outdated_version`
FROM `server`
WHERE NOT `hidden`', ['dev_version' => $dev_version, 'stable_version' => $stable_version]);
$stmt = 'SELECT LEFT(`language`, 2) AS `language`, COUNT(*) AS `total`, COUNT(*) / :total AS `ratio`
FROM `server`
WHERE `language` IS NOT NULL
AND `available`
AND NOT `hidden`
GROUP BY LEFT(`language`, 2)
ORDER BY COUNT(*) DESC';
$serverLanguages = $this->connection->fetchAll($stmt, ['total' => $serverCounts['language']]);
$stmt = 'SELECT `version`, COUNT(*) AS `total`, COUNT(*) / :total AS `ratio`
FROM `server`
WHERE `version` IS NOT NULL
AND `available`
AND NOT `hidden`
GROUP BY `version`
ORDER BY COUNT(*) DESC';
$serverVersions = $this->connection->fetchAll($stmt, ['total' => $serverCounts['version']]);
$vars = [
'stats' => [
'profile_queue' => [
'total' => $profilePollQueueCount
],
'profile' => [
'total' => $profileCounts['total'],
'ratio' => $profileCounts['total'] / $profilePollQueueCount,
'available' => [
'total' => $profileCounts['available'],
'ratio' => $profileCounts['available'] / $profileCounts['total']
],
'language' => [
'total' => $profileCounts['language'],
'ratio' => $profileCounts['language'] / $profileCounts['available']
],
'languages' => $profileLanguages,
],
'server_queue' => [
'total' => $serverPollQueueCount
],
'server' => [
'total' => $serverCounts['total'],
'ratio' => $serverCounts['total'] / $serverPollQueueCount,
'available' => [
'total' => $serverCounts['available'],
'ratio' => $serverCounts['available'] / $serverCounts['total']
],
'language' => [
'total' => $serverCounts['language'],
'ratio' => $serverCounts['language'] / $serverCounts['available']
],
'open' => [
'total' => $serverCounts['open'],
'ratio' => $serverCounts['open'] / $serverCounts['available']
],
'version' => [
'total' => $serverCounts['version'],
'ratio' => $serverCounts['version'] / $serverCounts['available']
],
'languages' => $serverLanguages,
'versions' => $serverVersions,
],
],
'dev_version' => $dev_version,
'stable_version' => $stable_version,
];
$content = $this->renderer->fetch('statistics.phtml', $vars);
// Render index view
return ['content' => $content, 'noNavSearch' => true];
}
}

View file

@ -0,0 +1,20 @@
<?php
namespace Friendica\Directory\Routes\Web;
/**
* @author Hypolite Petovan <mrpetovan@gmail.com>
*/
class Statistics extends BaseRoute
{
public function __construct(\Slim\Container $container)
{
parent::__construct($container);
$this->controller = new \Friendica\Directory\Controllers\Web\Statistics(
$this->container->atlas,
$this->container->simplecache,
$this->container->renderer
);
}
}

View file

@ -21,6 +21,8 @@ $app->get('/search[/{account_type}]', function (Request $request, Response $resp
return $route($request, $response, $args);
})->setName('search');
$app->get('/stats', \Friendica\Directory\Routes\Web\Statistics::class);
$app->get('/submit', \Friendica\Directory\Routes\Http\Submit::class);
$app->get('/photo/{profile_id:[0-9]+}.jpg', \Friendica\Directory\Routes\Http\Photo::class)->setName('photo');

View file

@ -58,6 +58,8 @@
</li>
<li class="nav-item">
<a class="nav-link" href="servers"><i class="fa fa-hotel"></i> <?php echo $this->__('Public servers')?></a>
</li><li class="nav-item">
<a class="nav-link" href="stats"><i class="fa fa-clipboard-list"></i> <?php echo $this->__('Stats')?></a>
</li>
<li class="nav-item">
<div class="dropdown">

View file

@ -0,0 +1,55 @@
<?php
use \Friendica\Directory\Utils\L10n;
?>
<h1><?php echo $this->__('Directory statistics') ?></h1>
<h2><?php echo $this->__('Profiles') ?></h2>
<p>This directory knows about <strong><?php echo L10n::formatNumber($stats['profile_queue']['total'])?> distinct potential profile URLs</strong>.</p>
<p>Out of those, there are <strong><?php echo L10n::formatNumber($stats['profile']['total'])?> URLs (<?php echo L10n::formatNumber($stats['profile']['ratio'], L10n::PERCENT)?>)</strong> that have been an opt-in profile URL at least once.</p>
<p>Out of those, there are <strong><?php echo L10n::formatNumber($stats['profile']['available']['total'])?> profiles (<?php echo L10n::formatNumber($stats['profile']['available']['ratio'], L10n::PERCENT)?>)</strong> that are currently available. <a href="/">Check them out!</a></p>
<h3><?php echo $this->__('Languages') ?></h3>
Out of <strong><?php echo L10n::formatNumber($stats['profile']['language']['total'])?></strong> profiles reporting their language there are:
<ul>
<?php foreach($stats['profile']['languages'] as $language):?>
<li>
<strong><?php echo L10n::formatNumber($language['total'])?></strong>
<?php echo empty($language['language']) ? 'N/A' : L10n::localeToLanguageString($language['language'])?>
(<?php echo L10n::formatNumber($language['ratio'], L10n::PERCENT)?>)
</li>
<?php endforeach;?>
</ul>
<h2><?php echo $this->__('Servers') ?></h2>
<p>This directory knows about <strong><?php echo L10n::formatNumber($stats['server_queue']['total'])?> distinct potential server URLs</strong>.</p>
<p>Out of those, there are <strong><?php echo L10n::formatNumber($stats['server']['total'])?> domains (<?php echo L10n::formatNumber($stats['server']['ratio'], L10n::PERCENT)?>)</strong> that have been a server at least once.</p>
<p>Out of those, there are :</p>
<ul>
<li><strong><?php echo L10n::formatNumber($stats['server']['available']['total'])?> servers (<?php echo L10n::formatNumber($stats['server']['available']['ratio'], L10n::PERCENT)?>%)</strong> that are currently available.</li>
<li><strong><?php echo L10n::formatNumber($stats['server']['open']['total'])?> public servers (<?php echo L10n::formatNumber($stats['server']['open']['ratio'], L10n::PERCENT)?>%)</strong> currently open for registration. <a href="/servers">Check them out!</a></li>
</ul>
<h3><?php echo $this->__('Languages') ?></h3>
Out of <strong><?php echo L10n::formatNumber($stats['server']['language']['total'])?></strong> servers reporting their language there are:
<ul>
<?php foreach($stats['server']['languages'] as $language):?>
<li>
<strong><?php echo L10n::formatNumber($language['total'])?></strong>
<?php echo empty($language['language']) ? 'N/A' : L10n::localeToLanguageString($language['language'])?>
(<?php echo L10n::formatNumber($language['ratio'], L10n::PERCENT)?>)
</li>
<?php endforeach;?>
</ul>
<h3><?php echo $this->__('Versions') ?></h3>
Out of <strong><?php echo L10n::formatNumber($stats['server']['version']['total'])?></strong> servers reporting their version there are:
<ul>
<?php foreach($stats['server']['versions'] as $version):?>
<li>
<strong><?php echo L10n::formatNumber($version['total'])?></strong>
<code><?php echo $this->e($version['version'])?></code>
(<?php echo L10n::formatNumber($version['ratio'], L10n::PERCENT)?>)
<?php if ($version['version'] == $stable_version):?>
<span class="badge badge-success"><i class="fa fa-smile"></i> <?php echo $this->__('Stable Version')?></span>
<?php elseif ($version['version'] == $dev_version):?>
<span class="badge badge-secondary"><i class="fa fa-poo"></i> <?php echo $this->__('Develop Version')?></span>
<?php endif;?>
</li>
<?php endforeach;?>
</ul>