1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| <?php
namespace App\Console\Commands;
use App\Services\Scheduling\ScheduleStatistics; use Illuminate\Console\Command;
class ScheduleMonitorCommand extends Command { protected $signature = 'schedule:monitor {--task= : Specific task to monitor} {--days=7 : Number of days to analyze} {--format=table : Output format (table|json)}'; protected $description = 'Monitor scheduled task performance'; public function handle(ScheduleStatistics $stats): int { if ($task = $this->option('task')) { return $this->monitorTask($stats, $task); } return $this->monitorAll($stats); } protected function monitorTask(ScheduleStatistics $stats, string $task): int { $statistics = $stats->getTaskStatistics($task, (int) $this->option('days')); if ($this->option('format') === 'json') { $this->line(json_encode($statistics, JSON_PRETTY_PRINT)); return self::SUCCESS; } $this->info("Task: {$task}"); $this->newLine(); $this->table( ['Metric', 'Value'], [ ['Total Executions', $statistics['total_executions']], ['Successful', $statistics['successful']], ['Failed', $statistics['failed']], ['Success Rate', $statistics['success_rate'] . '%'], ['Avg Duration', round($statistics['average_duration'] ?? 0, 2) . 's'], ['Max Duration', round($statistics['max_duration'] ?? 0, 2) . 's'], ['Avg Memory', $this->formatBytes($statistics['average_memory'] ?? 0)], ] ); return self::SUCCESS; } protected function monitorAll(ScheduleStatistics $stats): int { $overview = $stats->getOverview(); $slowest = $stats->getSlowestTasks(); $failing = $stats->getMostFailingTasks(); $this->info('=== Schedule Overview ==='); $this->table( ['Metric', 'Value'], [ ['Total Today', $overview['total_executions_today']], ['Successful Today', $overview['successful_today']], ['Failed Today', $overview['failed_today']], ['Avg Duration', round($overview['average_duration_today'] ?? 0, 2) . 's'], ] ); $this->newLine(); $this->info('=== Slowest Tasks (Last 7 Days) ==='); $this->table(['Task', 'Avg Duration'], $slowest); $this->newLine(); $this->info('=== Most Failing Tasks (Last 7 Days) ==='); $this->table(['Task', 'Total', 'Failures'], $failing); return self::SUCCESS; } protected function formatBytes(int $bytes): string { $units = ['B', 'KB', 'MB', 'GB']; $power = $bytes > 0 ? floor(log($bytes, 1024)) : 0; return number_format($bytes / pow(1024, $power), 2) . ' ' . $units[$power]; } }
|