forked from alexjustesen/speedtest-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.php
More file actions
64 lines (49 loc) · 1.65 KB
/
helpers.php
File metadata and controls
64 lines (49 loc) · 1.65 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
use App\Models\Result;
use App\Settings\ThresholdSettings;
if (! function_exists('absoluteThresholdPassed')) {
function absoluteThresholdPassed(Result $result): bool
{
$thresholds = new (ThresholdSettings::class);
if (! $thresholds->absolute_enabled) {
return true;
}
return formatBits(formatBytesToBits($result->download), 2, false) > $thresholds->absolute_download
&& formatBits(formatBytesToBits($result->upload), 2, false) > $thresholds->absolute_upload
&& $result->ping < $thresholds->absolute_ping;
}
}
if (! function_exists('formatBits')) {
function formatBits(int $bits, $precision = 2, $suffix = true)
{
if ($bits > 0) {
$i = floor(log($bits) / log(1000));
if (! $suffix) {
return round($bits / pow(1000, $i), $precision);
}
$sizes = ['B', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb', 'Eb', 'Zb', 'Yb'];
return sprintf('%.02F', round($bits / pow(1000, $i), $precision)) * 1 .' '.@$sizes[$i];
}
return 0;
}
}
if (! function_exists('formatBytes')) {
function formatBytes(int $bytes, $precision = 2)
{
if ($bytes > 0) {
$i = floor(log($bytes) / log(1024));
$sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
return sprintf('%.02F', round($bytes / pow(1024, $i), $precision)) * 1 .' '.@$sizes[$i];
}
return 0;
}
}
if (! function_exists('formatBytesToBits')) {
function formatBytestoBits(int $bytes)
{
if ($bytes > 0) {
return $bytes * 8;
}
return 0;
}
}