Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/serverinfo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkesselb <mail@danielkesselberg.de>2020-08-13 19:47:44 +0300
committerGitHub <noreply@github.com>2020-08-13 19:47:44 +0300
commitf2d88f3e94fd47bb1453aa06dcb4b1378ee47ed3 (patch)
treecc5cf611dfaec45bb3698eabebb2b5157791d9a1
parent52b39a711672b18112edbc8aa9d599693d7dacc4 (diff)
parent2ede9269c67459aa51916eed57d0e3bd5242df63 (diff)
Merge pull request #222 from nextcloud/enh/noid/add-memory-object
Add memory object and interface
-rw-r--r--lib/OperatingSystems/DefaultOs.php49
-rw-r--r--lib/OperatingSystems/FreeBSD.php34
-rw-r--r--lib/OperatingSystems/IOperatingSystem.php51
-rw-r--r--lib/Os.php26
-rw-r--r--lib/Resources/Memory.php74
-rw-r--r--lib/Settings/AdminSettings.php2
-rw-r--r--templates/settings-admin.php4
-rw-r--r--tests/lib/DefaultOsTest.php29
-rw-r--r--tests/lib/FreeBSDTest.php35
9 files changed, 192 insertions, 112 deletions
diff --git a/lib/OperatingSystems/DefaultOs.php b/lib/OperatingSystems/DefaultOs.php
index 4ba800e..650fc4a 100644
--- a/lib/OperatingSystems/DefaultOs.php
+++ b/lib/OperatingSystems/DefaultOs.php
@@ -20,12 +20,9 @@
namespace OCA\ServerInfo\OperatingSystems;
-/**
- * Class Ubuntu
- *
- * @package OCA\ServerInfo\OperatingSystems
- */
-class DefaultOs {
+use OCA\ServerInfo\Resources\Memory;
+
+class DefaultOs implements IOperatingSystem {
/**
* @return bool
@@ -34,14 +31,8 @@ class DefaultOs {
return true;
}
- /**
- * Get memory will return a list key => value where all values are in bytes.
- * [MemTotal => 0, MemFree => 0, MemAvailable => 0, SwapTotal => 0, SwapFree => 0].
- *
- * @return array
- */
- public function getMemory(): array {
- $data = ['MemTotal' => -1, 'MemFree' => -1, 'MemAvailable' => -1, 'SwapTotal' => -1, 'SwapFree' => -1];
+ public function getMemory(): Memory {
+ $data = new Memory();
try {
$meminfo = $this->readContent('/proc/meminfo');
@@ -65,18 +56,29 @@ class DefaultOs {
$value *= 1024;
}
- $data[$key] = $value;
+ switch ($key) {
+ case 'MemTotal':
+ $data->setMemTotal($value);
+ break;
+ case 'MemFree':
+ $data->setMemFree($value);
+ break;
+ case 'MemAvailable':
+ $data->setMemAvailable($value);
+ break;
+ case 'SwapTotal':
+ $data->setSwapTotal($value);
+ break;
+ case 'SwapFree':
+ $data->setSwapFree($value);
+ break;
+ }
}
return $data;
}
- /**
- * Get name of the processor
- *
- * @return string
- */
- public function getCPUName(): string {
+ public function getCpuName(): string {
$data = 'Unknown Processor';
try {
@@ -113,11 +115,6 @@ class DefaultOs {
return $uptime;
}
- /**
- * Get the total number of seconds the system has been up or -1 on failure.
- *
- * @return int
- */
public function getUptime(): int {
$data = -1;
diff --git a/lib/OperatingSystems/FreeBSD.php b/lib/OperatingSystems/FreeBSD.php
index b4cea1f..31dc39b 100644
--- a/lib/OperatingSystems/FreeBSD.php
+++ b/lib/OperatingSystems/FreeBSD.php
@@ -22,12 +22,14 @@ declare(strict_types=1);
namespace OCA\ServerInfo\OperatingSystems;
+use OCA\ServerInfo\Resources\Memory;
+
/**
* Class FreeBSD
*
* @package OCA\ServerInfo\OperatingSystems
*/
-class FreeBSD {
+class FreeBSD implements IOperatingSystem {
/**
* @return bool
@@ -36,14 +38,8 @@ class FreeBSD {
return false;
}
- /**
- * Get memory will return a list key => value where all values are in bytes.
- * [MemTotal => 0, MemFree => 0, MemAvailable => 0, SwapTotal => 0, SwapFree => 0].
- *
- * @return array
- */
- public function getMemory(): array {
- $data = ['MemTotal' => -1, 'MemFree' => -1, 'MemAvailable' => -1, 'SwapTotal' => -1, 'SwapFree' => -1];
+ public function getMemory(): Memory {
+ $data = new Memory();
try {
$swapinfo = $this->executeCommand('/usr/sbin/swapinfo -k');
@@ -56,8 +52,8 @@ class FreeBSD {
$result = preg_match_all($pattern, $swapinfo, $matches);
if ($result === 1) {
- $data['SwapTotal'] = (int)$matches['Avail'][0];
- $data['SwapFree'] = $data['SwapTotal'] - (int)$matches['Used'][0];
+ $data->setSwapTotal((int)$matches['Avail'][0]);
+ $data->setSwapFree($data->getSwapTotal() - (int)$matches['Used'][0]);
}
unset($matches, $result);
@@ -70,8 +66,8 @@ class FreeBSD {
$lines = explode("\n", $meminfo);
if (count($lines) > 4) {
- $data['MemTotal'] = (int)$lines[0];
- $data['MemAvailable'] = (int)$lines[1] * ((int)$lines[2] + (int)$lines[3] + (int)$lines[4]);
+ $data->setMemTotal((int)$lines[0]);
+ $data->setMemAvailable((int)$lines[1] * ((int)$lines[2] + (int)$lines[3] + (int)$lines[4]));
}
unset($lines);
@@ -79,12 +75,7 @@ class FreeBSD {
return $data;
}
- /**
- * Get name of the processor
- *
- * @return string
- */
- public function getCPUName(): string {
+ public function getCpuName(): string {
$data = 'Unknown Processor';
try {
@@ -116,11 +107,6 @@ class FreeBSD {
return $time;
}
- /**
- * Get the total number of seconds the system has been up or -1 on failure.
- *
- * @return int
- */
public function getUptime(): int {
$uptime = -1;
diff --git a/lib/OperatingSystems/IOperatingSystem.php b/lib/OperatingSystems/IOperatingSystem.php
new file mode 100644
index 0000000..000e536
--- /dev/null
+++ b/lib/OperatingSystems/IOperatingSystem.php
@@ -0,0 +1,51 @@
+<?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\OperatingSystems;
+
+use OCA\ServerInfo\Resources\Memory;
+
+interface IOperatingSystem {
+ /**
+ * Get memory returns a Memory object. All values are in bytes.
+ *
+ * @return Memory
+ */
+ public function getMemory(): Memory;
+
+ /**
+ * Get name of the processor
+ *
+ * @return string
+ */
+ public function getCpuName(): string;
+
+ /**
+ * Get the total number of seconds the system has been up or -1 on failure.
+ *
+ * @return int
+ */
+ public function getUptime(): int;
+}
diff --git a/lib/Os.php b/lib/Os.php
index 5d5ee15..0736006 100644
--- a/lib/Os.php
+++ b/lib/Os.php
@@ -22,8 +22,10 @@ namespace OCA\ServerInfo;
use OCA\ServerInfo\OperatingSystems\DefaultOs;
use OCA\ServerInfo\OperatingSystems\FreeBSD;
+use OCA\ServerInfo\OperatingSystems\IOperatingSystem;
+use OCA\ServerInfo\Resources\Memory;
-class Os {
+class Os implements IOperatingSystem {
/** @var */
protected $backend;
@@ -61,23 +63,12 @@ class Os {
return PHP_OS . ' ' . php_uname('r') . ' ' . php_uname('m');
}
- /**
- * Get memory will return a list key => value where all values are in bytes.
- * [MemTotal => 0, MemFree => 0, MemAvailable => 0, SwapTotal => 0, SwapFree => 0].
- *
- * @return array
- */
- public function getMemory(): array {
+ public function getMemory(): Memory {
return $this->backend->getMemory();
}
- /**
- * Get name of the processor
- *
- * @return string
- */
- public function getCPUName(): string {
- return $this->backend->getCPUName();
+ public function getCpuName(): string {
+ return $this->backend->getCpuName();
}
/**
@@ -88,11 +79,6 @@ class Os {
return $data;
}
- /**
- * Get the total number of seconds the system has been up
- *
- * @return int
- */
public function getUptime(): int {
return $this->backend->getUptime();
}
diff --git a/lib/Resources/Memory.php b/lib/Resources/Memory.php
new file mode 100644
index 0000000..71bd603
--- /dev/null
+++ b/lib/Resources/Memory.php
@@ -0,0 +1,74 @@
+<?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 Memory {
+ private $memTotal = -1;
+ private $memFree = -1;
+ private $memAvailable = -1;
+ private $swapTotal = -1;
+ private $swapFree = -1;
+
+ public function getMemTotal(): int {
+ return $this->memTotal;
+ }
+
+ public function setMemTotal(int $memTotal): void {
+ $this->memTotal = $memTotal;
+ }
+
+ public function getMemFree(): int {
+ return $this->memFree;
+ }
+
+ public function setMemFree(int $memFree): void {
+ $this->memFree = $memFree;
+ }
+
+ public function getMemAvailable(): int {
+ return $this->memAvailable;
+ }
+
+ public function setMemAvailable(int $memAvailable): void {
+ $this->memAvailable = $memAvailable;
+ }
+
+ public function getSwapTotal(): int {
+ return $this->swapTotal;
+ }
+
+ public function setSwapTotal(int $swapTotal): void {
+ $this->swapTotal = $swapTotal;
+ }
+
+ public function getSwapFree(): int {
+ return $this->swapFree;
+ }
+
+ public function setSwapFree(int $swapFree): void {
+ $this->swapFree = $swapFree;
+ }
+}
diff --git a/lib/Settings/AdminSettings.php b/lib/Settings/AdminSettings.php
index 6244ec0..bde6270 100644
--- a/lib/Settings/AdminSettings.php
+++ b/lib/Settings/AdminSettings.php
@@ -105,7 +105,7 @@ class AdminSettings implements ISettings {
'hostname' => $this->os->getHostname(),
'osname' => $this->os->getOSName(),
'memory' => $this->os->getMemory(),
- 'cpu' => $this->os->getCPUName(),
+ 'cpu' => $this->os->getCpuName(),
'diskinfo' => $this->os->getDiskInfo(),
'networkinfo' => $this->os->getNetworkInfo(),
'networkinterfaces' => $this->os->getNetworkInterfaces(),
diff --git a/templates/settings-admin.php b/templates/settings-admin.php
index 7b78001..f298208 100644
--- a/templates/settings-admin.php
+++ b/templates/settings-admin.php
@@ -35,6 +35,8 @@ function FormatBytes($byte) {
return number_format($byte, 2, '.', '.') . ' ' . $unim[$count];
}
+/** @var \OCA\ServerInfo\Resources\Memory $memory */
+$memory = $_['memory'];
?>
<div class="server-info-wrapper">
@@ -62,7 +64,7 @@ function FormatBytes($byte) {
</tr>
<tr>
<td><?php p($l->t('Memory')); ?>:</td>
- <td><?php p(FormatBytes($_['memory']['MemTotal'])) ?></td>
+ <td><?php p(FormatBytes($memory->getMemTotal())) ?></td>
</tr>
<tr>
<td><?php p($l->t('Server time')); ?>:</td>
diff --git a/tests/lib/DefaultOsTest.php b/tests/lib/DefaultOsTest.php
index 5415e3a..7f3eb6c 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\Memory;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;
@@ -56,17 +57,11 @@ class DefaultOsTest extends TestCase {
$memory = $this->os->getMemory();
- $this->assertArrayHasKey('MemTotal', $memory);
- $this->assertArrayHasKey('MemFree', $memory);
- $this->assertArrayHasKey('MemAvailable', $memory);
- $this->assertArrayHasKey('SwapTotal', $memory);
- $this->assertArrayHasKey('SwapFree', $memory);
-
- $this->assertEquals(16330252 * 1024, $memory['MemTotal']);
- $this->assertEquals(2443908 * 1024, $memory['MemFree']);
- $this->assertEquals(7675276 * 1024, $memory['MemAvailable']);
- $this->assertEquals(999420 * 1024, $memory['SwapTotal']);
- $this->assertEquals(917756 * 1024, $memory['SwapFree']);
+ $this->assertEquals(16330252 * 1024, $memory->getMemTotal());
+ $this->assertEquals(2443908 * 1024, $memory->getMemFree());
+ $this->assertEquals(7675276 * 1024, $memory->getMemAvailable());
+ $this->assertEquals(999420 * 1024, $memory->getSwapTotal());
+ $this->assertEquals(917756 * 1024, $memory->getSwapFree());
}
public function testGetMemoryNoData(): void {
@@ -74,7 +69,7 @@ class DefaultOsTest extends TestCase {
->with('/proc/meminfo')
->willThrowException(new \RuntimeException('Unable to read: "/proc/meminfo"'));
- $this->assertSame(['MemTotal' => -1, 'MemFree' => -1, 'MemAvailable' => -1, 'SwapTotal' => -1, 'SwapFree' => -1], $this->os->getMemory());
+ $this->assertEquals(new Memory(), $this->os->getMemory());
}
public function testGetMemoryInvalidData(): void {
@@ -82,7 +77,7 @@ class DefaultOsTest extends TestCase {
->with('/proc/meminfo')
->willReturn('invalid_data');
- $this->assertSame(['MemTotal' => -1, 'MemFree' => -1, 'MemAvailable' => -1, 'SwapTotal' => -1, 'SwapFree' => -1], $this->os->getMemory());
+ $this->assertEquals(new Memory(), $this->os->getMemory());
}
public function testGetCPUName(): void {
@@ -90,7 +85,7 @@ class DefaultOsTest extends TestCase {
->with('/proc/cpuinfo')
->willReturn(file_get_contents(__DIR__ . '/../data/cpuinfo'));
- $this->assertEquals('Intel(R) Core(TM) i5-6500 CPU @ 3.20GHz (4 cores)', $this->os->getCPUName());
+ $this->assertEquals('Intel(R) Core(TM) i5-6500 CPU @ 3.20GHz (4 cores)', $this->os->getCpuName());
}
public function testGetCPUNameOneCore(): void {
@@ -98,7 +93,7 @@ class DefaultOsTest extends TestCase {
->with('/proc/cpuinfo')
->willReturn(file_get_contents(__DIR__ . '/../data/cpuinfo_one_core'));
- $this->assertEquals('Intel(R) Core(TM) i5-6500 CPU @ 3.20GHz (1 core)', $this->os->getCPUName());
+ $this->assertEquals('Intel(R) Core(TM) i5-6500 CPU @ 3.20GHz (1 core)', $this->os->getCpuName());
}
public function testGetCPUNameNoData(): void {
@@ -106,7 +101,7 @@ class DefaultOsTest extends TestCase {
->with('/proc/cpuinfo')
->willThrowException(new \RuntimeException('Unable to read: "/proc/cpuinfo"'));
- $this->assertEquals('Unknown Processor', $this->os->getCPUName());
+ $this->assertEquals('Unknown Processor', $this->os->getCpuName());
}
public function testGetCPUNameInvalidData(): void {
@@ -114,7 +109,7 @@ class DefaultOsTest extends TestCase {
->with('/proc/cpuinfo')
->willReturn('invalid_data');
- $this->assertEquals('Unknown Processor', $this->os->getCPUName());
+ $this->assertEquals('Unknown Processor', $this->os->getCpuName());
}
public function testGetUptime(): void {
diff --git a/tests/lib/FreeBSDTest.php b/tests/lib/FreeBSDTest.php
index a9355ca..1b4c20e 100644
--- a/tests/lib/FreeBSDTest.php
+++ b/tests/lib/FreeBSDTest.php
@@ -24,6 +24,7 @@
namespace OCA\ServerInfo\Tests;
use OCA\ServerInfo\OperatingSystems\FreeBSD;
+use OCA\ServerInfo\Resources\Memory;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;
@@ -58,17 +59,11 @@ class FreeBSDTest extends TestCase {
$memory = $this->os->getMemory();
- $this->assertArrayHasKey('MemTotal', $memory);
- $this->assertArrayHasKey('MemFree', $memory);
- $this->assertArrayHasKey('MemAvailable', $memory);
- $this->assertArrayHasKey('SwapTotal', $memory);
- $this->assertArrayHasKey('SwapFree', $memory);
-
- $this->assertEquals(68569628672, $memory['MemTotal']);
- $this->assertEquals(-1, $memory['MemFree']);
- $this->assertEquals(15809376256, $memory['MemAvailable']);
- $this->assertEquals(3744300, $memory['SwapTotal']);
- $this->assertEquals(3744300, $memory['SwapFree']);
+ $this->assertEquals(68569628672, $memory->getMemTotal());
+ $this->assertEquals(-1, $memory->getMemFree());
+ $this->assertEquals(15809376256, $memory->getMemAvailable());
+ $this->assertEquals(3744300, $memory->getSwapTotal());
+ $this->assertEquals(3744300, $memory->getSwapFree());
}
public function testGetMemoryNoSwapinfo(): void {
@@ -84,24 +79,18 @@ class FreeBSDTest extends TestCase {
$memory = $this->os->getMemory();
- $this->assertArrayHasKey('MemTotal', $memory);
- $this->assertArrayHasKey('MemFree', $memory);
- $this->assertArrayHasKey('MemAvailable', $memory);
- $this->assertArrayHasKey('SwapTotal', $memory);
- $this->assertArrayHasKey('SwapFree', $memory);
-
- $this->assertEquals(68569628672, $memory['MemTotal']);
- $this->assertEquals(-1, $memory['MemFree']);
- $this->assertEquals(15809376256, $memory['MemAvailable']);
- $this->assertEquals(-1, $memory['SwapTotal']);
- $this->assertEquals(-1, $memory['SwapFree']);
+ $this->assertEquals(68569628672, $memory->getMemTotal());
+ $this->assertEquals(-1, $memory->getMemFree());
+ $this->assertEquals(15809376256, $memory->getMemAvailable());
+ $this->assertEquals(-1, $memory->getSwapTotal());
+ $this->assertEquals(-1, $memory->getSwapFree());
}
public function testGetMemoryNoData(): void {
$this->os->method('executeCommand')
->willThrowException(new \RuntimeException('No output for command: xxx'));
- $this->assertSame(['MemTotal' => -1, 'MemFree' => -1, 'MemAvailable' => -1, 'SwapTotal' => -1, 'SwapFree' => -1], $this->os->getMemory());
+ $this->assertEquals(new Memory(), $this->os->getMemory());
}
public function testSupported(): void {