Extract admin dashboard statistics from core into an extension

This commit is contained in:
Toby Zerner 2017-12-10 21:03:48 +10:30
parent 617a76dda8
commit 3dcfe32b27
5 changed files with 13 additions and 309 deletions

184
js/admin/dist/app.js vendored
View File

@ -18360,17 +18360,15 @@ System.register('flarum/components/Checkbox', ['flarum/Component', 'flarum/compo
});;
'use strict';
System.register('flarum/components/DashboardPage', ['flarum/components/Page', 'flarum/components/StatusWidget', 'flarum/components/StatisticsWidget'], function (_export, _context) {
System.register('flarum/components/DashboardPage', ['flarum/components/Page', 'flarum/components/StatusWidget'], function (_export, _context) {
"use strict";
var Page, StatusWidget, StatisticsWidget, DashboardPage;
var Page, StatusWidget, DashboardPage;
return {
setters: [function (_flarumComponentsPage) {
Page = _flarumComponentsPage.default;
}, function (_flarumComponentsStatusWidget) {
StatusWidget = _flarumComponentsStatusWidget.default;
}, function (_flarumComponentsStatisticsWidget) {
StatisticsWidget = _flarumComponentsStatisticsWidget.default;
}],
execute: function () {
DashboardPage = function (_Page) {
@ -18390,11 +18388,15 @@ System.register('flarum/components/DashboardPage', ['flarum/components/Page', 'f
m(
'div',
{ className: 'container' },
m(StatusWidget, null),
m(StatisticsWidget, null)
this.availableWidgets()
)
);
}
}, {
key: 'availableWidgets',
value: function availableWidgets() {
return [m(StatusWidget, null)];
}
}]);
return DashboardPage;
}(Page);
@ -21112,176 +21114,6 @@ System.register('flarum/components/SplitDropdown', ['flarum/components/Dropdown'
});;
'use strict';
System.register('flarum/components/StatisticsWidget', ['flarum/components/DashboardWidget', 'flarum/helpers/icon', 'flarum/helpers/listItems', 'flarum/utils/ItemList'], function (_export, _context) {
"use strict";
var DashboardWidget, icon, listItems, ItemList, StatisticsWidget;
return {
setters: [function (_flarumComponentsDashboardWidget) {
DashboardWidget = _flarumComponentsDashboardWidget.default;
}, function (_flarumHelpersIcon) {
icon = _flarumHelpersIcon.default;
}, function (_flarumHelpersListItems) {
listItems = _flarumHelpersListItems.default;
}, function (_flarumUtilsItemList) {
ItemList = _flarumUtilsItemList.default;
}],
execute: function () {
StatisticsWidget = function (_DashboardWidget) {
babelHelpers.inherits(StatisticsWidget, _DashboardWidget);
function StatisticsWidget() {
babelHelpers.classCallCheck(this, StatisticsWidget);
return babelHelpers.possibleConstructorReturn(this, (StatisticsWidget.__proto__ || Object.getPrototypeOf(StatisticsWidget)).apply(this, arguments));
}
babelHelpers.createClass(StatisticsWidget, [{
key: 'className',
value: function className() {
return 'StatisticsWidget';
}
}, {
key: 'content',
value: function content() {
return m(
'table',
null,
m(
'thead',
null,
m(
'tr',
null,
m('th', null),
m(
'th',
null,
app.translator.trans('core.admin.statistics.users_heading')
),
m(
'th',
null,
app.translator.trans('core.admin.statistics.discussions_heading')
),
m(
'th',
null,
app.translator.trans('core.admin.statistics.posts_heading')
)
)
),
m(
'tbody',
null,
m(
'tr',
{ className: 'StatisticsWidget-total' },
m(
'th',
null,
app.translator.trans('core.admin.statistics.total_label')
),
m(
'td',
null,
app.data.statistics.total.users
),
m(
'td',
null,
app.data.statistics.total.discussions
),
m(
'td',
null,
app.data.statistics.total.posts
)
),
m(
'tr',
null,
m(
'th',
null,
app.translator.trans('core.admin.statistics.last_28_days_label')
),
m(
'td',
null,
app.data.statistics.month.users
),
m(
'td',
null,
app.data.statistics.month.discussions
),
m(
'td',
null,
app.data.statistics.month.posts
)
),
m(
'tr',
null,
m(
'th',
null,
app.translator.trans('core.admin.statistics.last_7_days_label')
),
m(
'td',
null,
app.data.statistics.week.users
),
m(
'td',
null,
app.data.statistics.week.discussions
),
m(
'td',
null,
app.data.statistics.week.posts
)
),
m(
'tr',
null,
m(
'th',
null,
app.translator.trans('core.admin.statistics.today_label')
),
m(
'td',
null,
app.data.statistics.today.users
),
m(
'td',
null,
app.data.statistics.today.discussions
),
m(
'td',
null,
app.data.statistics.today.posts
)
)
)
);
}
}]);
return StatisticsWidget;
}(DashboardWidget);
_export('default', StatisticsWidget);
}
};
});;
'use strict';
System.register('flarum/components/StatusWidget', ['flarum/components/DashboardWidget', 'flarum/helpers/icon', 'flarum/helpers/listItems', 'flarum/utils/ItemList'], function (_export, _context) {
"use strict";

View File

@ -1,16 +1,18 @@
import Page from 'flarum/components/Page';
import StatusWidget from 'flarum/components/StatusWidget';
import StatisticsWidget from 'flarum/components/StatisticsWidget';
export default class DashboardPage extends Page {
view() {
return (
<div className="DashboardPage">
<div className="container">
<StatusWidget/>
<StatisticsWidget/>
{this.availableWidgets()}
</div>
</div>
);
}
availableWidgets() {
return [<StatusWidget/>];
}
}

View File

@ -1,60 +0,0 @@
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import DashboardWidget from 'flarum/components/DashboardWidget';
import icon from 'flarum/helpers/icon';
import listItems from 'flarum/helpers/listItems';
import ItemList from 'flarum/utils/ItemList';
export default class StatisticsWidget extends DashboardWidget {
className() {
return 'StatisticsWidget';
}
content() {
return (
<table>
<thead>
<tr>
<th></th>
<th>{app.translator.trans('core.admin.statistics.users_heading')}</th>
<th>{app.translator.trans('core.admin.statistics.discussions_heading')}</th>
<th>{app.translator.trans('core.admin.statistics.posts_heading')}</th>
</tr>
</thead>
<tbody>
<tr className="StatisticsWidget-total">
<th>{app.translator.trans('core.admin.statistics.total_label')}</th>
<td>{app.data.statistics.total.users}</td>
<td>{app.data.statistics.total.discussions}</td>
<td>{app.data.statistics.total.posts}</td>
</tr>
<tr>
<th>{app.translator.trans('core.admin.statistics.last_28_days_label')}</th>
<td>{app.data.statistics.month.users}</td>
<td>{app.data.statistics.month.discussions}</td>
<td>{app.data.statistics.month.posts}</td>
</tr>
<tr>
<th>{app.translator.trans('core.admin.statistics.last_7_days_label')}</th>
<td>{app.data.statistics.week.users}</td>
<td>{app.data.statistics.week.discussions}</td>
<td>{app.data.statistics.week.posts}</td>
</tr>
<tr>
<th>{app.translator.trans('core.admin.statistics.today_label')}</th>
<td>{app.data.statistics.today.users}</td>
<td>{app.data.statistics.today.discussions}</td>
<td>{app.data.statistics.today.posts}</td>
</tr>
</tbody>
</table>
);
}
}

View File

@ -45,38 +45,3 @@
}
}
}
.StatisticsWidget {
td {
font-size: 14px;
}
td, th {
padding: 5px 20px 5px 0;
text-align: left;
font-weight: normal;
border-bottom: 1px solid @control-bg;
}
tbody tr:last-child {
td, th {
border-bottom: 0;
}
}
th {
color: @muted-color;
}
thead th {
font-weight: bold;
}
}
.StatisticsWidget-total {
td, th {
font-weight: bold;
}
}
@media @tablet-up {
.StatisticsWidget {
td, th {
min-width: 120px;
}
}
}

View File

@ -11,12 +11,8 @@
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;
@ -73,37 +69,6 @@ class WebAppController extends AbstractWebAppController
$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);
}
}