From 3dcfe32b27f27f896b3aa22ec4085b6fcac005b9 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Sun, 10 Dec 2017 21:03:48 +1030 Subject: [PATCH] Extract admin dashboard statistics from core into an extension --- js/admin/dist/app.js | 184 +------------------- js/admin/src/components/DashboardPage.js | 8 +- js/admin/src/components/StatisticsWidget.js | 60 ------- less/admin/DashboardPage.less | 35 ---- src/Admin/Controller/WebAppController.php | 35 ---- 5 files changed, 13 insertions(+), 309 deletions(-) delete mode 100644 js/admin/src/components/StatisticsWidget.js diff --git a/js/admin/dist/app.js b/js/admin/dist/app.js index 7d1a68eb7..64c0aa374 100644 --- a/js/admin/dist/app.js +++ b/js/admin/dist/app.js @@ -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"; diff --git a/js/admin/src/components/DashboardPage.js b/js/admin/src/components/DashboardPage.js index c1a7bd888..30e0fb589 100644 --- a/js/admin/src/components/DashboardPage.js +++ b/js/admin/src/components/DashboardPage.js @@ -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 (
- - + {this.availableWidgets()}
); } + + availableWidgets() { + return []; + } } diff --git a/js/admin/src/components/StatisticsWidget.js b/js/admin/src/components/StatisticsWidget.js deleted file mode 100644 index a653a3c0c..000000000 --- a/js/admin/src/components/StatisticsWidget.js +++ /dev/null @@ -1,60 +0,0 @@ -/* - * This file is part of Flarum. - * - * (c) Toby Zerner - * - * 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 ( - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
{app.translator.trans('core.admin.statistics.users_heading')}{app.translator.trans('core.admin.statistics.discussions_heading')}{app.translator.trans('core.admin.statistics.posts_heading')}
{app.translator.trans('core.admin.statistics.total_label')}{app.data.statistics.total.users}{app.data.statistics.total.discussions}{app.data.statistics.total.posts}
{app.translator.trans('core.admin.statistics.last_28_days_label')}{app.data.statistics.month.users}{app.data.statistics.month.discussions}{app.data.statistics.month.posts}
{app.translator.trans('core.admin.statistics.last_7_days_label')}{app.data.statistics.week.users}{app.data.statistics.week.discussions}{app.data.statistics.week.posts}
{app.translator.trans('core.admin.statistics.today_label')}{app.data.statistics.today.users}{app.data.statistics.today.discussions}{app.data.statistics.today.posts}
- ); - } -} diff --git a/less/admin/DashboardPage.less b/less/admin/DashboardPage.less index 3c97eedc3..0ec7f6534 100644 --- a/less/admin/DashboardPage.less +++ b/less/admin/DashboardPage.less @@ -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; - } - } -} diff --git a/src/Admin/Controller/WebAppController.php b/src/Admin/Controller/WebAppController.php index 30c660ff3..a3d5a8100 100644 --- a/src/Admin/Controller/WebAppController.php +++ b/src/Admin/Controller/WebAppController.php @@ -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); - } }