|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Jobs\Ookla; |
| 4 | + |
| 5 | +use App\Enums\ResultStatus; |
| 6 | +use App\Events\SpeedtestBenchmarking; |
| 7 | +use App\Models\Result; |
| 8 | +use App\Settings\ThresholdSettings; |
| 9 | +use Illuminate\Bus\Batchable; |
| 10 | +use Illuminate\Contracts\Queue\ShouldQueue; |
| 11 | +use Illuminate\Foundation\Queue\Queueable; |
| 12 | +use Illuminate\Support\Arr; |
| 13 | + |
| 14 | +class BenchmarkSpeedtestJob implements ShouldQueue |
| 15 | +{ |
| 16 | + use Batchable, Queueable; |
| 17 | + |
| 18 | + /** |
| 19 | + * Create a new job instance. |
| 20 | + */ |
| 21 | + public function __construct( |
| 22 | + public Result $result, |
| 23 | + ) {} |
| 24 | + |
| 25 | + /** |
| 26 | + * Execute the job. |
| 27 | + */ |
| 28 | + public function handle(): void |
| 29 | + { |
| 30 | + if ($this->batch()->cancelled()) { |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + $settings = app(ThresholdSettings::class); |
| 35 | + |
| 36 | + if ($settings->absolute_enabled === false) { |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + $this->result->update([ |
| 41 | + 'status' => ResultStatus::Benchmarking, |
| 42 | + ]); |
| 43 | + |
| 44 | + SpeedtestBenchmarking::dispatch($this->result); |
| 45 | + |
| 46 | + $benchmarks = $this->buildBenchmarks($settings); |
| 47 | + |
| 48 | + if (count($benchmarks) > 0) { |
| 49 | + $this->result->update([ |
| 50 | + 'benchmarks' => $benchmarks, |
| 51 | + ]); |
| 52 | + } else { |
| 53 | + return; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + private function buildBenchmarks(ThresholdSettings $settings): array |
| 58 | + { |
| 59 | + $benchmarks = []; |
| 60 | + |
| 61 | + if (! blank($settings->absolute_download) && $settings->absolute_download > 0) { |
| 62 | + $benchmarks = Arr::add($benchmarks, 'download', [ |
| 63 | + 'bar' => 'min', |
| 64 | + 'type' => 'absolute', |
| 65 | + 'value' => $settings->absolute_download, |
| 66 | + 'unit' => 'mbps', |
| 67 | + ]); |
| 68 | + } |
| 69 | + |
| 70 | + if (! blank($settings->absolute_upload) && $settings->absolute_upload > 0) { |
| 71 | + $benchmarks = Arr::add($benchmarks, 'upload', [ |
| 72 | + 'bar' => 'min', |
| 73 | + 'type' => 'absolute', |
| 74 | + 'value' => $settings->absolute_upload, |
| 75 | + 'unit' => 'mbps', |
| 76 | + ]); |
| 77 | + } |
| 78 | + |
| 79 | + if (! blank($settings->absolute_ping) && $settings->absolute_ping > 0) { |
| 80 | + $benchmarks = Arr::add($benchmarks, 'ping', [ |
| 81 | + 'bar' => 'max', |
| 82 | + 'type' => 'absolute', |
| 83 | + 'value' => $settings->absolute_ping, |
| 84 | + 'unit' => 'ms', |
| 85 | + ]); |
| 86 | + } |
| 87 | + |
| 88 | + return $benchmarks; |
| 89 | + } |
| 90 | +} |
0 commit comments