Skip to content

Commit 7788a4a

Browse files
authored
Merge pull request #224 from nextcloud/add-disk-object
Add disk object
2 parents 7b3b344 + 2e93ac8 commit 7788a4a

7 files changed

Lines changed: 173 additions & 106 deletions

File tree

lib/OperatingSystems/DefaultOs.php

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
namespace OCA\ServerInfo\OperatingSystems;
2222

23+
use OCA\ServerInfo\Resources\Disk;
2324
use OCA\ServerInfo\Resources\Memory;
2425

2526
class DefaultOs implements IOperatingSystem {
@@ -190,15 +191,6 @@ public function getNetworkInterfaces() {
190191
return $result;
191192
}
192193

193-
/**
194-
* Get diskInfo will return a list of disks. Used and Available in bytes.
195-
*
196-
* [
197-
* [device => /dev/mapper/homestead--vg-root, fs => ext4, used => 6205468, available => 47321220, percent => 12%, mount => /]
198-
* ]
199-
*
200-
* @return array
201-
*/
202194
public function getDiskInfo(): array {
203195
$data = [];
204196

@@ -221,14 +213,15 @@ public function getDiskInfo(): array {
221213
continue;
222214
}
223215

224-
$data[] = [
225-
'device' => $filesystem,
226-
'fs' => $matches['Type'][$i],
227-
'used' => (int)$matches['Used'][$i] * 1024,
228-
'available' => (int)$matches['Available'][$i] * 1024,
229-
'percent' => $matches['Capacity'][$i],
230-
'mount' => $matches['Mounted'][$i],
231-
];
216+
$disk = new Disk();
217+
$disk->setDevice($filesystem);
218+
$disk->setFs($matches['Type'][$i]);
219+
$disk->setUsed((int)$matches['Used'][$i] * 1024);
220+
$disk->setAvailable((int)$matches['Available'][$i] * 1024);
221+
$disk->setPercent($matches['Capacity'][$i]);
222+
$disk->setMount($matches['Mounted'][$i]);
223+
224+
$data[] = $disk;
232225
}
233226

234227
return $data;

lib/OperatingSystems/FreeBSD.php

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
namespace OCA\ServerInfo\OperatingSystems;
2424

25+
use OCA\ServerInfo\Resources\Disk;
2526
use OCA\ServerInfo\Resources\Memory;
2627

2728
/**
@@ -213,15 +214,6 @@ public function getNetworkInterfaces() {
213214
return $result;
214215
}
215216

216-
/**
217-
* Get diskInfo will return a list of disks. Used and Available in bytes.
218-
*
219-
* [
220-
* [device => /dev/mapper/homestead--vg-root, fs => ext4, used => 6205468, available => 47321220, percent => 12%, mount => /]
221-
* ]
222-
*
223-
* @return array
224-
*/
225217
public function getDiskInfo(): array {
226218
$data = [];
227219

@@ -244,14 +236,15 @@ public function getDiskInfo(): array {
244236
continue;
245237
}
246238

247-
$data[] = [
248-
'device' => $filesystem,
249-
'fs' => $matches['Type'][$i],
250-
'used' => (int)$matches['Used'][$i] * 1024,
251-
'available' => (int)$matches['Available'][$i] * 1024,
252-
'percent' => $matches['Capacity'][$i],
253-
'mount' => $matches['Mounted'][$i],
254-
];
239+
$disk = new Disk();
240+
$disk->setDevice($filesystem);
241+
$disk->setFs($matches['Type'][$i]);
242+
$disk->setUsed((int)$matches['Used'][$i] * 1024);
243+
$disk->setAvailable((int)$matches['Available'][$i] * 1024);
244+
$disk->setPercent($matches['Capacity'][$i]);
245+
$disk->setMount($matches['Mounted'][$i]);
246+
247+
$data[] = $disk;
255248
}
256249

257250
return $data;

lib/OperatingSystems/IOperatingSystem.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,30 @@
2525

2626
namespace OCA\ServerInfo\OperatingSystems;
2727

28+
use OCA\ServerInfo\Resources\Disk;
2829
use OCA\ServerInfo\Resources\Memory;
2930

3031
interface IOperatingSystem {
3132
/**
32-
* Get memory returns a Memory object. All values are in bytes.
33+
* Get name of the processor
3334
*
34-
* @return Memory
35+
* @return string
3536
*/
36-
public function getMemory(): Memory;
37+
public function getCpuName(): string;
3738

3839
/**
39-
* Get name of the processor
40+
* Get disk info returns a list of Disk objects. Used and Available in bytes.
4041
*
41-
* @return string
42+
* @return Disk[]
4243
*/
43-
public function getCpuName(): string;
44+
public function getDiskInfo(): array;
45+
46+
/**
47+
* Get memory returns a Memory object. All values are in bytes.
48+
*
49+
* @return Memory
50+
*/
51+
public function getMemory(): Memory;
4452

4553
/**
4654
* Get the total number of seconds the system has been up or -1 on failure.

lib/Os.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,6 @@ public function getTimeServers() {
9191
return explode("\n", $data);
9292
}
9393

94-
/**
95-
* Get diskInfo will return a list of disks. Used and Available in bytes.
96-
*
97-
* [
98-
* [device => /dev/mapper/homestead--vg-root, fs => ext4, used => 6205468, available => 47321220, percent => 12%, mount => /]
99-
* ]
100-
*
101-
* @return array
102-
*/
10394
public function getDiskInfo(): array {
10495
return $this->backend->getDiskInfo();
10596
}
@@ -115,12 +106,11 @@ public function getDiskInfo(): array {
115106
*/
116107
public function getDiskData(): array {
117108
$data = [];
118-
$disks = $this->backend->getDiskInfo();
119109

120-
foreach ($disks as $disk) {
110+
foreach ($this->backend->getDiskInfo() as $disk) {
121111
$data[] = [
122-
round($disk['used'] / 1024 / 1024 / 1024, 1),
123-
round($disk['available'] / 1024 / 1024 / 1024, 1)
112+
round($disk->getUsed() / 1024 / 1024 / 1024, 1),
113+
round($disk->getAvailable() / 1024 / 1024 / 1024, 1)
124114
];
125115
}
126116

lib/Resources/Disk.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* @copyright Copyright (c) 2020 Daniel Kesselberg <mail@danielkesselberg.de>
6+
*
7+
* @author Daniel Kesselberg <mail@danielkesselberg.de>
8+
*
9+
* @license GNU AGPL version 3 or any later version
10+
*
11+
* This program is free software: you can redistribute it and/or modify
12+
* it under the terms of the GNU Affero General Public License as
13+
* published by the Free Software Foundation, either version 3 of the
14+
* License, or (at your option) any later version.
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Affero General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Affero General Public License
22+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
23+
*
24+
*/
25+
26+
namespace OCA\ServerInfo\Resources;
27+
28+
class Disk {
29+
private $device = '';
30+
private $fs = '';
31+
private $used = 0;
32+
private $available = 0;
33+
private $percent = '';
34+
private $mount = '';
35+
36+
public function getDevice(): string {
37+
return $this->device;
38+
}
39+
40+
public function setDevice(string $device): void {
41+
$this->device = $device;
42+
}
43+
44+
public function getFs(): string {
45+
return $this->fs;
46+
}
47+
48+
public function setFs(string $fs): void {
49+
$this->fs = $fs;
50+
}
51+
52+
public function getUsed(): int {
53+
return $this->used;
54+
}
55+
56+
public function setUsed(int $used): void {
57+
$this->used = $used;
58+
}
59+
60+
public function getAvailable(): int {
61+
return $this->available;
62+
}
63+
64+
public function setAvailable(int $available): void {
65+
$this->available = $available;
66+
}
67+
68+
public function getPercent(): string {
69+
return $this->percent;
70+
}
71+
72+
public function setPercent(string $percent): void {
73+
$this->percent = $percent;
74+
}
75+
76+
public function getMount(): string {
77+
return $this->mount;
78+
}
79+
80+
public function setMount(string $mount): void {
81+
$this->mount = $mount;
82+
}
83+
}

templates/settings-admin.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ function FormatBytes($byte) {
3737

3838
/** @var \OCA\ServerInfo\Resources\Memory $memory */
3939
$memory = $_['memory'];
40+
/** @var \OCA\ServerInfo\Resources\Disk[] $disks */
41+
$disks = $_['diskinfo'];
4042
?>
4143

4244
<div class="server-info-wrapper">
@@ -122,25 +124,25 @@ function FormatBytes($byte) {
122124
<?php p($l->t('Disk')); ?>
123125
</h2>
124126
</div>
125-
<?php foreach ($_['diskinfo'] as $disk): ?>
127+
<?php foreach ($disks as $disk): ?>
126128
<div class="col col-4 col-xl-6 col-m-12">
127129
<div class="infobox">
128130
<div class="diskchart-container">
129131
<canvas id="DiskChart" class="DiskChart" style="width:100%; height:200px" width="600"
130132
height="200"></canvas>
131133
</div>
132134
<div class="diskinfo-container">
133-
<h3><?php p(basename($disk['device'])); ?></h3>
135+
<h3><?php p(basename($disk->getDevice())); ?></h3>
134136
<?php p($l->t('Mount')); ?> :
135-
<span class="info"><?php p($disk['mount']); ?></span><br>
137+
<span class="info"><?php p($disk->getMount()); ?></span><br>
136138
<?php p($l->t('Filesystem')); ?> :
137-
<span class="info"><?php p($disk['fs']); ?></span><br>
139+
<span class="info"><?php p($disk->getFs()); ?></span><br>
138140
<?php p($l->t('Size')); ?> :
139-
<span class="info"><?php p(FormatBytes($disk['used'] + $disk['available'])); ?></span><br>
141+
<span class="info"><?php p(FormatBytes($disk->getUsed() + $disk->getAvailable())); ?></span><br>
140142
<?php p($l->t('Available')); ?> :
141-
<span class="info"><?php p(FormatBytes($disk['available'])); ?></span><br>
143+
<span class="info"><?php p(FormatBytes($disk->getAvailable())); ?></span><br>
142144
<?php p($l->t('Used')); ?> :
143-
<span class="info"><?php p($disk['percent']); ?></span><br>
145+
<span class="info"><?php p($disk->getPercent()); ?></span><br>
144146
</div>
145147
</div>
146148
</div>

0 commit comments

Comments
 (0)