Skip to content

Commit 96ad94f

Browse files
[Chore] Refactored benchmarking results (alexjustesen#1835)
* [Feature] Added Check benchmark for healthy (alexjustesen#1816) * Add Check and update benchmarsk state process * Add helper and result resource * Add filter * Fix result table * replace placeholder * Use Helpers\Benchmark --------- Co-authored-by: Alex Justesen <[email protected]> * moved skipspeedtestjob to ookla namespace * consolidated benchmark jobs * fixed typo * benchmark helper should be truthy --------- Co-authored-by: Sven van Ginkel <[email protected]>
1 parent ce3f4b4 commit 96ad94f

File tree

6 files changed

+39
-49
lines changed

6 files changed

+39
-49
lines changed

app/Actions/Ookla/EvaluateResultHealth.php

Lines changed: 0 additions & 35 deletions
This file was deleted.

app/Filament/Resources/ResultResource.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ public static function form(Form $form): Form
140140
Forms\Components\Placeholder::make('server_host')
141141
->content(fn (Result $result): ?string => $result->server_host),
142142
Forms\Components\Checkbox::make('scheduled'),
143+
Forms\Components\Checkbox::make('healthy'),
143144
])
144145
->columns(1)
145146
->columnSpan([
@@ -315,6 +316,11 @@ public static function table(Table $table): Table
315316
->toggleable()
316317
->toggledHiddenByDefault()
317318
->alignment(Alignment::Center),
319+
Tables\Columns\IconColumn::make('healthy')
320+
->boolean()
321+
->toggleable()
322+
->toggledHiddenByDefault()
323+
->alignment(Alignment::Center),
318324
Tables\Columns\TextColumn::make('created_at')
319325
->dateTime(config('app.datetime_format'))
320326
->timezone(config('app.display_timezone'))
@@ -347,7 +353,7 @@ public static function table(Table $table): Table
347353
})
348354
->attribute('data->interface->externalIp'),
349355
Tables\Filters\TernaryFilter::make('scheduled')
350-
->placeholder('-')
356+
->nullable()
351357
->trueLabel('Only scheduled speedtests')
352358
->falseLabel('Only manual speedtests')
353359
->queries(
@@ -358,6 +364,10 @@ public static function table(Table $table): Table
358364
Tables\Filters\SelectFilter::make('status')
359365
->multiple()
360366
->options(ResultStatus::class),
367+
Tables\Filters\TernaryFilter::make('healthy')
368+
->nullable()
369+
->trueLabel('Only healthy speedtests')
370+
->falseLabel('Only unhealthy speedtests'),
361371
])
362372
->actions([
363373
Tables\Actions\ActionGroup::make([

app/Helpers/Benchmark.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static function bitrate(float|int $bytes, array $benchmark): bool
2020
return true;
2121
}
2222

23-
return Bitrate::bytesToBits($bytes) < Bitrate::normalizeToBits($value.$unit);
23+
return Bitrate::bytesToBits($bytes) >= Bitrate::normalizeToBits($value.$unit);
2424
}
2525

2626
/**
@@ -35,6 +35,6 @@ public static function ping(float|int $ping, array $benchmark): bool
3535
return true;
3636
}
3737

38-
return $ping >= $value;
38+
return $ping < $value;
3939
}
4040
}

app/Jobs/Ookla/BenchmarkSpeedtestJob.php

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace App\Jobs\Ookla;
44

5-
use App\Actions\Ookla\EvaluateResultHealth;
65
use App\Enums\ResultStatus;
76
use App\Events\SpeedtestBenchmarking;
7+
use App\Helpers\Benchmark;
88
use App\Models\Result;
99
use App\Settings\ThresholdSettings;
1010
use Illuminate\Bus\Batchable;
@@ -16,6 +16,8 @@ class BenchmarkSpeedtestJob implements ShouldQueue
1616
{
1717
use Batchable, Queueable;
1818

19+
public bool $healthy = true;
20+
1921
/**
2022
* Create a new job instance.
2123
*/
@@ -28,13 +30,9 @@ public function __construct(
2830
*/
2931
public function handle(): void
3032
{
31-
if ($this->batch()->cancelled()) {
32-
return;
33-
}
34-
3533
$settings = app(ThresholdSettings::class);
3634

37-
if ($settings->absolute_enabled === false) {
35+
if ($this->batch()->cancelled() || $settings->absolute_enabled == false) {
3836
return;
3937
}
4038

@@ -44,47 +42,65 @@ public function handle(): void
4442

4543
SpeedtestBenchmarking::dispatch($this->result);
4644

47-
$benchmarks = $this->buildBenchmarks($settings);
45+
$benchmarks = $this->benchmark(
46+
result: $this->result,
47+
settings: $settings,
48+
);
4849

4950
if (! count($benchmarks)) {
5051
return;
5152
}
5253

5354
$this->result->update([
5455
'benchmarks' => $benchmarks,
55-
'healthy' => EvaluateResultHealth::run($this->result, $benchmarks),
56+
'healthy' => $this->healthy,
5657
]);
5758
}
5859

59-
private function buildBenchmarks(ThresholdSettings $settings): array
60+
private function benchmark(Result $result, ThresholdSettings $settings): array
6061
{
6162
$benchmarks = [];
6263

6364
if (! blank($settings->absolute_download) && $settings->absolute_download > 0) {
6465
$benchmarks = Arr::add($benchmarks, 'download', [
6566
'bar' => 'min',
67+
'passed' => Benchmark::bitrate($result->download, ['value' => $settings->absolute_download, 'unit' => 'mbps']),
6668
'type' => 'absolute',
6769
'value' => $settings->absolute_download,
6870
'unit' => 'mbps',
6971
]);
72+
73+
if (Arr::get($benchmarks, 'download.passed') == false) {
74+
$this->healthy = false;
75+
}
7076
}
7177

7278
if (! blank($settings->absolute_upload) && $settings->absolute_upload > 0) {
7379
$benchmarks = Arr::add($benchmarks, 'upload', [
7480
'bar' => 'min',
81+
'passed' => filter_var(Benchmark::bitrate($result->upload, ['value' => $settings->absolute_upload, 'unit' => 'mbps']), FILTER_VALIDATE_BOOLEAN),
7582
'type' => 'absolute',
7683
'value' => $settings->absolute_upload,
7784
'unit' => 'mbps',
7885
]);
86+
87+
if (Arr::get($benchmarks, 'upload.passed') == false) {
88+
$this->healthy = false;
89+
}
7990
}
8091

8192
if (! blank($settings->absolute_ping) && $settings->absolute_ping > 0) {
8293
$benchmarks = Arr::add($benchmarks, 'ping', [
8394
'bar' => 'max',
95+
'passed' => Benchmark::ping($result->ping, ['value' => $settings->absolute_ping]),
8496
'type' => 'absolute',
8597
'value' => $settings->absolute_ping,
8698
'unit' => 'ms',
8799
]);
100+
101+
if (Arr::get($benchmarks, 'ping.passed') == false) {
102+
$this->healthy = false;
103+
}
88104
}
89105

90106
return $benchmarks;

app/Jobs/Ookla/ProcessSpeedtestBatch.php

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

55
use App\Jobs\CheckForInternetConnectionJob;
6-
use App\Jobs\SkipSpeedtestJob;
76
use App\Models\Result;
87
use Illuminate\Bus\Batch;
98
use Illuminate\Contracts\Queue\ShouldQueue;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace App\Jobs;
3+
namespace App\Jobs\Ookla;
44

55
use App\Actions\GetExternalIpAddress;
66
use App\Enums\ResultStatus;

0 commit comments

Comments
 (0)