Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions app/Enums/ThresholdBreached.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Enums;

use Filament\Support\Contracts\HasColor;
use Filament\Support\Contracts\HasLabel;

enum ThresholdBreached: string implements HasColor, HasLabel
{
case Failed = 'Failed';
case Passed = 'Passed';
case NotChecked = 'NotChecked';

public function getColor(): ?string
{
return match ($this) {
self::Failed => 'danger',
self::Passed => 'success',
self::NotChecked => 'warning',
};
}

public function getLabel(): ?string
{
return match ($this) {
self::Failed => 'Failed',
self::Passed => 'Passed',
self::NotChecked => 'NotChecked',
};
}
}
5 changes: 5 additions & 0 deletions app/Filament/Exports/ResultExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions app/Filament/Resources/ResultResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -359,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([
Expand Down
3 changes: 3 additions & 0 deletions app/Jobs/Speedtests/ExecuteOoklaSpeedtest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ public function handle(): void
'status' => ResultStatus::Completed,
]);

// Ensure thresholds are checked and updated
$this->result->checkAndUpdateThresholds();

SpeedtestCompleted::dispatch($this->result);
}

Expand Down
35 changes: 35 additions & 0 deletions app/Models/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
*
Expand Down Expand Up @@ -290,4 +301,28 @@ 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;

// Update only the threshold_breached field
$this->update([
'threshold_breached' => $thresholdsEnabled
? ($downloadBreached || $uploadBreached || $pingBreached ? 'Failed' : 'Passed')
: 'NotChecked',
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('results', function (Blueprint $table) {
$table->string('threshold_breached')->default('NotChecked');
});
}

public function down(): void
{
Schema::table('results', function (Blueprint $table) {
if (Schema::hasColumn('results', 'threshold_breached')) {
$table->dropColumn('threshold_breached');
}
});
}
};