| Total Complexity | 49 |
| Total Lines | 441 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like AdminPageController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AdminPageController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class AdminPageController extends BasePageController |
||
| 10 | { |
||
| 11 | protected UserStatsService $userStatsService; |
||
| 12 | |||
| 13 | protected SystemMetricsService $systemMetricsService; |
||
| 14 | |||
| 15 | public function __construct(UserStatsService $userStatsService, SystemMetricsService $systemMetricsService) |
||
| 20 | } |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @throws \Exception |
||
| 24 | */ |
||
| 25 | public function index() |
||
| 47 | ])); |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Get recent user activity from the user_activities table |
||
| 52 | */ |
||
| 53 | protected function getRecentUserActivity(): array |
||
| 54 | { |
||
| 55 | $activities = \App\Models\UserActivity::orderBy('created_at', 'desc') |
||
| 56 | ->limit(10) |
||
| 57 | ->get(); |
||
| 58 | |||
| 59 | return $activities->map(function ($activity) { |
||
| 60 | return (object) [ |
||
| 61 | 'type' => $activity->activity_type, |
||
| 62 | 'message' => $activity->description, |
||
| 63 | 'icon' => $activity->icon, |
||
| 64 | 'icon_bg' => 'bg-'.$activity->color_class.'-100 dark:bg-'.$activity->color_class.'-900', |
||
| 65 | 'icon_color' => 'text-'.$activity->color_class.'-600 dark:text-'.$activity->color_class.'-400', |
||
| 66 | 'created_at' => $activity->created_at, |
||
| 67 | 'username' => $activity->username, |
||
| 68 | ]; |
||
| 69 | })->toArray(); |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * API endpoint to get recent user activity (for auto-refresh) |
||
| 74 | */ |
||
| 75 | public function getRecentActivity() |
||
| 76 | { |
||
| 77 | $activities = $this->getRecentUserActivity(); |
||
| 78 | |||
| 79 | return response()->json([ |
||
| 80 | 'success' => true, |
||
| 81 | 'activities' => array_map(function ($activity) { |
||
| 82 | return [ |
||
| 83 | 'type' => $activity->type, |
||
| 84 | 'message' => $activity->message, |
||
| 85 | 'icon' => $activity->icon, |
||
| 86 | 'icon_bg' => $activity->icon_bg, |
||
| 87 | 'icon_color' => $activity->icon_color, |
||
| 88 | 'created_at' => $activity->created_at->diffForHumans(), |
||
| 89 | 'username' => $activity->username, |
||
| 90 | ]; |
||
| 91 | }, $activities), |
||
| 92 | ]); |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Get default dashboard statistics |
||
| 97 | */ |
||
| 98 | protected function getDefaultStats(): array |
||
| 99 | { |
||
| 100 | $today = now()->format('Y-m-d'); |
||
| 101 | |||
| 102 | return [ |
||
| 103 | 'releases' => \App\Models\Release::count(), |
||
| 104 | 'releases_today' => \App\Models\Release::whereRaw('DATE(adddate) = ?', [$today])->count(), |
||
| 105 | 'users' => \App\Models\User::whereNull('deleted_at')->count(), |
||
| 106 | 'users_today' => \App\Models\User::whereRaw('DATE(created_at) = ?', [$today])->count(), |
||
| 107 | 'groups' => \App\Models\UsenetGroup::count(), |
||
| 108 | 'active_groups' => \App\Models\UsenetGroup::where('active', 1)->count(), |
||
| 109 | 'failed' => \App\Models\DnzbFailure::count(), |
||
| 110 | 'disk_free' => $this->getDiskSpace(), |
||
| 111 | ]; |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Get disk space information |
||
| 116 | */ |
||
| 117 | protected function getDiskSpace(): string |
||
| 118 | { |
||
| 119 | try { |
||
| 120 | $bytes = disk_free_space('/'); |
||
| 121 | $units = ['B', 'KB', 'MB', 'GB', 'TB']; |
||
| 122 | |||
| 123 | for ($i = 0; $bytes > 1024 && $i < count($units) - 1; $i++) { |
||
| 124 | $bytes /= 1024; |
||
| 125 | } |
||
| 126 | |||
| 127 | return round($bytes, 2).' '.$units[$i]; |
||
| 128 | } catch (\Exception $e) { |
||
| 129 | return 'N/A'; |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Get system metrics (CPU and RAM usage) |
||
| 135 | */ |
||
| 136 | protected function getSystemMetrics(): array |
||
| 137 | { |
||
| 138 | $cpuUsage = $this->getCpuUsage(); |
||
| 139 | $ramUsage = $this->getRamUsage(); |
||
| 140 | $cpuInfo = $this->getCpuInfo(); |
||
| 141 | $loadAverage = $this->getLoadAverage(); |
||
| 142 | |||
| 143 | // Get historical data from database - both hourly (24h) and daily (30d) |
||
| 144 | $cpuHistory24h = $this->systemMetricsService->getHourlyMetrics('cpu', 24); |
||
| 145 | $cpuHistory30d = $this->systemMetricsService->getDailyMetrics('cpu', 30); |
||
| 146 | $ramHistory24h = $this->systemMetricsService->getHourlyMetrics('ram', 24); |
||
| 147 | $ramHistory30d = $this->systemMetricsService->getDailyMetrics('ram', 30); |
||
| 148 | |||
| 149 | return [ |
||
| 150 | 'cpu' => [ |
||
| 151 | 'current' => $cpuUsage, |
||
| 152 | 'label' => 'CPU Usage', |
||
| 153 | 'history_24h' => $cpuHistory24h, |
||
| 154 | 'history_30d' => $cpuHistory30d, |
||
| 155 | 'cores' => $cpuInfo['cores'], |
||
| 156 | 'threads' => $cpuInfo['threads'], |
||
| 157 | 'model' => $cpuInfo['model'], |
||
| 158 | 'load_average' => $loadAverage, |
||
| 159 | ], |
||
| 160 | 'ram' => [ |
||
| 161 | 'used' => $ramUsage['used'], |
||
| 162 | 'total' => $ramUsage['total'], |
||
| 163 | 'percentage' => $ramUsage['percentage'], |
||
| 164 | 'label' => 'RAM Usage', |
||
| 165 | 'history_24h' => $ramHistory24h, |
||
| 166 | 'history_30d' => $ramHistory30d, |
||
| 167 | ], |
||
| 168 | ]; |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Get current CPU usage percentage |
||
| 173 | */ |
||
| 174 | protected function getCpuUsage(): float |
||
| 175 | { |
||
| 176 | try { |
||
| 177 | if (PHP_OS_FAMILY === 'Windows') { |
||
| 178 | // Windows command |
||
| 179 | $output = shell_exec('wmic cpu get loadpercentage'); |
||
| 180 | if ($output) { |
||
| 181 | preg_match('/\d+/', $output, $matches); |
||
| 182 | |||
| 183 | return $matches[0] ?? 0; |
||
| 184 | } |
||
| 185 | } else { |
||
| 186 | // Linux command - get load average and convert to percentage |
||
| 187 | $load = sys_getloadavg(); |
||
| 188 | if ($load !== false) { |
||
| 189 | $cpuCount = $this->getCpuCount(); |
||
| 190 | |||
| 191 | return round(($load[0] / $cpuCount) * 100, 2); |
||
| 192 | } |
||
| 193 | } |
||
| 194 | } catch (\Exception $e) { |
||
| 195 | \Log::warning('Could not get CPU usage: '.$e->getMessage()); |
||
| 196 | } |
||
| 197 | |||
| 198 | return 0; |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Get number of CPU cores |
||
| 203 | */ |
||
| 204 | protected function getCpuCount(): int |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Get detailed CPU information (cores, threads, model) |
||
| 229 | */ |
||
| 230 | protected function getCpuInfo(): array |
||
| 231 | { |
||
| 232 | $info = [ |
||
| 233 | 'cores' => 0, |
||
| 234 | 'threads' => 0, |
||
| 235 | 'model' => 'Unknown', |
||
| 236 | ]; |
||
| 237 | |||
| 238 | try { |
||
| 239 | if (PHP_OS_FAMILY === 'Windows') { |
||
| 240 | // Get number of cores |
||
| 241 | $coresOutput = shell_exec('wmic cpu get NumberOfCores'); |
||
| 242 | if ($coresOutput) { |
||
| 243 | preg_match('/\d+/', $coresOutput, $matches); |
||
| 244 | $info['cores'] = (int) ($matches[0] ?? 0); |
||
| 245 | } |
||
| 246 | |||
| 247 | // Get number of logical processors (threads) |
||
| 248 | $threadsOutput = shell_exec('wmic cpu get NumberOfLogicalProcessors'); |
||
| 249 | if ($threadsOutput) { |
||
| 250 | preg_match('/\d+/', $threadsOutput, $matches); |
||
| 251 | $info['threads'] = (int) ($matches[0] ?? 0); |
||
| 252 | } |
||
| 253 | |||
| 254 | // Get CPU model |
||
| 255 | $modelOutput = shell_exec('wmic cpu get Name'); |
||
| 256 | if ($modelOutput) { |
||
| 257 | $lines = explode("\n", trim($modelOutput)); |
||
| 258 | if (isset($lines[1])) { |
||
| 259 | $info['model'] = trim($lines[1]); |
||
| 260 | } |
||
| 261 | } |
||
| 262 | } else { |
||
| 263 | // Linux |
||
| 264 | $cpuinfo = file_get_contents('/proc/cpuinfo'); |
||
| 265 | |||
| 266 | // Get number of physical cores |
||
| 267 | preg_match_all('/^cpu cores\s*:\s*(\d+)/m', $cpuinfo, $coresMatches); |
||
| 268 | if (! empty($coresMatches[1])) { |
||
| 269 | $info['cores'] = (int) $coresMatches[1][0]; |
||
| 270 | } |
||
| 271 | |||
| 272 | // Get number of logical processors (threads) |
||
| 273 | preg_match_all('/^processor/m', $cpuinfo, $processorMatches); |
||
| 274 | $info['threads'] = count($processorMatches[0]) ?: 0; |
||
| 275 | |||
| 276 | // Get CPU model |
||
| 277 | preg_match('/^model name\s*:\s*(.+)$/m', $cpuinfo, $modelMatches); |
||
| 278 | if (! empty($modelMatches[1])) { |
||
| 279 | $info['model'] = trim($modelMatches[1]); |
||
| 280 | } |
||
| 281 | |||
| 282 | // If cores is 0, try to get from physical id count |
||
| 283 | if ($info['cores'] === 0) { |
||
| 284 | preg_match_all('/^physical id\s*:\s*(\d+)/m', $cpuinfo, $physicalMatches); |
||
| 285 | $uniquePhysical = ! empty($physicalMatches[1]) ? count(array_unique($physicalMatches[1])) : 1; |
||
| 286 | $info['cores'] = (int) ($info['threads'] / $uniquePhysical); |
||
| 287 | } |
||
| 288 | } |
||
| 289 | } catch (\Exception $e) { |
||
| 290 | \Log::warning('Could not get CPU info: '.$e->getMessage()); |
||
| 291 | } |
||
| 292 | |||
| 293 | return $info; |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Get system load average |
||
| 298 | */ |
||
| 299 | protected function getLoadAverage(): array |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Get RAM usage information |
||
| 337 | */ |
||
| 338 | protected function getRamUsage(): array |
||
| 339 | { |
||
| 340 | try { |
||
| 341 | if (PHP_OS_FAMILY === 'Windows') { |
||
| 342 | // Windows command |
||
| 343 | $output = shell_exec('wmic OS get FreePhysicalMemory,TotalVisibleMemorySize /Value'); |
||
| 344 | if ($output) { |
||
| 345 | preg_match('/FreePhysicalMemory=(\d+)/', $output, $free); |
||
| 346 | preg_match('/TotalVisibleMemorySize=(\d+)/', $output, $total); |
||
| 347 | |||
| 348 | if (isset($free[1]) && isset($total[1])) { |
||
| 349 | $freeKb = (float) $free[1]; |
||
| 350 | $totalKb = (float) $total[1]; |
||
| 351 | $usedKb = $totalKb - $freeKb; |
||
| 352 | |||
| 353 | return [ |
||
| 354 | 'used' => round($usedKb / 1024 / 1024, 2), |
||
| 355 | 'total' => round($totalKb / 1024 / 1024, 2), |
||
| 356 | 'percentage' => round(($usedKb / $totalKb) * 100, 2), |
||
| 357 | ]; |
||
| 358 | } |
||
| 359 | } |
||
| 360 | } else { |
||
| 361 | // Linux command |
||
| 362 | $meminfo = file_get_contents('/proc/meminfo'); |
||
| 363 | preg_match('/MemTotal:\s+(\d+)/', $meminfo, $total); |
||
| 364 | preg_match('/MemAvailable:\s+(\d+)/', $meminfo, $available); |
||
| 365 | |||
| 366 | if (isset($total[1]) && isset($available[1])) { |
||
| 367 | $totalKb = (float) $total[1]; |
||
| 368 | $availableKb = (float) $available[1]; |
||
| 369 | $usedKb = $totalKb - $availableKb; |
||
| 370 | |||
| 371 | return [ |
||
| 372 | 'used' => round($usedKb / 1024 / 1024, 2), |
||
| 373 | 'total' => round($totalKb / 1024 / 1024, 2), |
||
| 374 | 'percentage' => round(($usedKb / $totalKb) * 100, 2), |
||
| 375 | ]; |
||
| 376 | } |
||
| 377 | } |
||
| 378 | } catch (\Exception $e) { |
||
| 379 | \Log::warning('Could not get RAM usage: '.$e->getMessage()); |
||
| 380 | } |
||
| 381 | |||
| 382 | return [ |
||
| 383 | 'used' => 0, |
||
| 384 | 'total' => 0, |
||
| 385 | 'percentage' => 0, |
||
| 386 | ]; |
||
| 387 | } |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Get minute-to-minute user activity data (API endpoint) |
||
| 391 | */ |
||
| 392 | public function getUserActivityMinutes() |
||
| 400 | ]); |
||
| 401 | } |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Get current system metrics (API endpoint) |
||
| 405 | */ |
||
| 406 | public function getCurrentMetrics() |
||
| 426 | ], |
||
| 427 | ]); |
||
| 428 | } |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Get historical system metrics (API endpoint) |
||
| 432 | */ |
||
| 433 | public function getHistoricalMetrics() |
||
| 453 |