-
-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature] Add Average for Download, Upload, Ping (#1837)
* Add helper and average line * Fix query --------- Co-authored-by: Alex Justesen <[email protected]>
- Loading branch information
1 parent
5305b4f
commit fbf271f
Showing
7 changed files
with
168 additions
and
49 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
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
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
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,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); | ||
} | ||
} |