From 18dbd7f85c0d973145927963df4de6fa0475fac1 Mon Sep 17 00:00:00 2001 From: svenvg93 Date: Mon, 19 Aug 2024 13:25:45 +0200 Subject: [PATCH 01/10] first-commit --- app/Enums/ThresholdBreached.php | 31 +++++++++++++++++++ app/Filament/Resources/ResultResource.php | 8 +++++ app/Jobs/Speedtests/ExecuteOoklaSpeedtest.php | 3 ++ app/Models/Result.php | 30 ++++++++++++++++++ ...dd_threshold_breached_to_results_table.php | 26 ++++++++++++++++ 5 files changed, 98 insertions(+) create mode 100644 app/Enums/ThresholdBreached.php create mode 100644 database/migrations/2024_08_19_115130_add_threshold_breached_to_results_table.php diff --git a/app/Enums/ThresholdBreached.php b/app/Enums/ThresholdBreached.php new file mode 100644 index 000000000..d7ad29ba2 --- /dev/null +++ b/app/Enums/ThresholdBreached.php @@ -0,0 +1,31 @@ + 'danger', + self::Passed => 'success', + self::Unknown => 'warning', + }; + } + + public function getLabel(): ?string + { + return match ($this) { + self::Failed => 'Failed', + self::Passed => 'Passed', + self::Unknown => 'Unknown', + }; + } +} diff --git a/app/Filament/Resources/ResultResource.php b/app/Filament/Resources/ResultResource.php index 005269bf6..f97890af7 100644 --- a/app/Filament/Resources/ResultResource.php +++ b/app/Filament/Resources/ResultResource.php @@ -4,6 +4,7 @@ use App\Actions\MigrateBadJsonResults; use App\Enums\ResultStatus; +use App\Enums\ThresholdBreached; use App\Filament\Exports\ResultExporter; use App\Filament\Resources\ResultResource\Pages; use App\Helpers\Number; @@ -311,6 +312,13 @@ public static function table(Table $table): Table ->badge() ->toggleable() ->sortable(), + Tables\Columns\TextColumn::make('threshold_breached') + ->label('Threshold') + ->badge() + ->color(fn (string $state): string => ThresholdBreached::from($state)->getColor()) + ->toggleable() + ->toggledHiddenByDefault() + ->sortable(), Tables\Columns\IconColumn::make('scheduled') ->boolean() ->toggleable() diff --git a/app/Jobs/Speedtests/ExecuteOoklaSpeedtest.php b/app/Jobs/Speedtests/ExecuteOoklaSpeedtest.php index 575b820c6..03b7d358b 100644 --- a/app/Jobs/Speedtests/ExecuteOoklaSpeedtest.php +++ b/app/Jobs/Speedtests/ExecuteOoklaSpeedtest.php @@ -83,6 +83,9 @@ public function handle(): void 'status' => ResultStatus::Completed, ]); + // Ensure thresholds are checked and updated + $this->result->checkAndUpdateThresholds(); + SpeedtestCompleted::dispatch($this->result); } diff --git a/app/Models/Result.php b/app/Models/Result.php index 4965e49af..0997ed3ff 100644 --- a/app/Models/Result.php +++ b/app/Models/Result.php @@ -3,6 +3,8 @@ namespace App\Models; use App\Enums\ResultStatus; +use App\Helpers\Number; +use App\Settings\ThresholdSettings; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Factories\HasFactory; @@ -14,6 +16,15 @@ class Result extends Model { use HasFactory, Prunable; + protected $fillable = [ + 'status', + 'download', + 'upload', + 'ping', + 'data', + 'threshold_breached', + ]; + /** * The attributes that aren't mass assignable. * @@ -290,4 +301,23 @@ protected function uploadlatencyiqm(): Attribute get: fn () => Arr::get($this->data, 'upload.latency.iqm'), ); } + + public function checkAndUpdateThresholds(): void + { + + $thresholds = app(ThresholdSettings::class); + + // Check if the threshold is breached + $downloadInMbits = ! is_null($this->download) ? Number::bitsToMagnitude($this->download_bits, 2, 'mbit') : null; + $uploadInMbits = ! is_null($this->upload) ? Number::bitsToMagnitude($this->upload_bits, 2, 'mbit') : null; + + $downloadBreached = $downloadInMbits !== null && $downloadInMbits < $thresholds->absolute_download; + $uploadBreached = $uploadInMbits !== null && $uploadInMbits < $thresholds->absolute_upload; + $pingBreached = $this->ping !== null && $this->ping > $thresholds->absolute_ping; + + // Update only the threshold_breached field + $this->update([ + 'threshold_breached' => $downloadBreached || $uploadBreached || $pingBreached ? 'Failed' : 'Passed', + ]); + } } diff --git a/database/migrations/2024_08_19_115130_add_threshold_breached_to_results_table.php b/database/migrations/2024_08_19_115130_add_threshold_breached_to_results_table.php new file mode 100644 index 000000000..b2489639d --- /dev/null +++ b/database/migrations/2024_08_19_115130_add_threshold_breached_to_results_table.php @@ -0,0 +1,26 @@ +enum('threshold_breached', ['Passed', 'Failed', 'Unknown'])->default('Unknown'); + } + }); + } + + public function down(): void + { + Schema::table('results', function (Blueprint $table) { + if (Schema::hasColumn('results', 'threshold_breached')) { + $table->dropColumn('threshold_breached'); + } + }); + } +}; From 999150504221e2f3afe3bf2d3bff99b885aa51ae Mon Sep 17 00:00:00 2001 From: svenvg93 Date: Mon, 19 Aug 2024 16:14:24 +0200 Subject: [PATCH 02/10] add_filter --- app/Filament/Resources/ResultResource.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/Filament/Resources/ResultResource.php b/app/Filament/Resources/ResultResource.php index f97890af7..e7837af63 100644 --- a/app/Filament/Resources/ResultResource.php +++ b/app/Filament/Resources/ResultResource.php @@ -367,6 +367,10 @@ public static function table(Table $table): Table Tables\Filters\SelectFilter::make('status') ->multiple() ->options(ResultStatus::class), + Tables\Filters\SelectFilter::make('threshold_breached') + ->label('Threshold Status') + ->multiple() + ->options(ThresholdBreached::class), ]) ->actions([ Tables\Actions\ActionGroup::make([ From d4c2acd238cf06e7ecc8e0d8b567f4ace9a839ab Mon Sep 17 00:00:00 2001 From: svenvg93 Date: Mon, 19 Aug 2024 20:46:46 +0200 Subject: [PATCH 03/10] unknown_when_disbabled --- app/Models/Result.php | 29 +++++++++++-------- ...dd_threshold_breached_to_results_table.php | 7 +++-- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/app/Models/Result.php b/app/Models/Result.php index 0997ed3ff..80cdbcf1f 100644 --- a/app/Models/Result.php +++ b/app/Models/Result.php @@ -304,20 +304,25 @@ protected function uploadlatencyiqm(): Attribute public function checkAndUpdateThresholds(): void { - $thresholds = app(ThresholdSettings::class); - - // Check if the threshold is breached - $downloadInMbits = ! is_null($this->download) ? Number::bitsToMagnitude($this->download_bits, 2, 'mbit') : null; - $uploadInMbits = ! is_null($this->upload) ? Number::bitsToMagnitude($this->upload_bits, 2, 'mbit') : null; - - $downloadBreached = $downloadInMbits !== null && $downloadInMbits < $thresholds->absolute_download; - $uploadBreached = $uploadInMbits !== null && $uploadInMbits < $thresholds->absolute_upload; - $pingBreached = $this->ping !== null && $this->ping > $thresholds->absolute_ping; - + + // Determine if thresholds are enabled + $thresholdsEnabled = $thresholds->absolute_enabled; + + // Convert bits to Mbits if needed + $downloadInMbits = !is_null($this->download) ? Number::bitsToMagnitude($this->download_bits, 2, 'mbit') : null; + $uploadInMbits = !is_null($this->upload) ? Number::bitsToMagnitude($this->upload_bits, 2, 'mbit') : null; + + // Determine if thresholds are breached or unknown + $downloadBreached = $thresholdsEnabled && $downloadInMbits !== null && $downloadInMbits < $thresholds->absolute_download; + $uploadBreached = $thresholdsEnabled && $uploadInMbits !== null && $uploadInMbits < $thresholds->absolute_upload; + $pingBreached = $thresholdsEnabled && $this->ping !== null && $this->ping > $thresholds->absolute_ping; + // Update only the threshold_breached field $this->update([ - 'threshold_breached' => $downloadBreached || $uploadBreached || $pingBreached ? 'Failed' : 'Passed', + 'threshold_breached' => $thresholdsEnabled + ? ($downloadBreached || $uploadBreached || $pingBreached ? 'Failed' : 'Passed') + : 'Unknown', ]); - } + } } diff --git a/database/migrations/2024_08_19_115130_add_threshold_breached_to_results_table.php b/database/migrations/2024_08_19_115130_add_threshold_breached_to_results_table.php index b2489639d..94ae216a1 100644 --- a/database/migrations/2024_08_19_115130_add_threshold_breached_to_results_table.php +++ b/database/migrations/2024_08_19_115130_add_threshold_breached_to_results_table.php @@ -6,12 +6,13 @@ return new class extends Migration { + /** + * Run the migrations. + */ public function up(): void { Schema::table('results', function (Blueprint $table) { - if (! Schema::hasColumn('results', 'threshold_breached')) { - $table->enum('threshold_breached', ['Passed', 'Failed', 'Unknown'])->default('Unknown'); - } + $table->string('threshold_breached')->default('Unknown'); }); } From 6164632afbcfcc2677fbddcfc0cc6ee126b063e3 Mon Sep 17 00:00:00 2001 From: svenvg93 Date: Mon, 19 Aug 2024 22:38:46 +0200 Subject: [PATCH 04/10] lint --- app/Models/Result.php | 18 +++++++++--------- ...add_threshold_breached_to_results_table.php | 4 ++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/app/Models/Result.php b/app/Models/Result.php index 80cdbcf1f..342327efa 100644 --- a/app/Models/Result.php +++ b/app/Models/Result.php @@ -305,24 +305,24 @@ protected function uploadlatencyiqm(): Attribute public function checkAndUpdateThresholds(): void { $thresholds = app(ThresholdSettings::class); - + // Determine if thresholds are enabled $thresholdsEnabled = $thresholds->absolute_enabled; - + // Convert bits to Mbits if needed - $downloadInMbits = !is_null($this->download) ? Number::bitsToMagnitude($this->download_bits, 2, 'mbit') : null; - $uploadInMbits = !is_null($this->upload) ? Number::bitsToMagnitude($this->upload_bits, 2, 'mbit') : null; - + $downloadInMbits = ! is_null($this->download) ? Number::bitsToMagnitude($this->download_bits, 2, 'mbit') : null; + $uploadInMbits = ! is_null($this->upload) ? Number::bitsToMagnitude($this->upload_bits, 2, 'mbit') : null; + // Determine if thresholds are breached or unknown $downloadBreached = $thresholdsEnabled && $downloadInMbits !== null && $downloadInMbits < $thresholds->absolute_download; $uploadBreached = $thresholdsEnabled && $uploadInMbits !== null && $uploadInMbits < $thresholds->absolute_upload; $pingBreached = $thresholdsEnabled && $this->ping !== null && $this->ping > $thresholds->absolute_ping; - + // Update only the threshold_breached field $this->update([ - 'threshold_breached' => $thresholdsEnabled - ? ($downloadBreached || $uploadBreached || $pingBreached ? 'Failed' : 'Passed') + 'threshold_breached' => $thresholdsEnabled + ? ($downloadBreached || $uploadBreached || $pingBreached ? 'Failed' : 'Passed') : 'Unknown', ]); - } + } } diff --git a/database/migrations/2024_08_19_115130_add_threshold_breached_to_results_table.php b/database/migrations/2024_08_19_115130_add_threshold_breached_to_results_table.php index 94ae216a1..d7d8d01a6 100644 --- a/database/migrations/2024_08_19_115130_add_threshold_breached_to_results_table.php +++ b/database/migrations/2024_08_19_115130_add_threshold_breached_to_results_table.php @@ -6,13 +6,13 @@ return new class extends Migration { - /** + /** * Run the migrations. */ public function up(): void { Schema::table('results', function (Blueprint $table) { - $table->string('threshold_breached')->default('Unknown'); + $table->string('threshold_breached')->default('Unknown'); }); } From 2ea698a5306615b1988c8168833193719d1ca18e Mon Sep 17 00:00:00 2001 From: svenvg93 Date: Tue, 20 Aug 2024 14:07:04 +0200 Subject: [PATCH 05/10] add-export --- app/Enums/ThresholdBreached.php | 6 +++--- app/Filament/Exports/ResultExporter.php | 5 +++++ app/Models/Result.php | 4 ++-- ...08_19_115130_add_threshold_breached_to_results_table.php | 2 +- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/app/Enums/ThresholdBreached.php b/app/Enums/ThresholdBreached.php index d7ad29ba2..b275e67fc 100644 --- a/app/Enums/ThresholdBreached.php +++ b/app/Enums/ThresholdBreached.php @@ -9,14 +9,14 @@ enum ThresholdBreached: string implements HasColor, HasLabel { case Failed = 'Failed'; case Passed = 'Passed'; - case Unknown = 'Unknown'; + case NotChecked = 'NotChecked'; public function getColor(): ?string { return match ($this) { self::Failed => 'danger', self::Passed => 'success', - self::Unknown => 'warning', + self::NotChecked => 'warning', }; } @@ -25,7 +25,7 @@ public function getLabel(): ?string return match ($this) { self::Failed => 'Failed', self::Passed => 'Passed', - self::Unknown => 'Unknown', + self::NotChecked => 'NotChecked', }; } } diff --git a/app/Filament/Exports/ResultExporter.php b/app/Filament/Exports/ResultExporter.php index 05f7ac2ee..901f0a105 100644 --- a/app/Filament/Exports/ResultExporter.php +++ b/app/Filament/Exports/ResultExporter.php @@ -103,6 +103,11 @@ public static function getColumns(): array }), ExportColumn::make('comments') ->enabledByDefault(false), + ExportColumn::make('threshold_breached') + ->enabledByDefault(false) + ->state(function (Result $record): string { + return $record->threshold_breached; + }), // ExportColumn::make('status'), // TODO: enable status when upgrading to PHP v8.3: https://php.watch/versions/8.3/dynamic-class-const-enum-member-syntax-support ExportColumn::make('scheduled') ->state(function (Result $record): string { diff --git a/app/Models/Result.php b/app/Models/Result.php index 342327efa..0a6ad2889 100644 --- a/app/Models/Result.php +++ b/app/Models/Result.php @@ -313,7 +313,7 @@ public function checkAndUpdateThresholds(): void $downloadInMbits = ! is_null($this->download) ? Number::bitsToMagnitude($this->download_bits, 2, 'mbit') : null; $uploadInMbits = ! is_null($this->upload) ? Number::bitsToMagnitude($this->upload_bits, 2, 'mbit') : null; - // Determine if thresholds are breached or unknown + // Determine if thresholds are breached or NotChecked $downloadBreached = $thresholdsEnabled && $downloadInMbits !== null && $downloadInMbits < $thresholds->absolute_download; $uploadBreached = $thresholdsEnabled && $uploadInMbits !== null && $uploadInMbits < $thresholds->absolute_upload; $pingBreached = $thresholdsEnabled && $this->ping !== null && $this->ping > $thresholds->absolute_ping; @@ -322,7 +322,7 @@ public function checkAndUpdateThresholds(): void $this->update([ 'threshold_breached' => $thresholdsEnabled ? ($downloadBreached || $uploadBreached || $pingBreached ? 'Failed' : 'Passed') - : 'Unknown', + : 'NotChecked', ]); } } diff --git a/database/migrations/2024_08_19_115130_add_threshold_breached_to_results_table.php b/database/migrations/2024_08_19_115130_add_threshold_breached_to_results_table.php index d7d8d01a6..b714ac418 100644 --- a/database/migrations/2024_08_19_115130_add_threshold_breached_to_results_table.php +++ b/database/migrations/2024_08_19_115130_add_threshold_breached_to_results_table.php @@ -12,7 +12,7 @@ public function up(): void { Schema::table('results', function (Blueprint $table) { - $table->string('threshold_breached')->default('Unknown'); + $table->string('threshold_breached')->default('NotChecked'); }); } From f99963482655eaac42da2b18407d35c4e85c9faa Mon Sep 17 00:00:00 2001 From: svenvg93 Date: Fri, 27 Sep 2024 10:51:01 +0200 Subject: [PATCH 06/10] Add Download, Upload, Ping --- app/Filament/Exports/ResultExporter.php | 19 +++++++++-- app/Filament/Resources/ResultResource.php | 32 ++++++++++++++++--- app/Models/Result.php | 25 ++++++++++++--- ...dd_threshold_breached_to_results_table.php | 17 +++++++--- 4 files changed, 77 insertions(+), 16 deletions(-) diff --git a/app/Filament/Exports/ResultExporter.php b/app/Filament/Exports/ResultExporter.php index 901f0a105..175dfd19b 100644 --- a/app/Filament/Exports/ResultExporter.php +++ b/app/Filament/Exports/ResultExporter.php @@ -103,10 +103,25 @@ public static function getColumns(): array }), ExportColumn::make('comments') ->enabledByDefault(false), - ExportColumn::make('threshold_breached') + ExportColumn::make('threshold_breached_overall') ->enabledByDefault(false) ->state(function (Result $record): string { - return $record->threshold_breached; + return $record->threshold_breached_overall; + }), + ExportColumn::make('threshold_breached_download') + ->enabledByDefault(false) + ->state(function (Result $record): string { + return $record->threshold_breached_download; + }), + ExportColumn::make('threshold_breached_upload') + ->enabledByDefault(false) + ->state(function (Result $record): string { + return $record->threshold_breached_upload; + }), + ExportColumn::make('threshold_breached_ping') + ->enabledByDefault(false) + ->state(function (Result $record): string { + return $record->threshold_breached_ping; }), // ExportColumn::make('status'), // TODO: enable status when upgrading to PHP v8.3: https://php.watch/versions/8.3/dynamic-class-const-enum-member-syntax-support ExportColumn::make('scheduled') diff --git a/app/Filament/Resources/ResultResource.php b/app/Filament/Resources/ResultResource.php index e7837af63..ea8afb223 100644 --- a/app/Filament/Resources/ResultResource.php +++ b/app/Filament/Resources/ResultResource.php @@ -20,6 +20,7 @@ use Filament\Support\Enums\Alignment; use Filament\Tables; use Filament\Tables\Actions\Action; +use Filament\Tables\Enums\FiltersLayout; use Filament\Tables\Table; use Illuminate\Database\Eloquent\Builder; use Illuminate\Support\Facades\Auth; @@ -312,8 +313,29 @@ public static function table(Table $table): Table ->badge() ->toggleable() ->sortable(), - Tables\Columns\TextColumn::make('threshold_breached') - ->label('Threshold') + Tables\Columns\TextColumn::make('threshold_breached_overall') + ->label('Overall Threshold') + ->badge() + ->color(fn (string $state): string => ThresholdBreached::from($state)->getColor()) + ->toggleable() + ->toggledHiddenByDefault() + ->sortable(), + Tables\Columns\TextColumn::make('threshold_breached_download') + ->label('Download Threshold') + ->badge() + ->color(fn (string $state): string => ThresholdBreached::from($state)->getColor()) + ->toggleable() + ->toggledHiddenByDefault() + ->sortable(), + Tables\Columns\TextColumn::make('threshold_breached_upload') + ->label('Upload Threshold') + ->badge() + ->color(fn (string $state): string => ThresholdBreached::from($state)->getColor()) + ->toggleable() + ->toggledHiddenByDefault() + ->sortable(), + Tables\Columns\TextColumn::make('threshold_breached_ping') + ->label('Ping Threshold') ->badge() ->color(fn (string $state): string => ThresholdBreached::from($state)->getColor()) ->toggleable() @@ -367,11 +389,11 @@ public static function table(Table $table): Table Tables\Filters\SelectFilter::make('status') ->multiple() ->options(ResultStatus::class), - Tables\Filters\SelectFilter::make('threshold_breached') - ->label('Threshold Status') + Tables\Filters\SelectFilter::make('threshold_breached_overall') + ->label('Overall Threshold Status') ->multiple() ->options(ThresholdBreached::class), - ]) + ], layout: FiltersLayout::Modal) ->actions([ Tables\Actions\ActionGroup::make([ Action::make('view result') diff --git a/app/Models/Result.php b/app/Models/Result.php index 0a6ad2889..2e5018dd3 100644 --- a/app/Models/Result.php +++ b/app/Models/Result.php @@ -22,7 +22,11 @@ class Result extends Model 'upload', 'ping', 'data', - 'threshold_breached', + 'threshold_breached_overall', + 'threshold_breached_overall', + 'threshold_breached_download', + 'threshold_breached_upload', + 'threshold_breached_ping', ]; /** @@ -318,11 +322,22 @@ public function checkAndUpdateThresholds(): void $uploadBreached = $thresholdsEnabled && $uploadInMbits !== null && $uploadInMbits < $thresholds->absolute_upload; $pingBreached = $thresholdsEnabled && $this->ping !== null && $this->ping > $thresholds->absolute_ping; - // Update only the threshold_breached field + // Calculate individual statuses + $downloadStatus = $thresholdsEnabled ? ($downloadBreached ? 'Failed' : 'Passed') : 'NotChecked'; + $uploadStatus = $thresholdsEnabled ? ($uploadBreached ? 'Failed' : 'Passed') : 'NotChecked'; + $pingStatus = $thresholdsEnabled ? ($pingBreached ? 'Failed' : 'Passed') : 'NotChecked'; + + // Calculate the overall status + $overallStatus = $thresholdsEnabled + ? ($downloadBreached || $uploadBreached || $pingBreached ? 'Failed' : 'Passed') + : 'NotChecked'; + + // Update all relevant fields in the database $this->update([ - 'threshold_breached' => $thresholdsEnabled - ? ($downloadBreached || $uploadBreached || $pingBreached ? 'Failed' : 'Passed') - : 'NotChecked', + 'threshold_breached_overall' => $overallStatus, + 'threshold_breached_download' => $downloadStatus, + 'threshold_breached_upload' => $uploadStatus, + 'threshold_breached_ping' => $pingStatus, ]); } } diff --git a/database/migrations/2024_08_19_115130_add_threshold_breached_to_results_table.php b/database/migrations/2024_08_19_115130_add_threshold_breached_to_results_table.php index b714ac418..ed6690a9b 100644 --- a/database/migrations/2024_08_19_115130_add_threshold_breached_to_results_table.php +++ b/database/migrations/2024_08_19_115130_add_threshold_breached_to_results_table.php @@ -12,16 +12,25 @@ public function up(): void { Schema::table('results', function (Blueprint $table) { - $table->string('threshold_breached')->default('NotChecked'); + $table->string('threshold_breached_overall')->default('NotChecked'); + $table->string('threshold_breached_download')->default('NotChecked'); + $table->string('threshold_breached_upload')->default('NotChecked'); + $table->string('threshold_breached_ping')->default('NotChecked'); }); } + /** + * Reverse the migrations. + */ public function down(): void { Schema::table('results', function (Blueprint $table) { - if (Schema::hasColumn('results', 'threshold_breached')) { - $table->dropColumn('threshold_breached'); - } + $table->dropColumn([ + 'threshold_breached_overall', + 'threshold_breached_download', + 'threshold_breached_upload', + 'threshold_breached_ping', + ]); }); } }; From 3ba7f50f9a88e46376f7c6829f52720842f1f444 Mon Sep 17 00:00:00 2001 From: svenvg93 Date: Sat, 5 Oct 2024 09:46:58 +0200 Subject: [PATCH 07/10] Remove duplicate --- app/Models/Result.php | 1 - 1 file changed, 1 deletion(-) diff --git a/app/Models/Result.php b/app/Models/Result.php index 2e5018dd3..5511a5bb1 100644 --- a/app/Models/Result.php +++ b/app/Models/Result.php @@ -23,7 +23,6 @@ class Result extends Model 'ping', 'data', 'threshold_breached_overall', - 'threshold_breached_overall', 'threshold_breached_download', 'threshold_breached_upload', 'threshold_breached_ping', From 13e45c1cd8a00aab2649dc0dd01acd4adeef80a2 Mon Sep 17 00:00:00 2001 From: Sven van Ginkel Date: Thu, 21 Nov 2024 22:16:04 +0100 Subject: [PATCH 08/10] Refactor --- app/Jobs/CheckAndUpdateThresholds.php | 87 +++++++++++++++++++ app/Jobs/Speedtests/ExecuteOoklaSpeedtest.php | 3 +- app/Models/Result.php | 37 -------- 3 files changed, 89 insertions(+), 38 deletions(-) create mode 100644 app/Jobs/CheckAndUpdateThresholds.php diff --git a/app/Jobs/CheckAndUpdateThresholds.php b/app/Jobs/CheckAndUpdateThresholds.php new file mode 100644 index 000000000..e5c7d1d2c --- /dev/null +++ b/app/Jobs/CheckAndUpdateThresholds.php @@ -0,0 +1,87 @@ +result = $result; + } + + /** + * Execute the job. + */ + public function handle(): void + { + $thresholds = app(ThresholdSettings::class); + + // Determine if thresholds are enabled + $thresholdsEnabled = $thresholds->absolute_enabled; + + // Convert bits to Mbits if needed + $downloadInMbits = ! is_null($this->result->download) + ? Number::bitsToMagnitude($this->result->download_bits, 2, 'mbit') + : null; + $uploadInMbits = ! is_null($this->result->upload) + ? Number::bitsToMagnitude($this->result->upload_bits, 2, 'mbit') + : null; + + // Only check thresholds if they are enabled and not set to 0 + $downloadBreached = $thresholdsEnabled + && $thresholds->absolute_download > 0 + && $downloadInMbits !== null + && $downloadInMbits < $thresholds->absolute_download; + + $uploadBreached = $thresholdsEnabled + && $thresholds->absolute_upload > 0 + && $uploadInMbits !== null + && $uploadInMbits < $thresholds->absolute_upload; + + $pingBreached = $thresholdsEnabled + && $thresholds->absolute_ping > 0 + && $this->result->ping !== null + && $this->result->ping > $thresholds->absolute_ping; + + // Calculate individual statuses + $downloadStatus = $thresholdsEnabled && $thresholds->absolute_download > 0 + ? ($downloadBreached ? 'Failed' : 'Passed') + : 'NotChecked'; + + $uploadStatus = $thresholdsEnabled && $thresholds->absolute_upload > 0 + ? ($uploadBreached ? 'Failed' : 'Passed') + : 'NotChecked'; + + $pingStatus = $thresholdsEnabled && $thresholds->absolute_ping > 0 + ? ($pingBreached ? 'Failed' : 'Passed') + : 'NotChecked'; + + // Calculate the overall status + $overallStatus = $thresholdsEnabled + ? ($downloadBreached || $uploadBreached || $pingBreached ? 'Failed' : 'Passed') + : 'NotChecked'; + + // Update all relevant fields in the database + $this->result->update([ + 'threshold_breached_overall' => $overallStatus, + 'threshold_breached_download' => $downloadStatus, + 'threshold_breached_upload' => $uploadStatus, + 'threshold_breached_ping' => $pingStatus, + ]); + } +} diff --git a/app/Jobs/Speedtests/ExecuteOoklaSpeedtest.php b/app/Jobs/Speedtests/ExecuteOoklaSpeedtest.php index abc6fe0de..96f737f8f 100644 --- a/app/Jobs/Speedtests/ExecuteOoklaSpeedtest.php +++ b/app/Jobs/Speedtests/ExecuteOoklaSpeedtest.php @@ -5,6 +5,7 @@ use App\Enums\ResultStatus; use App\Events\SpeedtestCompleted; use App\Events\SpeedtestFailed; +use App\Jobs\CheckAndUpdateThresholds; use App\Models\Result; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldBeUnique; @@ -108,7 +109,7 @@ public function handle(): void ]); // Ensure thresholds are checked and updated - $this->result->checkAndUpdateThresholds(); + CheckAndUpdateThresholds::dispatch($this->result); SpeedtestCompleted::dispatch($this->result); } diff --git a/app/Models/Result.php b/app/Models/Result.php index 5511a5bb1..27e06e278 100644 --- a/app/Models/Result.php +++ b/app/Models/Result.php @@ -3,8 +3,6 @@ namespace App\Models; use App\Enums\ResultStatus; -use App\Helpers\Number; -use App\Settings\ThresholdSettings; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Factories\HasFactory; @@ -304,39 +302,4 @@ protected function uploadlatencyiqm(): Attribute get: fn () => Arr::get($this->data, 'upload.latency.iqm'), ); } - - public function checkAndUpdateThresholds(): void - { - $thresholds = app(ThresholdSettings::class); - - // Determine if thresholds are enabled - $thresholdsEnabled = $thresholds->absolute_enabled; - - // Convert bits to Mbits if needed - $downloadInMbits = ! is_null($this->download) ? Number::bitsToMagnitude($this->download_bits, 2, 'mbit') : null; - $uploadInMbits = ! is_null($this->upload) ? Number::bitsToMagnitude($this->upload_bits, 2, 'mbit') : null; - - // Determine if thresholds are breached or NotChecked - $downloadBreached = $thresholdsEnabled && $downloadInMbits !== null && $downloadInMbits < $thresholds->absolute_download; - $uploadBreached = $thresholdsEnabled && $uploadInMbits !== null && $uploadInMbits < $thresholds->absolute_upload; - $pingBreached = $thresholdsEnabled && $this->ping !== null && $this->ping > $thresholds->absolute_ping; - - // Calculate individual statuses - $downloadStatus = $thresholdsEnabled ? ($downloadBreached ? 'Failed' : 'Passed') : 'NotChecked'; - $uploadStatus = $thresholdsEnabled ? ($uploadBreached ? 'Failed' : 'Passed') : 'NotChecked'; - $pingStatus = $thresholdsEnabled ? ($pingBreached ? 'Failed' : 'Passed') : 'NotChecked'; - - // Calculate the overall status - $overallStatus = $thresholdsEnabled - ? ($downloadBreached || $uploadBreached || $pingBreached ? 'Failed' : 'Passed') - : 'NotChecked'; - - // Update all relevant fields in the database - $this->update([ - 'threshold_breached_overall' => $overallStatus, - 'threshold_breached_download' => $downloadStatus, - 'threshold_breached_upload' => $uploadStatus, - 'threshold_breached_ping' => $pingStatus, - ]); - } } From 14a0fcf200c54208fd3990ae1fdbdda4bf33f9d8 Mon Sep 17 00:00:00 2001 From: Sven van Ginkel Date: Thu, 21 Nov 2024 22:22:21 +0100 Subject: [PATCH 09/10] Remove fillable --- app/Models/Result.php | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/app/Models/Result.php b/app/Models/Result.php index 27e06e278..4965e49af 100644 --- a/app/Models/Result.php +++ b/app/Models/Result.php @@ -14,18 +14,6 @@ class Result extends Model { use HasFactory, Prunable; - protected $fillable = [ - 'status', - 'download', - 'upload', - 'ping', - 'data', - 'threshold_breached_overall', - 'threshold_breached_download', - 'threshold_breached_upload', - 'threshold_breached_ping', - ]; - /** * The attributes that aren't mass assignable. * From e564381313df6c0c12dce0f095a2d55bb2b7d5b1 Mon Sep 17 00:00:00 2001 From: Sven van Ginkel Date: Thu, 21 Nov 2024 22:30:54 +0100 Subject: [PATCH 10/10] lint --- app/Jobs/Speedtests/ExecuteOoklaSpeedtest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Jobs/Speedtests/ExecuteOoklaSpeedtest.php b/app/Jobs/Speedtests/ExecuteOoklaSpeedtest.php index bac300fdd..511823b6c 100644 --- a/app/Jobs/Speedtests/ExecuteOoklaSpeedtest.php +++ b/app/Jobs/Speedtests/ExecuteOoklaSpeedtest.php @@ -6,9 +6,9 @@ use App\Enums\ResultStatus; use App\Events\SpeedtestCompleted; use App\Events\SpeedtestFailed; -use App\Jobs\CheckAndUpdateThresholds; use App\Events\SpeedtestSkipped; use App\Helpers\Network; +use App\Jobs\CheckAndUpdateThresholds; use App\Models\Result; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldBeUnique;