[ 'server_info' => $systemInfo->getServerInfo(), 'php_info' => $systemInfo->getPhpInfo(), 'database_info' => $systemInfo->getDatabaseInfo(), 'performance' => $systemInfo->getPerformanceMetrics(), ], 'statistics' => [ 'users_online' => $this->getOnlineUsersCount(), 'total_requests' => $this->getTotalRequestsCount(), 'error_rate' => $this->getErrorRate(), 'response_time' => $this->getAverageResponseTime(), ], 'alerts' => $this->getSystemAlerts(), 'recent_actions' => $this->getRecentActions(), ]; }); } /** * 记录管理员操作 * * @param ADMIN_ACTION_TYPE $actionType * @param string $description * @param array $data * @return void */ public function logAdminAction(ADMIN_ACTION_TYPE $actionType, string $description, array $data = []): void { $adminUser = Auth::guard('admin')->user(); $logData = [ 'admin_id' => $adminUser?->id, 'admin_name' => $adminUser?->name ?? 'System', 'action_type' => $actionType->value, 'description' => $description, 'data' => $data, 'ip_address' => request()->ip(), 'user_agent' => request()->userAgent(), 'timestamp' => now(), ]; // 触发事件 Event::dispatch(new AdminActionEvent($logData)); // 记录日志 if ($actionType->needsDetailedLog()) { Log::channel('admin')->info("Admin Action: {$description}", $logData); } // 危险操作额外记录 if ($actionType->isDangerous()) { Log::channel('admin')->warning("Dangerous Admin Action: {$description}", $logData); } } /** * 获取在线用户数量 * * @return int */ protected function getOnlineUsersCount(): int { // 这里可以根据实际需求实现 // 例如:统计最近5分钟内有活动的用户 return Cache::remember('admin.stats.online_users', 60, function () { // 实现逻辑 return 0; }); } /** * 获取总请求数 * * @return int */ protected function getTotalRequestsCount(): int { return Cache::remember('admin.stats.total_requests', 300, function () { // 实现逻辑 return 0; }); } /** * 获取错误率 * * @return float */ protected function getErrorRate(): float { return Cache::remember('admin.stats.error_rate', 300, function () { // 实现逻辑 return 0.0; }); } /** * 获取平均响应时间 * * @return float */ protected function getAverageResponseTime(): float { return Cache::remember('admin.stats.response_time', 300, function () { // 实现逻辑 return 0.0; }); } /** * 获取系统警报 * * @return array */ protected function getSystemAlerts(): array { $alerts = []; $systemInfo = new SystemInfo(); $config = config('admin_module.monitoring.alerts'); // CPU使用率警报 $cpuUsage = $systemInfo->getCpuUsage(); if ($cpuUsage > $config['cpu_threshold']) { $alerts[] = [ 'type' => 'warning', 'title' => 'CPU使用率过高', 'message' => "当前CPU使用率: {$cpuUsage}%", 'timestamp' => now(), ]; } // 内存使用率警报 $memoryUsage = $systemInfo->getMemoryUsage(); if ($memoryUsage['percentage'] > $config['memory_threshold']) { $alerts[] = [ 'type' => 'warning', 'title' => '内存使用率过高', 'message' => "当前内存使用率: {$memoryUsage['percentage']}%", 'timestamp' => now(), ]; } // 磁盘使用率警报 $diskUsage = $systemInfo->getDiskUsage(); if ($diskUsage['percentage'] > $config['disk_threshold']) { $alerts[] = [ 'type' => 'danger', 'title' => '磁盘空间不足', 'message' => "当前磁盘使用率: {$diskUsage['percentage']}%", 'timestamp' => now(), ]; } return $alerts; } /** * 获取最近的操作记录 * * @param int $limit * @return array */ protected function getRecentActions(int $limit = 10): array { return Cache::remember("admin.recent_actions.{$limit}", 60, function () use ($limit) { // 这里应该从数据库获取最近的操作记录 // 暂时返回空数组 return []; }); } /** * 获取系统状态 * * @return array */ public function getSystemStatus(): array { $systemInfo = new SystemInfo(); return [ 'status' => 'running', 'uptime' => $systemInfo->getUptime(), 'load_average' => $systemInfo->getLoadAverage(), 'memory_usage' => $systemInfo->getMemoryUsage(), 'disk_usage' => $systemInfo->getDiskUsage(), 'database_status' => $systemInfo->getDatabaseStatus(), 'cache_status' => $this->getCacheStatus(), 'queue_status' => $this->getQueueStatus(), ]; } /** * 获取缓存状态 * * @return array */ protected function getCacheStatus(): array { try { Cache::put('admin.cache.test', 'test', 1); $test = Cache::get('admin.cache.test'); Cache::forget('admin.cache.test'); return [ 'status' => $test === 'test' ? 'healthy' : 'error', 'driver' => config('cache.default'), ]; } catch (\Exception $e) { return [ 'status' => 'error', 'error' => $e->getMessage(), ]; } } /** * 获取队列状态 * * @return array */ protected function getQueueStatus(): array { try { return [ 'status' => 'healthy', 'driver' => config('queue.default'), 'pending_jobs' => 0, // 这里可以实现获取待处理任务数量的逻辑 ]; } catch (\Exception $e) { return [ 'status' => 'error', 'error' => $e->getMessage(), ]; } } /** * 执行系统维护 * * @param array $options * @return array */ public function performMaintenance(array $options = []): array { $results = []; // 清理缓存 if ($options['clear_cache'] ?? true) { $cacheService = app('admin.cache'); $results['cache'] = $cacheService->clearAll(); } // 清理日志 if ($options['clean_logs'] ?? false) { $logService = app('admin.log'); $results['logs'] = $logService->cleanOldLogs(); } // 优化数据库 if ($options['optimize_database'] ?? false) { $results['database'] = $this->optimizeDatabase(); } // 记录维护操作 $this->logAdminAction( ADMIN_ACTION_TYPE::MAINTENANCE, '执行系统维护', ['options' => $options, 'results' => $results] ); return $results; } /** * 优化数据库 * * @return array */ protected function optimizeDatabase(): array { try { // 这里可以实现数据库优化逻辑 return [ 'status' => 'success', 'message' => '数据库优化完成', ]; } catch (\Exception $e) { return [ 'status' => 'error', 'message' => $e->getMessage(), ]; } } }