utilities.php 3.0 KB

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