Skip to content

Commit

Permalink
added healthy indicator to results
Browse files Browse the repository at this point in the history
  • Loading branch information
alexjustesen committed Nov 23, 2024
1 parent b39a692 commit df129d8
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 5 deletions.
60 changes: 60 additions & 0 deletions app/Actions/Ookla/EvaluateResultBenchmarks.php
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;
}
}
}
9 changes: 9 additions & 0 deletions app/Filament/Resources/ResultResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,10 @@ public static function table(Table $table): Table
->formatStateUsing(function ($state) {
return number_format((float) $state, 2, '.', '').' %';
}),
Tables\Columns\IconColumn::make('healthy')
->alignCenter()
->boolean()
->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('status')
->badge()
->toggleable()
Expand All @@ -330,6 +334,9 @@ public static function table(Table $table): Table
->alignment(Alignment::End),
])
->filters([
Tables\Filters\TernaryFilter::make('healthy')
->nullable(),

Tables\Filters\SelectFilter::make('ip_address')
->label('IP address')
->multiple()
Expand All @@ -346,6 +353,7 @@ public static function table(Table $table): Table
->toArray();
})
->attribute('data->interface->externalIp'),

Tables\Filters\TernaryFilter::make('scheduled')
->placeholder('-')
->trueLabel('Only scheduled speedtests')
Expand All @@ -355,6 +363,7 @@ public static function table(Table $table): Table
false: fn (Builder $query) => $query->where('scheduled', false),
blank: fn (Builder $query) => $query,
),

Tables\Filters\SelectFilter::make('status')
->multiple()
->options(ResultStatus::class),
Expand Down
94 changes: 94 additions & 0 deletions app/Helpers/Bitrate.php
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]
);
}
}
12 changes: 7 additions & 5 deletions app/Jobs/Ookla/BenchmarkSpeedtestJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Jobs\Ookla;

use App\Actions\Ookla\EvaluateResultBenchmarks;
use App\Enums\ResultStatus;
use App\Events\SpeedtestBenchmarking;
use App\Models\Result;
Expand Down Expand Up @@ -45,13 +46,14 @@ public function handle(): void

$benchmarks = $this->buildBenchmarks($settings);

if (count($benchmarks) > 0) {
$this->result->update([
'benchmarks' => $benchmarks,
]);
} else {
if (! count($benchmarks)) {
return;
}

$this->result->update([
'benchmarks' => $benchmarks,
'healthy' => EvaluateResultBenchmarks::run($this->result, $benchmarks),
]);
}

private function buildBenchmarks(ThresholdSettings $settings): array
Expand Down
1 change: 1 addition & 0 deletions app/Models/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ protected function casts(): array
return [
'benchmarks' => 'array',
'data' => 'array',
'healthy' => 'boolean',
'service' => ResultService::class,
'status' => ResultStatus::class,
'scheduled' => 'boolean',
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->boolean('healthy')
->nullable()
->after('benchmarks');
});
}

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

0 comments on commit df129d8

Please sign in to comment.