Skip to content

Commit

Permalink
added benchmarks to results table
Browse files Browse the repository at this point in the history
  • Loading branch information
alexjustesen committed Nov 23, 2024
1 parent 4f3b644 commit fa853ea
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 4 deletions.
2 changes: 2 additions & 0 deletions app/Enums/ResultStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

enum ResultStatus: string implements HasColor, HasLabel
{
case Benchmarking = 'benchmarking';
case Checking = 'checking';
case Completed = 'completed';
case Failed = 'failed';
Expand All @@ -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',
Expand Down
20 changes: 20 additions & 0 deletions app/Events/SpeedtestBenchmarking.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Events;

use App\Models\Result;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class SpeedtestBenchmarking
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* Create a new event instance.
*/
public function __construct(
public Result $result,
) {}
}
90 changes: 90 additions & 0 deletions app/Jobs/Ookla/BenchmarkSpeedtestJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace App\Jobs\Ookla;

use App\Enums\ResultStatus;
use App\Events\SpeedtestBenchmarking;
use App\Models\Result;
use App\Settings\ThresholdSettings;
use Illuminate\Bus\Batchable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Arr;

class BenchmarkSpeedtestJob implements ShouldQueue
{
use Batchable, Queueable;

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

/**
* Execute the job.
*/
public function handle(): void
{
if ($this->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;
}
}
34 changes: 34 additions & 0 deletions app/Jobs/Ookla/CompleteSpeedtestJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\Jobs\Ookla;

use App\Enums\ResultStatus;
use App\Events\SpeedtestCompleted;
use App\Models\Result;
use Illuminate\Bus\Batchable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;

class CompleteSpeedtestJob implements ShouldQueue
{
use Batchable, Queueable;

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

/**
* Execute the job.
*/
public function handle(): void
{
$this->result->update([
'status' => ResultStatus::Completed,
]);

SpeedtestCompleted::dispatch($this->result);
}
}
2 changes: 2 additions & 0 deletions app/Jobs/Ookla/ProcessSpeedtestBatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
6 changes: 2 additions & 4 deletions app/Jobs/Ookla/RunSpeedtestJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -68,6 +67,8 @@ public function handle(): void
'status' => ResultStatus::Failed,
]);

$this->batch()->cancel();

SpeedtestFailed::dispatch($this->result);

return;
Expand All @@ -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);
}
}
1 change: 1 addition & 0 deletions app/Models/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Result extends Model
protected function casts(): array
{
return [
'benchmarks' => 'array',
'data' => 'array',
'service' => ResultService::class,
'status' => ResultStatus::class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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->json('benchmarks')
->nullable()
->after('data');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
//
}
};

0 comments on commit fa853ea

Please sign in to comment.