format usage

This commit is contained in:
Roland Gruber 2020-06-14 21:22:50 +02:00
parent 2fcabf0c07
commit 12581a5dc0
2 changed files with 86 additions and 1 deletions

View File

@ -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.
*

View File

@ -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));
}
}