mirror of
https://github.com/alexjustesen/speedtest-tracker.git
synced 2026-06-23 06:20:09 +00:00
fbf271fe51
* Add helper and average line * Fix query --------- Co-authored-by: Alex Justesen <alexjustesen@users.noreply.github.com>
45 lines
1.4 KiB
PHP
45 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Helpers;
|
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
class Average
|
|
{
|
|
/**
|
|
* Calculate the average download speed from a collection of results.
|
|
*/
|
|
public static function averageDownload(Collection $results, int $precision = 2, string $magnitude = 'mbit'): float
|
|
{
|
|
return round(
|
|
$results->map(function ($item) use ($magnitude, $precision) {
|
|
return ! blank($item->download)
|
|
? Number::bitsToMagnitude(bits: $item->download_bits, precision: $precision, magnitude: $magnitude)
|
|
: 0;
|
|
})->avg(),
|
|
$precision
|
|
);
|
|
}
|
|
|
|
public static function averageUpload(Collection $results, int $precision = 2, string $magnitude = 'mbit'): float
|
|
{
|
|
return round(
|
|
$results->map(function ($item) use ($magnitude, $precision) {
|
|
return ! blank($item->upload)
|
|
? Number::bitsToMagnitude(bits: $item->upload_bits, precision: $precision, magnitude: $magnitude)
|
|
: 0;
|
|
})->avg(),
|
|
$precision
|
|
);
|
|
}
|
|
|
|
public static function averagePing(Collection $results, int $precision = 2): float
|
|
{
|
|
$avgPing = $results->filter(function ($item) {
|
|
return ! blank($item->ping);
|
|
})->avg('ping');
|
|
|
|
return round($avgPing, $precision);
|
|
}
|
|
}
|