-
-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b39a692
commit df129d8
Showing
6 changed files
with
199 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
namespace App\Actions\Ookla; | ||
|
||
use App\Helpers\Bitrate; | ||
use App\Models\Result; | ||
use Illuminate\Support\Arr; | ||
use Lorisleiva\Actions\Concerns\AsAction; | ||
|
||
class EvaluateResultBenchmarks | ||
{ | ||
use AsAction; | ||
|
||
public bool $healthy = true; | ||
|
||
public function handle(Result $result, array $benchmarks): bool | ||
{ | ||
if (Arr::get($benchmarks, 'download', false)) { | ||
$this->checkBitrate($result->download, $benchmarks['download']); | ||
} | ||
|
||
if (Arr::get($benchmarks, 'upload', false)) { | ||
$this->checkBitrate($result->upload, $benchmarks['upload']); | ||
} | ||
|
||
if (Arr::get($benchmarks, 'ping', false)) { | ||
$this->checkPing($result->ping, $benchmarks['ping']); | ||
} | ||
|
||
return $this->healthy; | ||
} | ||
|
||
private function checkBitrate(float|int $bytes, array $benchmark): void | ||
{ | ||
$value = Arr::get($benchmark, 'value'); | ||
|
||
$unit = Arr::get($benchmark, 'unit'); | ||
|
||
if (blank($value) || blank($unit)) { | ||
return; | ||
} | ||
|
||
if (Bitrate::bytesToBits($bytes) < Bitrate::normalizeToBits($value.$unit)) { | ||
$this->healthy = false; | ||
} | ||
} | ||
|
||
private function checkPing(float|int $ping, array $benchmark): void | ||
{ | ||
$value = Arr::get($benchmark, 'value'); | ||
|
||
if (blank($value)) { | ||
return; | ||
} | ||
|
||
if ($ping > $value) { | ||
$this->healthy = false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
namespace App\Helpers; | ||
|
||
use InvalidArgumentException; | ||
|
||
class Bitrate | ||
{ | ||
/** | ||
* Units conversion map to bits | ||
* Base unit is bits (not bytes) | ||
*/ | ||
private const UNITS = [ | ||
'b' => 1, | ||
'kb' => 1000, | ||
'kib' => 1024, | ||
'mb' => 1000000, | ||
'mib' => 1048576, | ||
'gb' => 1000000000, | ||
'gib' => 1073741824, | ||
'tb' => 1000000000000, | ||
'tib' => 1099511627776, | ||
]; | ||
|
||
/** | ||
* Convert bytes to bits. | ||
*/ | ||
public static function bytesToBits(int|float $bytes): int|float | ||
{ | ||
if ($bytes < 0) { | ||
throw new InvalidArgumentException('Bytes value cannot be negative'); | ||
} | ||
|
||
// 1 byte = 8 bits | ||
return $bytes * 8; | ||
} | ||
|
||
/** | ||
* Parse and normalize any bit rate to bits. | ||
*/ | ||
public static function normalizeToBits(float|int|string $bitrate): float | ||
{ | ||
// If numeric, assume it's already in bits | ||
if (is_numeric($bitrate)) { | ||
return (float) $bitrate; | ||
} | ||
|
||
// Convert to lowercase and remove any whitespace | ||
$bitrate = strtolower(trim($bitrate)); | ||
|
||
// Remove 'ps' or 'per second' suffix if present | ||
$bitrate = str_replace(['ps', 'per second'], '', $bitrate); | ||
|
||
// Extract numeric value and unit | ||
if (! preg_match('/^([\d.]+)\s*([kmgt]?i?b)$/', $bitrate, $matches)) { | ||
throw new InvalidArgumentException( | ||
"Invalid bitrate format. Expected format: '1.5 Mb', '500kb', etc." | ||
); | ||
} | ||
|
||
$value = (float) $matches[1]; | ||
$unit = $matches[2]; | ||
|
||
// Validate unit | ||
if (! isset(self::UNITS[$unit])) { | ||
throw new InvalidArgumentException( | ||
"Invalid unit '$unit'. Supported units: ".implode(', ', array_keys(self::UNITS)) | ||
); | ||
} | ||
|
||
// Convert to bits | ||
return $value * self::UNITS[$unit]; | ||
} | ||
|
||
/** | ||
* Format bits to human readable string. | ||
*/ | ||
public static function formatBits(float $bits, bool $useBinaryPrefix = false, int $precision = 2): string | ||
{ | ||
$units = $useBinaryPrefix | ||
? ['b', 'Kib', 'Mib', 'Gib', 'Tib'] | ||
: ['b', 'kb', 'Mb', 'Gb', 'Tb']; | ||
|
||
$divisor = $useBinaryPrefix ? 1024 : 1000; | ||
$power = floor(($bits ? log($bits) : 0) / log($divisor)); | ||
$power = min($power, count($units) - 1); | ||
|
||
return sprintf( | ||
"%.{$precision}f %s", | ||
$bits / pow($divisor, $power), | ||
$units[$power] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
database/migrations/2024_11_23_021744_add_healthy_to_results_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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->boolean('healthy') | ||
->nullable() | ||
->after('benchmarks'); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
// | ||
} | ||
}; |