Passed
Push — master ( 9f15d2...3a106b )
by Darko
10:30
created

AdminPageController::getDefaultStats()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 13
rs 9.9332
cc 1
nc 1
nop 0
1
<?php
2
3
namespace App\Http\Controllers\Admin;
4
5
use App\Http\Controllers\BasePageController;
6
use App\Services\UserStatsService;
7
8
class AdminPageController extends BasePageController
9
{
10
    protected UserStatsService $userStatsService;
11
12
    public function __construct(UserStatsService $userStatsService)
13
    {
14
        parent::__construct();
15
        $this->userStatsService = $userStatsService;
16
    }
17
18
    /**
19
     * @throws \Exception
20
     */
21
    public function index()
22
    {
23
        $this->setAdminPrefs();
24
25
        // Get user statistics
26
        $userStats = [
27
            'users_by_role' => $this->userStatsService->getUsersByRole(),
28
            'downloads_per_day' => $this->userStatsService->getDownloadsPerDay(7),
29
            'api_hits_per_day' => $this->userStatsService->getApiHitsPerDay(7),
30
            'summary' => $this->userStatsService->getSummaryStats(),
31
            'top_downloaders' => $this->userStatsService->getTopDownloaders(5),
32
        ];
33
34
        return view('admin.dashboard', array_merge($this->viewData, [
35
            'meta_title' => 'Admin Home',
36
            'meta_description' => 'Admin home page',
37
            'userStats' => $userStats,
38
            'stats' => $this->getDefaultStats(),
39
        ]));
40
    }
41
42
    /**
43
     * Get default dashboard statistics
44
     */
45
    protected function getDefaultStats(): array
46
    {
47
        $today = now()->format('Y-m-d');
48
49
        return [
50
            'releases' => \App\Models\Release::count(),
51
            'releases_today' => \App\Models\Release::whereRaw('DATE(adddate) = ?', [$today])->count(),
52
            'users' => \App\Models\User::whereNull('deleted_at')->count(),
53
            'users_today' => \App\Models\User::whereRaw('DATE(created_at) = ?', [$today])->count(),
54
            'groups' => \App\Models\UsenetGroup::count(),
55
            'active_groups' => \App\Models\UsenetGroup::where('active', 1)->count(),
56
            'failed' => \App\Models\DnzbFailure::count(),
57
            'disk_free' => $this->getDiskSpace(),
58
        ];
59
    }
60
61
    /**
62
     * Get disk space information
63
     */
64
    protected function getDiskSpace(): string
65
    {
66
        try {
67
            $bytes = disk_free_space('/');
68
            $units = ['B', 'KB', 'MB', 'GB', 'TB'];
69
70
            for ($i = 0; $bytes > 1024 && $i < count($units) - 1; $i++) {
71
                $bytes /= 1024;
72
            }
73
74
            return round($bytes, 2).' '.$units[$i];
75
        } catch (\Exception $e) {
76
            return 'N/A';
77
        }
78
    }
79
}
80