Files
Sven van Ginkel fbf271fe51 [Feature] Add Average for Download, Upload, Ping (#1837)
* Add helper and average line

* Fix query

---------

Co-authored-by: Alex Justesen <alexjustesen@users.noreply.github.com>
2024-11-27 08:43:44 -05:00

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);
}
}