| 2809 |
rexy |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* Copyright (C) 2019 Alexander Marston (alexander.marston@gmail.com)
|
|
|
5 |
*
|
|
|
6 |
* This program is free software: you can redistribute it and/or modify
|
|
|
7 |
* it under the terms of the GNU General Public License as published by
|
|
|
8 |
* the Free Software Foundation, either version 3 of the License, or
|
|
|
9 |
* (at your option) any later version.
|
|
|
10 |
*
|
|
|
11 |
* This program is distributed in the hope that it will be useful,
|
|
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
14 |
* GNU General Public License for more details.
|
|
|
15 |
*
|
|
|
16 |
* You should have received a copy of the GNU General Public License
|
|
|
17 |
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
18 |
*/
|
|
|
19 |
|
| 3106 |
rexy |
20 |
$logk = log(1024);
|
|
|
21 |
|
|
|
22 |
function getScale($bytes)
|
|
|
23 |
{
|
|
|
24 |
global $logk;
|
|
|
25 |
|
|
|
26 |
$ui = floor(round(log($bytes)/$logk,3));
|
|
|
27 |
if ($ui < 0) { $ui = 0; }
|
|
|
28 |
if ($ui > 8) { $ui = 8; }
|
|
|
29 |
|
|
|
30 |
return $ui;
|
|
|
31 |
}
|
|
|
32 |
|
| 2809 |
rexy |
33 |
// Get the largest value in an array
|
|
|
34 |
function getLargestValue($array) {
|
|
|
35 |
return $max = array_reduce($array, function ($a, $b) {
|
|
|
36 |
return $a > $b['total'] ? $a : $b['total'];
|
|
|
37 |
});
|
|
|
38 |
}
|
|
|
39 |
|
| 3106 |
rexy |
40 |
function getBaseValue($array, $scale)
|
|
|
41 |
{
|
|
|
42 |
$big = pow(1024,9);
|
|
|
43 |
|
|
|
44 |
// Find the smallest non-zero value
|
|
|
45 |
$sml = array_reduce($array, function ($a, $b) {
|
|
|
46 |
if ((1 <= $b['rx']) && ($b['rx'] < $b['tx'])) {
|
|
|
47 |
$sm = $b['rx'];
|
|
|
48 |
} else {
|
|
|
49 |
$sm = $b['tx'];
|
|
|
50 |
}
|
|
|
51 |
if (($sm < 1) || ($a < $sm)) {
|
|
|
52 |
return $a;
|
|
|
53 |
} else {
|
|
|
54 |
return $sm;
|
|
|
55 |
}
|
|
|
56 |
}, $big);
|
|
|
57 |
|
|
|
58 |
if ($sml >= $big/2) {
|
|
|
59 |
$sml = 1;
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
// divide by scale then round down to a power of 10
|
|
|
63 |
$base = pow(10,floor(round(log10($sml/pow(1024,$scale)),3)));
|
|
|
64 |
|
|
|
65 |
// convert back to bytes
|
|
|
66 |
$baseByte = $base * pow(1024, $scale);
|
|
|
67 |
|
|
|
68 |
// Don't make the bar invisable - must be > 5% difference
|
|
|
69 |
if ($sml / $baseByte < 1.05) {
|
|
|
70 |
$base = $base / 10;
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
return $base;
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
function formatSize($bytes, $vnstatJsonVersion, $decimals = 2) {
|
|
|
77 |
|
| 2809 |
rexy |
78 |
// json version 1 = convert from KiB
|
|
|
79 |
// json version 2 = convert from bytes
|
|
|
80 |
if ($vnstatJsonVersion == 1) {
|
|
|
81 |
$bytes *= 1024; // convert from kibibytes to bytes
|
|
|
82 |
}
|
|
|
83 |
|
| 3106 |
rexy |
84 |
return formatBytes($bytes, $decimals);
|
| 2809 |
rexy |
85 |
}
|
|
|
86 |
|
| 3106 |
rexy |
87 |
function getLargestPrefix($scale)
|
|
|
88 |
{
|
| 2809 |
rexy |
89 |
$suffixes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
| 3106 |
rexy |
90 |
return $suffixes[$scale];
|
|
|
91 |
}
|
| 2809 |
rexy |
92 |
|
| 3106 |
rexy |
93 |
function formatBytes($bytes, $decimals = 3) {
|
|
|
94 |
|
|
|
95 |
$scale = getScale($bytes);
|
|
|
96 |
|
|
|
97 |
return round($bytes/pow(1024, $scale), $decimals) .' '. getLargestPrefix($scale);
|
| 2809 |
rexy |
98 |
}
|
|
|
99 |
|
| 3106 |
rexy |
100 |
function formatBytesTo($bytes, $scale, $decimals = 4) {
|
|
|
101 |
|
| 2809 |
rexy |
102 |
if ($bytes == 0) {
|
|
|
103 |
return '0';
|
|
|
104 |
}
|
|
|
105 |
|
| 3106 |
rexy |
106 |
return number_format(($bytes / pow(1024, $scale)), $decimals, ".", "");
|
| 2809 |
rexy |
107 |
}
|
|
|
108 |
|
|
|
109 |
function kibibytesToBytes($kibibytes, $vnstatJsonVersion) {
|
|
|
110 |
if ($vnstatJsonVersion == 1) {
|
|
|
111 |
return $kibibytes *= 1024;
|
|
|
112 |
} else {
|
|
|
113 |
return $kibibytes;
|
|
|
114 |
}
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
function sortingFunction($item1, $item2) {
|
|
|
118 |
if ($item1['time'] == $item2['time']) {
|
|
|
119 |
return 0;
|
|
|
120 |
} else {
|
|
|
121 |
return $item1['time'] > $item2['time'] ? -1 : 1;
|
|
|
122 |
}
|
|
|
123 |
};
|
|
|
124 |
|
|
|
125 |
?>
|