forked from alexjustesen/speedtest-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBenchmarkSpeedtestJob.php
More file actions
125 lines (103 loc) · 3.55 KB
/
BenchmarkSpeedtestJob.php
File metadata and controls
125 lines (103 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
namespace App\Jobs\Ookla;
use App\Enums\ResultStatus;
use App\Events\SpeedtestBenchmarkFailed;
use App\Events\SpeedtestBenchmarking;
use App\Events\SpeedtestBenchmarkPassed;
use App\Helpers\Benchmark;
use App\Models\Result;
use App\Settings\ThresholdSettings;
use Illuminate\Bus\Batchable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Queue\Middleware\SkipIfBatchCancelled;
use Illuminate\Support\Arr;
class BenchmarkSpeedtestJob implements ShouldQueue
{
use Batchable, Queueable;
public bool $healthy = true;
/**
* Create a new job instance.
*/
public function __construct(
public Result $result,
) {}
/**
* Get the middleware the job should pass through.
*/
public function middleware(): array
{
return [
new SkipIfBatchCancelled,
];
}
/**
* Execute the job.
*/
public function handle(): void
{
$settings = app(ThresholdSettings::class);
if ($settings->absolute_enabled == false) {
return;
}
$this->result->update([
'status' => ResultStatus::Benchmarking,
]);
SpeedtestBenchmarking::dispatch($this->result);
$benchmarks = $this->benchmark(
result: $this->result,
settings: $settings,
);
if (! count($benchmarks)) {
return;
}
$this->result->update([
'benchmarks' => $benchmarks,
'healthy' => $this->healthy,
]);
$this->healthy
? SpeedtestBenchmarkPassed::dispatch($this->result)
: SpeedtestBenchmarkFailed::dispatch($this->result);
}
private function benchmark(Result $result, ThresholdSettings $settings): array
{
$benchmarks = [];
if (! blank($settings->absolute_download) && $settings->absolute_download > 0) {
$benchmarks = Arr::add($benchmarks, 'download', [
'bar' => 'min',
'passed' => Benchmark::bitrate($result->download, ['value' => $settings->absolute_download, 'unit' => 'mbps']),
'type' => 'absolute',
'value' => $settings->absolute_download,
'unit' => 'mbps',
]);
if (Arr::get($benchmarks, 'download.passed') == false) {
$this->healthy = false;
}
}
if (! blank($settings->absolute_upload) && $settings->absolute_upload > 0) {
$benchmarks = Arr::add($benchmarks, 'upload', [
'bar' => 'min',
'passed' => filter_var(Benchmark::bitrate($result->upload, ['value' => $settings->absolute_upload, 'unit' => 'mbps']), FILTER_VALIDATE_BOOLEAN),
'type' => 'absolute',
'value' => $settings->absolute_upload,
'unit' => 'mbps',
]);
if (Arr::get($benchmarks, 'upload.passed') == false) {
$this->healthy = false;
}
}
if (! blank($settings->absolute_ping) && $settings->absolute_ping > 0) {
$benchmarks = Arr::add($benchmarks, 'ping', [
'bar' => 'max',
'passed' => Benchmark::ping($result->ping, ['value' => $settings->absolute_ping]),
'type' => 'absolute',
'value' => $settings->absolute_ping,
'unit' => 'ms',
]);
if (Arr::get($benchmarks, 'ping.passed') == false) {
$this->healthy = false;
}
}
return $benchmarks;
}
}