Skip to content

Commit

Permalink
Extract admin dashboard statistics from core into an extension
Browse files Browse the repository at this point in the history
  • Loading branch information
tobyzerner committed Dec 10, 2017
1 parent 617a76d commit 3dcfe32
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 309 deletions.
184 changes: 8 additions & 176 deletions js/admin/dist/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -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";

Expand Down
8 changes: 5 additions & 3 deletions js/admin/src/components/DashboardPage.js
Original file line number Diff line number Diff line change
@@ -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/>];
}
}
60 changes: 0 additions & 60 deletions js/admin/src/components/StatisticsWidget.js

This file was deleted.

35 changes: 0 additions & 35 deletions less/admin/DashboardPage.less
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
35 changes: 0 additions & 35 deletions src/Admin/Controller/WebAppController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -73,37 +69,6 @@ protected function getView(ServerRequestInterface $request)
$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);
}
}

0 comments on commit 3dcfe32

Please sign in to comment.