blessing-skin-server/app/Http/Controllers/AdminController.php

164 lines
5.1 KiB
PHP
Raw Normal View History

2016-07-22 19:36:24 +08:00
<?php
2016-08-28 10:05:21 +08:00
namespace App\Http\Controllers;
2016-07-22 19:36:24 +08:00
2016-07-23 15:20:10 +08:00
use App\Models\Player;
use App\Models\Texture;
2019-12-14 11:10:37 +08:00
use App\Models\User;
2019-08-28 16:04:26 +08:00
use App\Services\PluginManager;
use Blessing\Filter;
2019-12-14 11:10:37 +08:00
use Carbon\Carbon;
use Illuminate\Filesystem\Filesystem;
2019-12-14 11:10:37 +08:00
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
2016-07-22 19:36:24 +08:00
class AdminController extends Controller
2016-07-22 19:36:24 +08:00
{
2019-12-15 21:04:20 +08:00
public function index(Filter $filter)
2019-09-17 23:10:44 +08:00
{
2019-12-15 21:04:20 +08:00
$grid = [
'layout' => [
['md-6', 'md-6'],
],
'widgets' => [
[
[
'admin.widgets.dashboard.usage',
'admin.widgets.dashboard.notification',
],
['admin.widgets.dashboard.chart'],
],
],
];
$grid = $filter->apply('grid:admin.index', $grid);
2019-09-17 23:10:44 +08:00
return view('admin.index', [
2019-12-15 21:04:20 +08:00
'grid' => $grid,
2019-09-17 23:10:44 +08:00
'sum' => [
'users' => User::count(),
'players' => Player::count(),
'textures' => Texture::count(),
'storage' => Texture::select('size')->sum('size'),
],
]);
}
2019-09-07 11:15:23 +08:00
public function chartData()
2016-07-22 19:36:24 +08:00
{
2019-12-22 16:18:59 +08:00
$xAxis = Collection::times(31, function ($i) {
return Carbon::today()->subDays(31 - $i)->format('m-d');
2019-03-19 23:35:13 +08:00
});
2019-12-22 16:18:59 +08:00
$oneMonthAgo = Carbon::today()->subMonth();
2019-03-19 23:35:13 +08:00
2019-03-19 23:48:45 +08:00
$grouping = function ($field) {
return function ($item) use ($field) {
return substr($item->$field, 5, 5);
};
};
$mapping = function ($item) {
return count($item);
};
$aligning = function ($data) {
return function ($day) use ($data) {
return $data->get($day) ?? 0;
};
};
2019-08-24 10:22:26 +08:00
$userRegistration = User::where('register_at', '>=', $oneMonthAgo)
2019-03-19 23:35:13 +08:00
->select('register_at')
->get()
2019-03-19 23:48:45 +08:00
->groupBy($grouping('register_at'))
->map($mapping);
2019-03-19 23:35:13 +08:00
$textureUploads = Texture::where('upload_at', '>=', $oneMonthAgo)
->select('upload_at')
->get()
2019-03-19 23:48:45 +08:00
->groupBy($grouping('upload_at'))
->map($mapping);
2019-03-19 19:16:03 +08:00
return [
'labels' => [
trans('admin.index.user-registration'),
2019-04-19 19:36:36 +08:00
trans('admin.index.texture-uploads'),
],
2019-03-19 19:16:03 +08:00
'xAxis' => $xAxis,
2019-03-19 23:35:13 +08:00
'data' => [
2019-03-19 23:48:45 +08:00
$xAxis->map($aligning($userRegistration)),
$xAxis->map($aligning($textureUploads)),
2019-04-19 19:36:36 +08:00
],
2018-02-24 17:08:32 +08:00
];
2016-07-22 19:36:24 +08:00
}
public function status(
Request $request,
PluginManager $plugins,
2019-12-15 19:04:30 +08:00
Filesystem $filesystem,
Filter $filter
) {
2019-12-21 15:50:29 +08:00
$db = config('database.connections.'.config('database.default'));
$dbType = Arr::get([
'mysql' => 'MySQL/MariaDB',
'sqlite' => 'SQLite',
'pgsql' => 'PostgreSQL',
], config('database.default'), '');
2019-08-28 16:04:26 +08:00
$enabledPlugins = $plugins->getEnabledPlugins()->map(function ($plugin) {
return ['title' => trans($plugin->title), 'version' => $plugin->version];
});
2019-08-28 14:52:51 +08:00
if ($filesystem->exists(base_path('.git'))) {
$process = new \Symfony\Component\Process\Process(
['git', 'log', '--pretty=%H', '-1']
);
$process->run();
$commit = $process->isSuccessful() ? trim($process->getOutput()) : '';
}
2019-12-15 19:04:30 +08:00
$grid = [
'layout' => [
2019-12-15 21:04:20 +08:00
['md-6', 'md-6'],
2019-12-15 19:04:30 +08:00
],
'widgets' => [
[
['admin.widgets.status.info'],
2020-03-10 15:20:42 +08:00
['admin.widgets.status.plugins'],
2019-12-15 21:04:20 +08:00
],
],
2019-12-15 19:04:30 +08:00
];
$grid = $filter->apply('grid:admin.status', $grid);
2019-08-28 14:52:51 +08:00
return view('admin.status')
2019-12-15 19:04:30 +08:00
->with('grid', $grid)
2019-08-28 14:52:51 +08:00
->with('detail', [
2019-08-28 16:04:26 +08:00
'bs' => [
'version' => config('app.version'),
'env' => config('app.env'),
'debug' => config('app.debug') ? trans('general.yes') : trans('general.no'),
'commit' => Str::limit(
$commit ?? resolve(\App\Services\Webpack::class)->commit,
16,
''
),
2019-08-28 16:04:26 +08:00
'laravel' => app()->version(),
],
2019-08-28 14:52:51 +08:00
'server' => [
'php' => PHP_VERSION,
'web' => $request->server('SERVER_SOFTWARE', trans('general.unknown')),
'os' => sprintf('%s %s %s', php_uname('s'), php_uname('r'), php_uname('m')),
],
'db' => [
2019-12-21 15:50:29 +08:00
'type' => $dbType,
2019-08-28 14:52:51 +08:00
'host' => Arr::get($db, 'host', ''),
'port' => Arr::get($db, 'port', ''),
'username' => Arr::get($db, 'username'),
'database' => Arr::get($db, 'database'),
'prefix' => Arr::get($db, 'prefix'),
],
2019-08-28 16:04:26 +08:00
])
->with('plugins', $enabledPlugins);
2019-08-28 11:39:00 +08:00
}
2016-07-22 19:36:24 +08:00
}