Subversion Repositories ALCASAR

Rev

Rev 2809 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log

Rev 2809 Rev 3106
Line 15... Line 15...
15
 *
15
 *
16
 * You should have received a copy of the GNU General Public License
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/>.
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 */
18
 */
19
 
19
 
-
 
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
 
20
// Get the largest value in an array
33
// Get the largest value in an array
21
function getLargestValue($array) {
34
function getLargestValue($array) {
22
    return $max = array_reduce($array, function ($a, $b) {
35
    return $max = array_reduce($array, function ($a, $b) {
23
        return $a > $b['total'] ? $a : $b['total'];
36
        return $a > $b['total'] ? $a : $b['total'];
24
    });
37
    });
25
}
38
}
26
 
39
 
-
 
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
 
27
function formatSize($bytes, $vnstatJsonVersion) {
76
function formatSize($bytes, $vnstatJsonVersion, $decimals = 2) {
-
 
77
 
28
    // json version 1 = convert from KiB
78
    // json version 1 = convert from KiB
29
    // json version 2 = convert from bytes
79
    // json version 2 = convert from bytes
30
    if ($vnstatJsonVersion == 1) {
80
    if ($vnstatJsonVersion == 1) {
31
        $bytes *= 1024;  // convert from kibibytes to bytes
81
        $bytes *= 1024;  // convert from kibibytes to bytes
32
    }
82
    }
33
 
83
 
34
    return formatBytes($bytes);
84
    return formatBytes($bytes, $decimals);
35
}
85
}
36
 
86
 
37
function formatBytes($bytes, $decimals = 2) {
87
function getLargestPrefix($scale)
38
    $base = log(floatval($bytes), 1024);
-
 
-
 
88
{
39
    $suffixes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
89
    $suffixes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
-
 
90
    return $suffixes[$scale];
-
 
91
}
-
 
92
 
-
 
93
function formatBytes($bytes, $decimals = 3) {
40
 
94
 
-
 
95
    $scale = getScale($bytes);
-
 
96
 
41
    return round(pow(1024, $base - floor($base)), $decimals) .' '. $suffixes[floor($base)];
97
    return round($bytes/pow(1024, $scale), $decimals) .' '. getLargestPrefix($scale);
42
}
98
}
43
 
99
 
44
function formatBytesTo($bytes, $delimiter, $decimals = 2) {
100
function formatBytesTo($bytes, $scale, $decimals = 4) {
-
 
101
 
45
    if ($bytes == 0) {
102
    if ($bytes == 0) {
46
        return '0';
103
        return '0';
47
    }
104
    }
48
 
105
 
49
    $k = 1024;
-
 
50
    $dm = $decimals < 0 ? 0 : $decimals;
-
 
51
    $sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
-
 
52
 
-
 
53
    $i = array_search($delimiter, $sizes);
-
 
54
 
-
 
55
    return number_format(($bytes / pow($k, $i)), $decimals);
106
    return number_format(($bytes / pow(1024, $scale)), $decimals, ".", "");
56
}
107
}
57
 
108
 
58
function kibibytesToBytes($kibibytes, $vnstatJsonVersion) {
109
function kibibytesToBytes($kibibytes, $vnstatJsonVersion) {
59
    if ($vnstatJsonVersion == 1) {
110
    if ($vnstatJsonVersion == 1) {
60
        return $kibibytes *= 1024;
111
        return $kibibytes *= 1024;
61
    } else {
112
    } else {
62
        return $kibibytes;
113
        return $kibibytes;
63
    }
114
    }
64
}
115
}
65
 
116
 
66
function getLargestPrefix($kb)
-
 
67
{
-
 
68
    $units = ['TB', 'GB', 'MB', 'KB', 'B'];
-
 
69
    $scale = 1024 * 1024 * 1024 * 1024;
-
 
70
    $ui = 0;
-
 
71
 
-
 
72
    while ((($kb < $scale) && ($scale > 1))) {
-
 
73
        $ui++;
-
 
74
        $scale = $scale / 1024;
-
 
75
    }
-
 
76
 
-
 
77
    return $units[$ui];
-
 
78
}
-
 
79
 
-
 
80
function sortingFunction($item1, $item2) {
117
function sortingFunction($item1, $item2) {
81
    if ($item1['time'] == $item2['time']) {
118
    if ($item1['time'] == $item2['time']) {
82
        return 0;
119
        return 0;
83
    } else {
120
    } else {
84
        return $item1['time'] > $item2['time'] ? -1 : 1;
121
        return $item1['time'] > $item2['time'] ? -1 : 1;