diff options
-rw-r--r-- | lib/OperatingSystems/DefaultOs.php | 27 | ||||
-rw-r--r-- | lib/OperatingSystems/FreeBSD.php | 27 | ||||
-rw-r--r-- | lib/OperatingSystems/IOperatingSystem.php | 20 | ||||
-rw-r--r-- | lib/Os.php | 16 | ||||
-rw-r--r-- | lib/Resources/Disk.php | 83 | ||||
-rw-r--r-- | templates/settings-admin.php | 16 | ||||
-rw-r--r-- | tests/lib/DefaultOsTest.php | 90 |
7 files changed, 173 insertions, 106 deletions
diff --git a/lib/OperatingSystems/DefaultOs.php b/lib/OperatingSystems/DefaultOs.php index ed0a665..e69c7f7 100644 --- a/lib/OperatingSystems/DefaultOs.php +++ b/lib/OperatingSystems/DefaultOs.php @@ -20,6 +20,7 @@ namespace OCA\ServerInfo\OperatingSystems; +use OCA\ServerInfo\Resources\Disk; use OCA\ServerInfo\Resources\Memory; class DefaultOs implements IOperatingSystem { @@ -190,15 +191,6 @@ class DefaultOs implements IOperatingSystem { return $result; } - /** - * Get diskInfo will return a list of disks. Used and Available in bytes. - * - * [ - * [device => /dev/mapper/homestead--vg-root, fs => ext4, used => 6205468, available => 47321220, percent => 12%, mount => /] - * ] - * - * @return array - */ public function getDiskInfo(): array { $data = []; @@ -221,14 +213,15 @@ class DefaultOs implements IOperatingSystem { continue; } - $data[] = [ - 'device' => $filesystem, - 'fs' => $matches['Type'][$i], - 'used' => (int)$matches['Used'][$i] * 1024, - 'available' => (int)$matches['Available'][$i] * 1024, - 'percent' => $matches['Capacity'][$i], - 'mount' => $matches['Mounted'][$i], - ]; + $disk = new Disk(); + $disk->setDevice($filesystem); + $disk->setFs($matches['Type'][$i]); + $disk->setUsed((int)$matches['Used'][$i] * 1024); + $disk->setAvailable((int)$matches['Available'][$i] * 1024); + $disk->setPercent($matches['Capacity'][$i]); + $disk->setMount($matches['Mounted'][$i]); + + $data[] = $disk; } return $data; diff --git a/lib/OperatingSystems/FreeBSD.php b/lib/OperatingSystems/FreeBSD.php index 31dc39b..4c27c6c 100644 --- a/lib/OperatingSystems/FreeBSD.php +++ b/lib/OperatingSystems/FreeBSD.php @@ -22,6 +22,7 @@ declare(strict_types=1); namespace OCA\ServerInfo\OperatingSystems; +use OCA\ServerInfo\Resources\Disk; use OCA\ServerInfo\Resources\Memory; /** @@ -213,15 +214,6 @@ class FreeBSD implements IOperatingSystem { return $result; } - /** - * Get diskInfo will return a list of disks. Used and Available in bytes. - * - * [ - * [device => /dev/mapper/homestead--vg-root, fs => ext4, used => 6205468, available => 47321220, percent => 12%, mount => /] - * ] - * - * @return array - */ public function getDiskInfo(): array { $data = []; @@ -244,14 +236,15 @@ class FreeBSD implements IOperatingSystem { continue; } - $data[] = [ - 'device' => $filesystem, - 'fs' => $matches['Type'][$i], - 'used' => (int)$matches['Used'][$i] * 1024, - 'available' => (int)$matches['Available'][$i] * 1024, - 'percent' => $matches['Capacity'][$i], - 'mount' => $matches['Mounted'][$i], - ]; + $disk = new Disk(); + $disk->setDevice($filesystem); + $disk->setFs($matches['Type'][$i]); + $disk->setUsed((int)$matches['Used'][$i] * 1024); + $disk->setAvailable((int)$matches['Available'][$i] * 1024); + $disk->setPercent($matches['Capacity'][$i]); + $disk->setMount($matches['Mounted'][$i]); + + $data[] = $disk; } return $data; diff --git a/lib/OperatingSystems/IOperatingSystem.php b/lib/OperatingSystems/IOperatingSystem.php index 000e536..5238e96 100644 --- a/lib/OperatingSystems/IOperatingSystem.php +++ b/lib/OperatingSystems/IOperatingSystem.php @@ -25,22 +25,30 @@ declare(strict_types=1); namespace OCA\ServerInfo\OperatingSystems; +use OCA\ServerInfo\Resources\Disk; use OCA\ServerInfo\Resources\Memory; interface IOperatingSystem { /** - * Get memory returns a Memory object. All values are in bytes. + * Get name of the processor * - * @return Memory + * @return string */ - public function getMemory(): Memory; + public function getCpuName(): string; /** - * Get name of the processor + * Get disk info returns a list of Disk objects. Used and Available in bytes. * - * @return string + * @return Disk[] */ - public function getCpuName(): string; + public function getDiskInfo(): array; + + /** + * Get memory returns a Memory object. All values are in bytes. + * + * @return Memory + */ + public function getMemory(): Memory; /** * Get the total number of seconds the system has been up or -1 on failure. @@ -91,15 +91,6 @@ class Os implements IOperatingSystem { return explode("\n", $data); } - /** - * Get diskInfo will return a list of disks. Used and Available in bytes. - * - * [ - * [device => /dev/mapper/homestead--vg-root, fs => ext4, used => 6205468, available => 47321220, percent => 12%, mount => /] - * ] - * - * @return array - */ public function getDiskInfo(): array { return $this->backend->getDiskInfo(); } @@ -115,12 +106,11 @@ class Os implements IOperatingSystem { */ public function getDiskData(): array { $data = []; - $disks = $this->backend->getDiskInfo(); - foreach ($disks as $disk) { + foreach ($this->backend->getDiskInfo() as $disk) { $data[] = [ - round($disk['used'] / 1024 / 1024 / 1024, 1), - round($disk['available'] / 1024 / 1024 / 1024, 1) + round($disk->getUsed() / 1024 / 1024 / 1024, 1), + round($disk->getAvailable() / 1024 / 1024 / 1024, 1) ]; } diff --git a/lib/Resources/Disk.php b/lib/Resources/Disk.php new file mode 100644 index 0000000..1c6893a --- /dev/null +++ b/lib/Resources/Disk.php @@ -0,0 +1,83 @@ +<?php + +declare(strict_types=1); +/** + * @copyright Copyright (c) 2020 Daniel Kesselberg <mail@danielkesselberg.de> + * + * @author Daniel Kesselberg <mail@danielkesselberg.de> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\ServerInfo\Resources; + +class Disk { + private $device = ''; + private $fs = ''; + private $used = 0; + private $available = 0; + private $percent = ''; + private $mount = ''; + + public function getDevice(): string { + return $this->device; + } + + public function setDevice(string $device): void { + $this->device = $device; + } + + public function getFs(): string { + return $this->fs; + } + + public function setFs(string $fs): void { + $this->fs = $fs; + } + + public function getUsed(): int { + return $this->used; + } + + public function setUsed(int $used): void { + $this->used = $used; + } + + public function getAvailable(): int { + return $this->available; + } + + public function setAvailable(int $available): void { + $this->available = $available; + } + + public function getPercent(): string { + return $this->percent; + } + + public function setPercent(string $percent): void { + $this->percent = $percent; + } + + public function getMount(): string { + return $this->mount; + } + + public function setMount(string $mount): void { + $this->mount = $mount; + } +} diff --git a/templates/settings-admin.php b/templates/settings-admin.php index f298208..f39fd6e 100644 --- a/templates/settings-admin.php +++ b/templates/settings-admin.php @@ -37,6 +37,8 @@ function FormatBytes($byte) { /** @var \OCA\ServerInfo\Resources\Memory $memory */ $memory = $_['memory']; +/** @var \OCA\ServerInfo\Resources\Disk[] $disks */ +$disks = $_['diskinfo']; ?> <div class="server-info-wrapper"> @@ -122,7 +124,7 @@ $memory = $_['memory']; <?php p($l->t('Disk')); ?> </h2> </div> - <?php foreach ($_['diskinfo'] as $disk): ?> + <?php foreach ($disks as $disk): ?> <div class="col col-4 col-xl-6 col-m-12"> <div class="infobox"> <div class="diskchart-container"> @@ -130,17 +132,17 @@ $memory = $_['memory']; height="200"></canvas> </div> <div class="diskinfo-container"> - <h3><?php p(basename($disk['device'])); ?></h3> + <h3><?php p(basename($disk->getDevice())); ?></h3> <?php p($l->t('Mount')); ?> : - <span class="info"><?php p($disk['mount']); ?></span><br> + <span class="info"><?php p($disk->getMount()); ?></span><br> <?php p($l->t('Filesystem')); ?> : - <span class="info"><?php p($disk['fs']); ?></span><br> + <span class="info"><?php p($disk->getFs()); ?></span><br> <?php p($l->t('Size')); ?> : - <span class="info"><?php p(FormatBytes($disk['used'] + $disk['available'])); ?></span><br> + <span class="info"><?php p(FormatBytes($disk->getUsed() + $disk->getAvailable())); ?></span><br> <?php p($l->t('Available')); ?> : - <span class="info"><?php p(FormatBytes($disk['available'])); ?></span><br> + <span class="info"><?php p(FormatBytes($disk->getAvailable())); ?></span><br> <?php p($l->t('Used')); ?> : - <span class="info"><?php p($disk['percent']); ?></span><br> + <span class="info"><?php p($disk->getPercent()); ?></span><br> </div> </div> </div> diff --git a/tests/lib/DefaultOsTest.php b/tests/lib/DefaultOsTest.php index 790b7ae..15de15b 100644 --- a/tests/lib/DefaultOsTest.php +++ b/tests/lib/DefaultOsTest.php @@ -24,6 +24,7 @@ namespace OCA\ServerInfo\Tests; use OCA\ServerInfo\OperatingSystems\DefaultOs; +use OCA\ServerInfo\Resources\Disk; use OCA\ServerInfo\Resources\Memory; use PHPUnit\Framework\MockObject\MockObject; use Test\TestCase; @@ -133,50 +134,47 @@ class DefaultOsTest extends TestCase { ->with('df -TP') ->willReturn(file_get_contents(__DIR__ . '/../data/df_tp')); - $disks = [ - [ - 'device' => '/dev/mapper/homestead--vg-root', - 'fs' => 'ext4', - 'used' => 6354399232, - 'available' => 48456929280, - 'percent' => '12%', - 'mount' => '/', - ], - [ - 'device' => '/dev/mapper/homestead--vg-mysql--master', - 'fs' => 'ext4', - 'used' => 263385088, - 'available' => 63388057600, - 'percent' => '1%', - 'mount' => '/homestead-vg/master', - ], - [ - 'device' => 'vagrant', - 'fs' => 'vboxsf', - 'used' => 629587079168, - 'available' => 351531044864, - 'percent' => '65%', - 'mount' => '/vagrant', - ], - [ - 'device' => 'home_vagrant_code', - 'fs' => 'vboxsf', - 'used' => 629587079168, - 'available' => 351531044864, - 'percent' => '65%', - 'mount' => '/home/vagrant/code', - ], - [ - 'device' => 'nfs.example.com:/export', - 'fs' => 'nfs4', - 'used' => 0, - 'available' => 1259520, - 'percent' => '0%', - 'mount' => '/nfs', - ] - ]; - - $this->assertSame($disks, $this->os->getDiskInfo()); + $disk1 = new Disk(); + $disk1->setDevice('/dev/mapper/homestead--vg-root'); + $disk1->setFs('ext4'); + $disk1->setUsed(6354399232); + $disk1->setAvailable(48456929280); + $disk1->setPercent('12%'); + $disk1->setMount('/'); + + $disk2 = new Disk(); + $disk2->setDevice('/dev/mapper/homestead--vg-mysql--master'); + $disk2->setFs('ext4'); + $disk2->setUsed(263385088); + $disk2->setAvailable(63388057600); + $disk2->setPercent('1%'); + $disk2->setMount('/homestead-vg/master'); + + $disk3 = new Disk(); + $disk3->setDevice('vagrant'); + $disk3->setFs('vboxsf'); + $disk3->setUsed(629587079168); + $disk3->setAvailable(351531044864); + $disk3->setPercent('65%'); + $disk3->setMount('/vagrant'); + + $disk4 = new Disk(); + $disk4->setDevice('home_vagrant_code'); + $disk4->setFs('vboxsf'); + $disk4->setUsed(629587079168); + $disk4->setAvailable(351531044864); + $disk4->setPercent('65%'); + $disk4->setMount('/home/vagrant/code'); + + $disk5 = new Disk(); + $disk5->setDevice('nfs.example.com:/export'); + $disk5->setFs('nfs4'); + $disk5->setUsed(0); + $disk5->setAvailable(1259520); + $disk5->setPercent('0%'); + $disk5->setMount('/nfs'); + + $this->assertEquals([$disk1, $disk2, $disk3, $disk4, $disk5], $this->os->getDiskInfo()); } public function testGetDiskInfoNoCommandOutput(): void { @@ -184,7 +182,7 @@ class DefaultOsTest extends TestCase { ->with('df -TP') ->willThrowException(new \RuntimeException('No output for command "df -TP"')); - $this->assertSame([], $this->os->getDiskInfo()); + $this->assertEquals([], $this->os->getDiskInfo()); } public function testGetDiskInfoInvalidCommandOutput(): void { @@ -192,7 +190,7 @@ class DefaultOsTest extends TestCase { ->with('df -TP') ->willReturn('invalid_data'); - $this->assertSame([], $this->os->getDiskInfo()); + $this->assertEquals([], $this->os->getDiskInfo()); } public function testSupported(): void { |