From 12581a5dc0c1d6e4d8a8b8d9b9b44ef5f21df583 Mon Sep 17 00:00:00 2001 From: Roland Gruber Date: Sun, 14 Jun 2020 21:22:50 +0200 Subject: [PATCH] format usage --- lam/lib/modules/quota.inc | 57 +++++++++++++++++++++++++++++ lam/tests/lib/modules/quotaTest.php | 30 ++++++++++++++- 2 files changed, 86 insertions(+), 1 deletion(-) diff --git a/lam/lib/modules/quota.inc b/lam/lib/modules/quota.inc index df05899a..8e54c25b 100644 --- a/lam/lib/modules/quota.inc +++ b/lam/lib/modules/quota.inc @@ -206,8 +206,10 @@ class quota extends baseModule { } $allQuotas[$i] = substr($allQuotas[$i], strlen(self::$QUOTA_PREFIX)); $singleQuota = explode(",", $allQuotas[$i]); + $singleQuota[1] = $this->formatBlockUsage($singleQuota[1]); $singleQuota[2] = $this->addBlockUnits($singleQuota[2]); $singleQuota[3] = $this->addBlockUnits($singleQuota[3]); + $singleQuota[5] = $this->formatInodeUsage($singleQuota[5]); $singleQuota[6] = $this->addInodeUnits($singleQuota[6]); $singleQuota[7] = $this->addInodeUnits($singleQuota[7]); $this->quota[$server][$i] = $singleQuota; @@ -231,6 +233,7 @@ class quota extends baseModule { * Adds units (M/G/T) for block numbers. * * @param int $value raw value + * @return string value with unit */ public function addBlockUnits($value) { $mebibytes = 1024; @@ -251,10 +254,35 @@ class quota extends baseModule { return $value; } + /** + * Formats block usage. + * + * @param int $value raw value + */ + public function formatBlockUsage($value) { + $mebibytes = 1024; + $gibibytes = 1024 * $mebibytes; + $tebibytes = 1024 * $gibibytes; + if (empty($value) || !get_preg($value, 'digit') || ($value < $mebibytes)) { + return $value; + } + if ($value >= $tebibytes) { + return round($value / $tebibytes, 3) . 'T'; + } + if ($value >= $gibibytes) { + return round($value / $gibibytes, 3) . 'G'; + } + if ($value >= $mebibytes) { + return round($value / $mebibytes, 3) . 'M'; + } + return $value; + } + /** * Adds units (m/g/t) for inode numbers. * * @param int $value raw value + * @return string value with unit */ public function addInodeUnits($value) { $kilo = 1000; @@ -279,6 +307,35 @@ class quota extends baseModule { return $value; } + /** + * Formats the inode usage. + * + * @param int $value raw value + * @return string value with unit + */ + public function formatInodeUsage($value) { + $kilo = 1000; + $million = 1000 * $kilo; + $billion = 1000 * $million; + $trillion = 1000 * $billion; + if (empty($value) || !get_preg($value, 'digit')) { + return $value; + } + if ($value >= $trillion) { + return round($value / $trillion, 3) . 't'; + } + if ($value >= $billion) { + return round($value / $billion, 3) . 'g'; + } + if ($value >= $million) { + return round($value / $million, 3) . 'm'; + } + if ($value >= $kilo) { + return round($value / $kilo, 3) . 'k'; + } + return $value; + } + /** * Gets the cn from the Unix group module. * diff --git a/lam/tests/lib/modules/quotaTest.php b/lam/tests/lib/modules/quotaTest.php index a5f9a527..cab644a6 100644 --- a/lam/tests/lib/modules/quotaTest.php +++ b/lam/tests/lib/modules/quotaTest.php @@ -33,7 +33,7 @@ include_once __DIR__ . '/../../../lib/modules/quota.inc'; class QuotaTest extends TestCase { protected function setUp(): void { - $_SESSION = array('language' => 'de_DE.utf8'); + $_SESSION = array('language' => 'en_GB.utf8'); } public function testAddBlockUnits() { @@ -49,6 +49,19 @@ class QuotaTest extends TestCase { $this->assertEquals('5000', $quota->addBlockUnits(5000)); } + public function testFormatBlockUsage() { + $quota = new quota('user'); + + $this->assertEquals('123T', $quota->formatBlockUsage(1024*1024*1024*123)); + $this->assertEquals('123G', $quota->formatBlockUsage(1024*1024*123)); + $this->assertEquals('123M', $quota->formatBlockUsage(1024*123)); + $this->assertEquals('123', $quota->formatBlockUsage(123)); + $this->assertEquals('1.001M', $quota->formatBlockUsage(1025)); + $this->assertEquals('4.883T', $quota->formatBlockUsage(1024*1024*5000)); + $this->assertEquals('4.883G', $quota->formatBlockUsage(1024*5000)); + $this->assertEquals('4.883M', $quota->formatBlockUsage(5000)); + } + public function testAddInodeUnits() { $quota = new quota('user'); @@ -64,4 +77,19 @@ class QuotaTest extends TestCase { $this->assertEquals('5001', $quota->addInodeUnits(5001)); } + public function testFormatInodeUsage() { + $quota = new quota('user'); + + $this->assertEquals('123t', $quota->formatInodeUsage(1000*1000*1000*1000*123)); + $this->assertEquals('123g', $quota->formatInodeUsage(1000*1000*1000*123)); + $this->assertEquals('123m', $quota->formatInodeUsage(1000*1000*123)); + $this->assertEquals('123k', $quota->formatInodeUsage(1000*123)); + $this->assertEquals('123', $quota->formatInodeUsage(123)); + $this->assertEquals('1.025k', $quota->formatInodeUsage(1025)); + $this->assertEquals('5.001t', $quota->formatInodeUsage(1000*1000*1000*5001)); + $this->assertEquals('5.001g', $quota->formatInodeUsage(1000*1000*5001)); + $this->assertEquals('5.001m', $quota->formatInodeUsage(1000*5001)); + $this->assertEquals('5.001k', $quota->formatInodeUsage(5001)); + } + }