|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PHPeek\LaravelQueueMetrics\Actions; |
| 6 | + |
| 7 | +use Carbon\Carbon; |
| 8 | +use PHPeek\LaravelQueueMetrics\DataTransferObjects\AggregatedJobMetricsData; |
| 9 | +use PHPeek\LaravelQueueMetrics\Repositories\Contracts\JobMetricsRepository; |
| 10 | + |
| 11 | +/** |
| 12 | + * Calculate aggregated metrics for a job class across all queues. |
| 13 | + */ |
| 14 | +final readonly class CalculateAggregatedJobMetricsAction |
| 15 | +{ |
| 16 | + public function __construct( |
| 17 | + private JobMetricsRepository $repository, |
| 18 | + private CalculateJobMetricsAction $calculateJobMetrics, |
| 19 | + ) {} |
| 20 | + |
| 21 | + public function execute(string $jobClass): AggregatedJobMetricsData |
| 22 | + { |
| 23 | + // Get all jobs from the discovery set |
| 24 | + $allJobs = $this->repository->listJobs(); |
| 25 | + |
| 26 | + // Filter to find all connection/queue combinations for this job class |
| 27 | + $jobQueues = array_filter($allJobs, fn (array $job): bool => $job['jobClass'] === $jobClass); |
| 28 | + |
| 29 | + // If no queues found, return empty aggregated metrics |
| 30 | + if (empty($jobQueues)) { |
| 31 | + return new AggregatedJobMetricsData( |
| 32 | + jobClass: $jobClass, |
| 33 | + totalExecutions: 0, |
| 34 | + totalFailures: 0, |
| 35 | + avgDurationMs: 0.0, |
| 36 | + avgMemoryMb: 0.0, |
| 37 | + failureRate: 0.0, |
| 38 | + throughputPerMinute: 0.0, |
| 39 | + byQueue: [], |
| 40 | + calculatedAt: Carbon::now(), |
| 41 | + ); |
| 42 | + } |
| 43 | + |
| 44 | + // Calculate metrics for each connection/queue combination |
| 45 | + $byQueue = []; |
| 46 | + $totalExecutions = 0; |
| 47 | + $totalFailures = 0; |
| 48 | + $weightedDuration = 0.0; |
| 49 | + $weightedMemory = 0.0; |
| 50 | + $totalThroughput = 0.0; |
| 51 | + |
| 52 | + foreach ($jobQueues as $job) { |
| 53 | + $metrics = $this->calculateJobMetrics->execute( |
| 54 | + $job['jobClass'], |
| 55 | + $job['connection'], |
| 56 | + $job['queue'] |
| 57 | + ); |
| 58 | + |
| 59 | + $executions = $metrics->execution->totalProcessed; |
| 60 | + $failures = $metrics->execution->totalFailed; |
| 61 | + |
| 62 | + // Accumulate totals |
| 63 | + $totalExecutions += $executions; |
| 64 | + $totalFailures += $failures; |
| 65 | + |
| 66 | + // Weighted averages (weight by number of executions) |
| 67 | + if ($executions > 0) { |
| 68 | + $weightedDuration += $metrics->duration->avg * $executions; |
| 69 | + $weightedMemory += $metrics->memory->avg * $executions; |
| 70 | + } |
| 71 | + |
| 72 | + $totalThroughput += $metrics->throughput->perMinute; |
| 73 | + |
| 74 | + // Calculate failure rate for this queue |
| 75 | + $totalJobs = $executions + $failures; |
| 76 | + $failureRate = $totalJobs > 0 ? ($failures / $totalJobs) * 100 : 0.0; |
| 77 | + |
| 78 | + // Add to by_queue breakdown |
| 79 | + $byQueue[] = [ |
| 80 | + 'connection' => $job['connection'], |
| 81 | + 'queue' => $job['queue'], |
| 82 | + 'executions' => $executions, |
| 83 | + 'failures' => $failures, |
| 84 | + 'avg_duration_ms' => round($metrics->duration->avg, 2), |
| 85 | + 'avg_memory_mb' => round($metrics->memory->avg, 2), |
| 86 | + 'failure_rate' => round($failureRate, 2), |
| 87 | + 'throughput_per_minute' => round($metrics->throughput->perMinute, 2), |
| 88 | + ]; |
| 89 | + } |
| 90 | + |
| 91 | + // Calculate overall weighted averages |
| 92 | + $avgDurationMs = $totalExecutions > 0 ? $weightedDuration / $totalExecutions : 0.0; |
| 93 | + $avgMemoryMb = $totalExecutions > 0 ? $weightedMemory / $totalExecutions : 0.0; |
| 94 | + |
| 95 | + // Calculate overall failure rate |
| 96 | + $totalJobs = $totalExecutions + $totalFailures; |
| 97 | + $failureRate = $totalJobs > 0 ? ($totalFailures / $totalJobs) * 100 : 0.0; |
| 98 | + |
| 99 | + return new AggregatedJobMetricsData( |
| 100 | + jobClass: $jobClass, |
| 101 | + totalExecutions: $totalExecutions, |
| 102 | + totalFailures: $totalFailures, |
| 103 | + avgDurationMs: $avgDurationMs, |
| 104 | + avgMemoryMb: $avgMemoryMb, |
| 105 | + failureRate: $failureRate, |
| 106 | + throughputPerMinute: $totalThroughput, |
| 107 | + byQueue: $byQueue, |
| 108 | + calculatedAt: Carbon::now(), |
| 109 | + ); |
| 110 | + } |
| 111 | +} |
0 commit comments