diff --git a/app/Enums/ResultStatus.php b/app/Enums/ResultStatus.php index d93dc34b5..539ee7a1c 100644 --- a/app/Enums/ResultStatus.php +++ b/app/Enums/ResultStatus.php @@ -8,6 +8,7 @@ enum ResultStatus: string implements HasColor, HasLabel { + case Benchmarking = 'benchmarking'; case Checking = 'checking'; case Completed = 'completed'; case Failed = 'failed'; @@ -18,6 +19,7 @@ enum ResultStatus: string implements HasColor, HasLabel public function getColor(): ?string { return match ($this) { + self::Benchmarking => 'info', self::Checking => 'info', self::Completed => 'success', self::Failed => 'danger', diff --git a/app/Events/SpeedtestBenchmarking.php b/app/Events/SpeedtestBenchmarking.php new file mode 100644 index 000000000..a20ecb377 --- /dev/null +++ b/app/Events/SpeedtestBenchmarking.php @@ -0,0 +1,20 @@ +batch()->cancelled()) { + return; + } + + $settings = app(ThresholdSettings::class); + + if ($settings->absolute_enabled === false) { + return; + } + + $this->result->update([ + 'status' => ResultStatus::Benchmarking, + ]); + + SpeedtestBenchmarking::dispatch($this->result); + + $benchmarks = $this->buildBenchmarks($settings); + + if (count($benchmarks) > 0) { + $this->result->update([ + 'benchmarks' => $benchmarks, + ]); + } else { + return; + } + } + + private function buildBenchmarks(ThresholdSettings $settings): array + { + $benchmarks = []; + + if (! blank($settings->absolute_download) && $settings->absolute_download > 0) { + $benchmarks = Arr::add($benchmarks, 'download', [ + 'bar' => 'min', + 'type' => 'absolute', + 'value' => $settings->absolute_download, + 'unit' => 'mbps', + ]); + } + + if (! blank($settings->absolute_upload) && $settings->absolute_upload > 0) { + $benchmarks = Arr::add($benchmarks, 'upload', [ + 'bar' => 'min', + 'type' => 'absolute', + 'value' => $settings->absolute_upload, + 'unit' => 'mbps', + ]); + } + + if (! blank($settings->absolute_ping) && $settings->absolute_ping > 0) { + $benchmarks = Arr::add($benchmarks, 'ping', [ + 'bar' => 'max', + 'type' => 'absolute', + 'value' => $settings->absolute_ping, + 'unit' => 'ms', + ]); + } + + return $benchmarks; + } +} diff --git a/app/Jobs/Ookla/CompleteSpeedtestJob.php b/app/Jobs/Ookla/CompleteSpeedtestJob.php new file mode 100644 index 000000000..4e8c73d0a --- /dev/null +++ b/app/Jobs/Ookla/CompleteSpeedtestJob.php @@ -0,0 +1,34 @@ +result->update([ + 'status' => ResultStatus::Completed, + ]); + + SpeedtestCompleted::dispatch($this->result); + } +} diff --git a/app/Jobs/Ookla/ProcessSpeedtestBatch.php b/app/Jobs/Ookla/ProcessSpeedtestBatch.php index 0acc254ac..d21ec52bf 100644 --- a/app/Jobs/Ookla/ProcessSpeedtestBatch.php +++ b/app/Jobs/Ookla/ProcessSpeedtestBatch.php @@ -33,6 +33,8 @@ public function handle(): void new CheckForInternetConnectionJob($this->result), new SkipSpeedtestJob($this->result), new RunSpeedtestJob($this->result), + new BenchmarkSpeedtestJob($this->result), + new CompleteSpeedtestJob($this->result), ], ])->catch(function (Batch $batch, ?Throwable $e) { Log::error(sprintf('Speedtest batch "%s" failed for an unknown reason.', $batch->id)); diff --git a/app/Jobs/Ookla/RunSpeedtestJob.php b/app/Jobs/Ookla/RunSpeedtestJob.php index b285fd531..3b6ffbaef 100644 --- a/app/Jobs/Ookla/RunSpeedtestJob.php +++ b/app/Jobs/Ookla/RunSpeedtestJob.php @@ -3,7 +3,6 @@ namespace App\Jobs\Ookla; use App\Enums\ResultStatus; -use App\Events\SpeedtestCompleted; use App\Events\SpeedtestFailed; use App\Events\SpeedtestRunning; use App\Helpers\Ookla; @@ -68,6 +67,8 @@ public function handle(): void 'status' => ResultStatus::Failed, ]); + $this->batch()->cancel(); + SpeedtestFailed::dispatch($this->result); return; @@ -80,9 +81,6 @@ public function handle(): void 'download' => Arr::get($output, 'download.bandwidth'), 'upload' => Arr::get($output, 'upload.bandwidth'), 'data' => $output, - 'status' => ResultStatus::Completed, ]); - - SpeedtestCompleted::dispatch($this->result); } } diff --git a/app/Models/Result.php b/app/Models/Result.php index 4d84d5885..cfb980022 100644 --- a/app/Models/Result.php +++ b/app/Models/Result.php @@ -30,6 +30,7 @@ class Result extends Model protected function casts(): array { return [ + 'benchmarks' => 'array', 'data' => 'array', 'service' => ResultService::class, 'status' => ResultStatus::class, diff --git a/database/migrations/2024_11_22_235011_add_benchmarks_to_results_table.php b/database/migrations/2024_11_22_235011_add_benchmarks_to_results_table.php new file mode 100644 index 000000000..b6bba5458 --- /dev/null +++ b/database/migrations/2024_11_22_235011_add_benchmarks_to_results_table.php @@ -0,0 +1,28 @@ +json('benchmarks') + ->nullable() + ->after('data'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + // + } +};