mirror of
https://github.com/flarum/core.git
synced 2025-08-17 13:54:18 +02:00
Basic implementation of dashboard widgets, statistics
Currently not user-customizable. Just needed to display statistics for a client, so figured I'd make a start at this. Nothing too fancy for now, but I'm sure some people will be happy to have this information at their fingertips.
This commit is contained in:
@@ -11,13 +11,18 @@
|
||||
|
||||
namespace Flarum\Admin\Controller;
|
||||
|
||||
use DateTime;
|
||||
use Flarum\Admin\WebApp;
|
||||
use Flarum\Core\Discussion;
|
||||
use Flarum\Core\Permission;
|
||||
use Flarum\Core\Post;
|
||||
use Flarum\Core\User;
|
||||
use Flarum\Event\PrepareUnserializedSettings;
|
||||
use Flarum\Extension\ExtensionManager;
|
||||
use Flarum\Http\Controller\AbstractWebAppController;
|
||||
use Flarum\Settings\SettingsRepositoryInterface;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
use Illuminate\Database\ConnectionInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
|
||||
class WebAppController extends AbstractWebAppController
|
||||
@@ -37,13 +42,15 @@ class WebAppController extends AbstractWebAppController
|
||||
* @param Dispatcher $events
|
||||
* @param SettingsRepositoryInterface $settings
|
||||
* @param ExtensionManager $extensions
|
||||
* @param ConnectionInterface $db
|
||||
*/
|
||||
public function __construct(WebApp $webApp, Dispatcher $events, SettingsRepositoryInterface $settings, ExtensionManager $extensions)
|
||||
public function __construct(WebApp $webApp, Dispatcher $events, SettingsRepositoryInterface $settings, ExtensionManager $extensions, ConnectionInterface $db)
|
||||
{
|
||||
$this->webApp = $webApp;
|
||||
$this->events = $events;
|
||||
$this->settings = $settings;
|
||||
$this->extensions = $extensions;
|
||||
$this->db = $db;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -63,6 +70,40 @@ class WebAppController extends AbstractWebAppController
|
||||
$view->setVariable('permissions', Permission::map());
|
||||
$view->setVariable('extensions', $this->extensions->getExtensions()->toArray());
|
||||
|
||||
$view->setVariable('phpVersion', PHP_VERSION);
|
||||
$view->setVariable('mysqlVersion', $this->db->selectOne('select version() as version')->version);
|
||||
|
||||
$view->setVariable('statistics', $this->getStatistics());
|
||||
|
||||
return $view;
|
||||
}
|
||||
|
||||
private function getStatistics()
|
||||
{
|
||||
return [
|
||||
'total' => $this->getEntityCounts(),
|
||||
'month' => $this->getEntityCounts(new DateTime('-28 days')),
|
||||
'week' => $this->getEntityCounts(new DateTime('-7 days')),
|
||||
'today' => $this->getEntityCounts(new DateTime('-1 day'))
|
||||
];
|
||||
}
|
||||
|
||||
private function getEntityCounts($since = null)
|
||||
{
|
||||
$queries = [
|
||||
'users' => User::query(),
|
||||
'discussions' => Discussion::query(),
|
||||
'posts' => Post::where('type', 'comment')
|
||||
];
|
||||
|
||||
if ($since) {
|
||||
$queries['users']->where('join_time', '>', $since);
|
||||
$queries['discussions']->where('start_time', '>', $since);
|
||||
$queries['posts']->where('time', '>', $since);
|
||||
}
|
||||
|
||||
return array_map(function ($query) {
|
||||
return $query->count();
|
||||
}, $queries);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user