Skip to content

Commit b39a692

Browse files
authored
[Feature] Added benchmarks to results table (alexjustesen#1813)
added benchmarks to results table
1 parent 4f3b644 commit b39a692

File tree

8 files changed

+179
-4
lines changed

8 files changed

+179
-4
lines changed

app/Enums/ResultStatus.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
enum ResultStatus: string implements HasColor, HasLabel
1010
{
11+
case Benchmarking = 'benchmarking';
1112
case Checking = 'checking';
1213
case Completed = 'completed';
1314
case Failed = 'failed';
@@ -18,6 +19,7 @@ enum ResultStatus: string implements HasColor, HasLabel
1819
public function getColor(): ?string
1920
{
2021
return match ($this) {
22+
self::Benchmarking => 'info',
2123
self::Checking => 'info',
2224
self::Completed => 'success',
2325
self::Failed => 'danger',
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace App\Events;
4+
5+
use App\Models\Result;
6+
use Illuminate\Broadcasting\InteractsWithSockets;
7+
use Illuminate\Foundation\Events\Dispatchable;
8+
use Illuminate\Queue\SerializesModels;
9+
10+
class SpeedtestBenchmarking
11+
{
12+
use Dispatchable, InteractsWithSockets, SerializesModels;
13+
14+
/**
15+
* Create a new event instance.
16+
*/
17+
public function __construct(
18+
public Result $result,
19+
) {}
20+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
namespace App\Jobs\Ookla;
4+
5+
use App\Enums\ResultStatus;
6+
use App\Events\SpeedtestBenchmarking;
7+
use App\Models\Result;
8+
use App\Settings\ThresholdSettings;
9+
use Illuminate\Bus\Batchable;
10+
use Illuminate\Contracts\Queue\ShouldQueue;
11+
use Illuminate\Foundation\Queue\Queueable;
12+
use Illuminate\Support\Arr;
13+
14+
class BenchmarkSpeedtestJob implements ShouldQueue
15+
{
16+
use Batchable, Queueable;
17+
18+
/**
19+
* Create a new job instance.
20+
*/
21+
public function __construct(
22+
public Result $result,
23+
) {}
24+
25+
/**
26+
* Execute the job.
27+
*/
28+
public function handle(): void
29+
{
30+
if ($this->batch()->cancelled()) {
31+
return;
32+
}
33+
34+
$settings = app(ThresholdSettings::class);
35+
36+
if ($settings->absolute_enabled === false) {
37+
return;
38+
}
39+
40+
$this->result->update([
41+
'status' => ResultStatus::Benchmarking,
42+
]);
43+
44+
SpeedtestBenchmarking::dispatch($this->result);
45+
46+
$benchmarks = $this->buildBenchmarks($settings);
47+
48+
if (count($benchmarks) > 0) {
49+
$this->result->update([
50+
'benchmarks' => $benchmarks,
51+
]);
52+
} else {
53+
return;
54+
}
55+
}
56+
57+
private function buildBenchmarks(ThresholdSettings $settings): array
58+
{
59+
$benchmarks = [];
60+
61+
if (! blank($settings->absolute_download) && $settings->absolute_download > 0) {
62+
$benchmarks = Arr::add($benchmarks, 'download', [
63+
'bar' => 'min',
64+
'type' => 'absolute',
65+
'value' => $settings->absolute_download,
66+
'unit' => 'mbps',
67+
]);
68+
}
69+
70+
if (! blank($settings->absolute_upload) && $settings->absolute_upload > 0) {
71+
$benchmarks = Arr::add($benchmarks, 'upload', [
72+
'bar' => 'min',
73+
'type' => 'absolute',
74+
'value' => $settings->absolute_upload,
75+
'unit' => 'mbps',
76+
]);
77+
}
78+
79+
if (! blank($settings->absolute_ping) && $settings->absolute_ping > 0) {
80+
$benchmarks = Arr::add($benchmarks, 'ping', [
81+
'bar' => 'max',
82+
'type' => 'absolute',
83+
'value' => $settings->absolute_ping,
84+
'unit' => 'ms',
85+
]);
86+
}
87+
88+
return $benchmarks;
89+
}
90+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace App\Jobs\Ookla;
4+
5+
use App\Enums\ResultStatus;
6+
use App\Events\SpeedtestCompleted;
7+
use App\Models\Result;
8+
use Illuminate\Bus\Batchable;
9+
use Illuminate\Contracts\Queue\ShouldQueue;
10+
use Illuminate\Foundation\Queue\Queueable;
11+
12+
class CompleteSpeedtestJob implements ShouldQueue
13+
{
14+
use Batchable, Queueable;
15+
16+
/**
17+
* Create a new job instance.
18+
*/
19+
public function __construct(
20+
public Result $result,
21+
) {}
22+
23+
/**
24+
* Execute the job.
25+
*/
26+
public function handle(): void
27+
{
28+
$this->result->update([
29+
'status' => ResultStatus::Completed,
30+
]);
31+
32+
SpeedtestCompleted::dispatch($this->result);
33+
}
34+
}

app/Jobs/Ookla/ProcessSpeedtestBatch.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public function handle(): void
3333
new CheckForInternetConnectionJob($this->result),
3434
new SkipSpeedtestJob($this->result),
3535
new RunSpeedtestJob($this->result),
36+
new BenchmarkSpeedtestJob($this->result),
37+
new CompleteSpeedtestJob($this->result),
3638
],
3739
])->catch(function (Batch $batch, ?Throwable $e) {
3840
Log::error(sprintf('Speedtest batch "%s" failed for an unknown reason.', $batch->id));

app/Jobs/Ookla/RunSpeedtestJob.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace App\Jobs\Ookla;
44

55
use App\Enums\ResultStatus;
6-
use App\Events\SpeedtestCompleted;
76
use App\Events\SpeedtestFailed;
87
use App\Events\SpeedtestRunning;
98
use App\Helpers\Ookla;
@@ -68,6 +67,8 @@ public function handle(): void
6867
'status' => ResultStatus::Failed,
6968
]);
7069

70+
$this->batch()->cancel();
71+
7172
SpeedtestFailed::dispatch($this->result);
7273

7374
return;
@@ -80,9 +81,6 @@ public function handle(): void
8081
'download' => Arr::get($output, 'download.bandwidth'),
8182
'upload' => Arr::get($output, 'upload.bandwidth'),
8283
'data' => $output,
83-
'status' => ResultStatus::Completed,
8484
]);
85-
86-
SpeedtestCompleted::dispatch($this->result);
8785
}
8886
}

app/Models/Result.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class Result extends Model
3030
protected function casts(): array
3131
{
3232
return [
33+
'benchmarks' => 'array',
3334
'data' => 'array',
3435
'service' => ResultService::class,
3536
'status' => ResultStatus::class,
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::table('results', function (Blueprint $table) {
15+
$table->json('benchmarks')
16+
->nullable()
17+
->after('data');
18+
});
19+
}
20+
21+
/**
22+
* Reverse the migrations.
23+
*/
24+
public function down(): void
25+
{
26+
//
27+
}
28+
};

0 commit comments

Comments
 (0)