| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- <?php
- namespace App\Module\Game\Services;
- use App\Module\Game\Jobs\CollectUserLogJob;
- use App\Module\Game\Logics\UserLogCollectorManager;
- use Illuminate\Support\Facades\Log;
- /**
- * 用户日志调度服务
- *
- * 负责管理用户日志收集的调度任务
- */
- class UserLogScheduleService
- {
- /**
- * 执行日志收集(同步方式)
- *
- * @param string|null $collectorName 收集器名称,null表示执行所有收集器
- * @return array 执行结果
- */
- public static function collectNow(?string $collectorName = null): array
- {
- try {
- $manager = new UserLogCollectorManager();
- if ($collectorName) {
- return $manager->collectByName($collectorName);
- } else {
- return $manager->collectAll();
- }
- } catch (\Exception $e) {
- Log::error('同步执行日志收集失败', [
- 'collector' => $collectorName,
- 'error' => $e->getMessage()
- ]);
- return [
- 'status' => 'error',
- 'error' => $e->getMessage(),
- 'timestamp' => now()->toDateTimeString()
- ];
- }
- }
- /**
- * 调度日志收集任务(异步方式)
- *
- * @param string|null $collectorName 收集器名称,null表示执行所有收集器
- * @return void
- */
- public static function scheduleCollection(?string $collectorName = null): void
- {
- CollectUserLogJob::dispatchCollection($collectorName);
- }
- /**
- * 获取收集器信息
- *
- * @return array
- */
- public static function getCollectorsInfo(): array
- {
- try {
- $manager = new UserLogCollectorManager();
- return $manager->getCollectorsInfo();
- } catch (\Exception $e) {
- Log::error('获取收集器信息失败', [
- 'error' => $e->getMessage()
- ]);
- return [];
- }
- }
- /**
- * 重置收集器进度
- *
- * @param string|null $collectorName 收集器名称,null表示重置所有收集器
- * @return bool
- */
- public static function resetCollector(?string $collectorName = null): bool
- {
- try {
- $manager = new UserLogCollectorManager();
- if ($collectorName) {
- $manager->resetCollector($collectorName);
- } else {
- $manager->resetAllCollectors();
- }
- return true;
- } catch (\Exception $e) {
- Log::error('重置收集器进度失败', [
- 'collector' => $collectorName,
- 'error' => $e->getMessage()
- ]);
- return false;
- }
- }
- /**
- * 检查收集器状态
- *
- * @return array
- */
- public static function checkCollectorsStatus(): array
- {
- try {
- $manager = new UserLogCollectorManager();
- $collectorsInfo = $manager->getCollectorsInfo();
- $status = [];
- foreach ($collectorsInfo as $name => $info) {
- $cacheKey = "user_log_collector:last_processed_id:{$info['source_table']}";
- $lastProcessedId = \Illuminate\Support\Facades\Cache::get($cacheKey, 0);
-
- $status[$name] = [
- 'name' => $name,
- 'source_table' => $info['source_table'],
- 'source_type' => $info['source_type'],
- 'last_processed_id' => $lastProcessedId,
- 'last_check_time' => now()->toDateTimeString(),
- ];
- }
- return $status;
- } catch (\Exception $e) {
- Log::error('检查收集器状态失败', [
- 'error' => $e->getMessage()
- ]);
- return [];
- }
- }
- /**
- * 获取收集统计信息
- *
- * @param int $days 统计天数
- * @return array
- */
- public static function getCollectionStats(int $days = 7): array
- {
- try {
- $stats = [
- 'period' => "{$days}天",
- 'start_date' => now()->subDays($days)->toDateString(),
- 'end_date' => now()->toDateString(),
- 'total_logs' => 0,
- 'daily_stats' => [],
- 'source_type_stats' => [],
- ];
- // 获取总日志数
- $totalLogs = \App\Module\Game\Models\UserLog::where('created_at', '>=', now()->subDays($days))
- ->count();
- $stats['total_logs'] = $totalLogs;
- // 获取每日统计
- $dailyStats = \App\Module\Game\Models\UserLog::where('created_at', '>=', now()->subDays($days))
- ->selectRaw('DATE(created_at) as date, COUNT(*) as count')
- ->groupBy('date')
- ->orderBy('date')
- ->get()
- ->pluck('count', 'date')
- ->toArray();
- $stats['daily_stats'] = $dailyStats;
- // 获取来源类型统计
- $sourceTypeStats = \App\Module\Game\Models\UserLog::where('created_at', '>=', now()->subDays($days))
- ->selectRaw('source_type, COUNT(*) as count')
- ->whereNotNull('source_type')
- ->groupBy('source_type')
- ->get()
- ->pluck('count', 'source_type')
- ->toArray();
- $stats['source_type_stats'] = $sourceTypeStats;
- return $stats;
- } catch (\Exception $e) {
- Log::error('获取收集统计信息失败', [
- 'error' => $e->getMessage()
- ]);
- return [];
- }
- }
- /**
- * 健康检查
- *
- * @return array
- */
- public static function healthCheck(): array
- {
- try {
- $health = [
- 'status' => 'healthy',
- 'checks' => [],
- 'timestamp' => now()->toDateTimeString(),
- ];
- // 检查收集器是否正常注册
- $manager = new UserLogCollectorManager();
- $collectorsInfo = $manager->getCollectorsInfo();
-
- $health['checks']['collectors_registered'] = [
- 'status' => !empty($collectorsInfo) ? 'pass' : 'fail',
- 'count' => count($collectorsInfo),
- 'details' => array_keys($collectorsInfo),
- ];
- // 检查最近是否有日志生成
- $recentLogsCount = \App\Module\Game\Models\UserLog::where('created_at', '>=', now()->subMinutes(10))
- ->count();
-
- $health['checks']['recent_logs'] = [
- 'status' => 'info',
- 'count' => $recentLogsCount,
- 'message' => "最近10分钟生成了 {$recentLogsCount} 条日志",
- ];
- // 检查数据库连接
- try {
- \App\Module\Game\Models\UserLog::count();
- $health['checks']['database'] = [
- 'status' => 'pass',
- 'message' => '数据库连接正常',
- ];
- } catch (\Exception $e) {
- $health['checks']['database'] = [
- 'status' => 'fail',
- 'message' => '数据库连接失败: ' . $e->getMessage(),
- ];
- $health['status'] = 'unhealthy';
- }
- return $health;
- } catch (\Exception $e) {
- return [
- 'status' => 'unhealthy',
- 'error' => $e->getMessage(),
- 'timestamp' => now()->toDateTimeString(),
- ];
- }
- }
- }
|