forked from alexjustesen/speedtest-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAverage.php
More file actions
44 lines (38 loc) · 1.36 KB
/
Average.php
File metadata and controls
44 lines (38 loc) · 1.36 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
<?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);
}
}