Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Added Check benchmark for healthy #1816

12 changes: 11 additions & 1 deletion app/Filament/Resources/ResultResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ public static function form(Form $form): Form
Forms\Components\Placeholder::make('server_host')
->content(fn (Result $result): ?string => $result->server_host),
Forms\Components\Checkbox::make('scheduled'),
Forms\Components\Checkbox::make('healthy'),
])
->columns(1)
->columnSpan([
Expand Down Expand Up @@ -315,6 +316,11 @@ public static function table(Table $table): Table
->toggleable()
->toggledHiddenByDefault()
->alignment(Alignment::Center),
Tables\Columns\IconColumn::make('healthy')
->boolean()
->toggleable()
->toggledHiddenByDefault()
->alignment(Alignment::Center),
Tables\Columns\TextColumn::make('created_at')
->dateTime(config('app.datetime_format'))
->timezone(config('app.display_timezone'))
Expand Down Expand Up @@ -347,7 +353,7 @@ public static function table(Table $table): Table
})
->attribute('data->interface->externalIp'),
Tables\Filters\TernaryFilter::make('scheduled')
->placeholder('-')
->nullable()
->trueLabel('Only scheduled speedtests')
->falseLabel('Only manual speedtests')
->queries(
Expand All @@ -358,6 +364,10 @@ public static function table(Table $table): Table
Tables\Filters\SelectFilter::make('status')
->multiple()
->options(ResultStatus::class),
Tables\Filters\TernaryFilter::make('healthy')
->nullable()
->trueLabel('Only healthy speedtests')
->falseLabel('Only unhealthy speedtests'),
])
->actions([
Tables\Actions\ActionGroup::make([
Expand Down
69 changes: 69 additions & 0 deletions app/Jobs/CheckAndUpdateBenchmarkResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace App\Jobs;

use App\Helpers\Benchmark;
use App\Models\Result;
use Illuminate\Bus\Batchable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Arr;

class CheckAndUpdateBenchmarkResult implements ShouldQueue
{
use Batchable, Queueable;

/**
* Create a new job instance.
*/
public function __construct(
public Result $result,
) {}

/**
* Execute the job.
*/
public function handle(): void
svenvg93 marked this conversation as resolved.
Show resolved Hide resolved
{
// If the batch is cancelled, do nothing
if ($this->batch()->cancelled()) {
return;
}

// Retrieve the benchmarks from the result
$benchmarks = $this->result->benchmarks;

// Process the benchmarks (assuming you have download, upload, ping types)
$types = ['download', 'upload', 'ping'];

foreach ($types as $type) {
$value = $this->result->{$type};

// Retrieve the benchmark settings for the given type
$benchmark = Arr::get($benchmarks, $type);

// Only check the benchmark if the value and benchmark are valid
if ($benchmark && $value !== null) {
if ($type === 'ping') {
// Use the ping method for the ping benchmark
$passed = Benchmark::ping($value, $benchmark);
} else {
// Use the bitrate method for download/upload benchmarks
$passed = Benchmark::bitrate($value, $benchmark);
}

// Invert the result logic here:
$passedStatus = ! $passed ? 'true' : 'false';

// If the result has changed, update the passed status
if (Arr::get($benchmarks, "$type.passed") !== $passedStatus) {
$benchmarks[$type]['passed'] = $passedStatus;
}
}
}

// After processing, update the result with the modified benchmarks
$this->result->benchmarks = $benchmarks;
$this->result->save();
}
}
2 changes: 2 additions & 0 deletions app/Jobs/Ookla/ProcessSpeedtestBatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Jobs\Ookla;

use App\Jobs\CheckAndUpdateBenchmarkResult;
use App\Jobs\CheckForInternetConnectionJob;
use App\Jobs\SkipSpeedtestJob;
use App\Models\Result;
Expand Down Expand Up @@ -35,6 +36,7 @@ public function handle(): void
new SelectSpeedtestServerJob($this->result),
new RunSpeedtestJob($this->result),
new BenchmarkSpeedtestJob($this->result),
new CheckAndUpdateBenchmarkResult($this->result),
new CompleteSpeedtestJob($this->result),
],
])->catch(function (Batch $batch, ?Throwable $e) {
Expand Down