2018-02-01 11:49:40 +00:00
|
|
|
<?php
|
2020-10-14 22:19:31 +00:00
|
|
|
|
2021-07-19 05:53:45 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-08-04 22:05:14 +00:00
|
|
|
namespace App\Controller\Admin;
|
2018-02-01 11:49:40 +00:00
|
|
|
|
|
|
|
use App\Acl;
|
2020-12-03 04:18:06 +00:00
|
|
|
use App\Environment;
|
2019-08-09 15:00:21 +00:00
|
|
|
use App\Http\Response;
|
|
|
|
use App\Http\ServerRequest;
|
2019-06-28 03:25:16 +00:00
|
|
|
use App\Radio\Quota;
|
2019-09-20 16:44:38 +00:00
|
|
|
use App\Sync\Runner;
|
2019-06-28 03:25:16 +00:00
|
|
|
use Brick\Math\BigInteger;
|
2018-11-17 15:30:28 +00:00
|
|
|
use Psr\Http\Message\ResponseInterface;
|
2018-02-01 11:49:40 +00:00
|
|
|
|
|
|
|
class IndexController
|
|
|
|
{
|
2020-03-01 16:26:49 +00:00
|
|
|
public function __invoke(
|
|
|
|
ServerRequest $request,
|
|
|
|
Response $response,
|
2020-12-03 04:18:06 +00:00
|
|
|
Runner $sync,
|
|
|
|
Environment $environment
|
2020-03-01 16:26:49 +00:00
|
|
|
): ResponseInterface {
|
2019-08-09 15:00:21 +00:00
|
|
|
$view = $request->getView();
|
2018-02-06 17:55:58 +00:00
|
|
|
|
2018-02-01 11:49:40 +00:00
|
|
|
// Remove the sidebar on the homepage.
|
2019-08-07 04:33:55 +00:00
|
|
|
$view->addData(['sidebar' => null]);
|
2018-02-01 11:49:40 +00:00
|
|
|
|
|
|
|
// Synchronization statuses
|
2019-08-09 15:00:21 +00:00
|
|
|
$acl = $request->getAcl();
|
2019-08-07 04:33:55 +00:00
|
|
|
|
2021-01-11 11:08:06 +00:00
|
|
|
if ($acl->isAllowed(Acl::GLOBAL_ALL)) {
|
|
|
|
$view->addData(
|
|
|
|
[
|
|
|
|
'sync_times' => $sync->getSyncTimes(),
|
|
|
|
]
|
|
|
|
);
|
2018-02-01 11:49:40 +00:00
|
|
|
}
|
|
|
|
|
2020-12-03 04:18:06 +00:00
|
|
|
$stationsBaseDir = $environment->getStationDirectory();
|
2019-06-28 03:25:16 +00:00
|
|
|
|
2021-04-24 04:43:23 +00:00
|
|
|
$spaceTotalFloat = disk_total_space($stationsBaseDir);
|
|
|
|
$spaceTotal = (is_float($spaceTotalFloat))
|
|
|
|
? BigInteger::of($spaceTotalFloat)
|
|
|
|
: BigInteger::zero();
|
|
|
|
|
|
|
|
$spaceFreeFloat = disk_free_space($stationsBaseDir);
|
|
|
|
$spaceFree = (is_float($spaceFreeFloat))
|
|
|
|
? BigInteger::of($spaceFreeFloat)
|
|
|
|
: BigInteger::zero();
|
|
|
|
|
2020-11-14 19:32:31 +00:00
|
|
|
$spaceUsed = $spaceTotal->minus($spaceFree);
|
|
|
|
|
|
|
|
// Get memory info.
|
2021-07-19 05:53:45 +00:00
|
|
|
$meminfoRaw = file("/proc/meminfo", FILE_IGNORE_NEW_LINES) ?: [];
|
2020-11-14 19:32:31 +00:00
|
|
|
$meminfo = [];
|
|
|
|
foreach ($meminfoRaw as $line) {
|
2021-04-23 02:16:00 +00:00
|
|
|
if (str_contains($line, ':')) {
|
|
|
|
[$key, $val] = explode(":", $line);
|
|
|
|
$meminfo[$key] = trim($val);
|
|
|
|
}
|
2020-11-14 19:32:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$memoryTotal = Quota::convertFromReadableSize($meminfo['MemTotal']) ?? BigInteger::zero();
|
|
|
|
$memoryFree = Quota::convertFromReadableSize($meminfo['MemAvailable']) ?? BigInteger::zero();
|
|
|
|
$memoryUsed = $memoryTotal->minus($memoryFree);
|
2019-06-28 03:25:16 +00:00
|
|
|
|
2021-01-11 11:08:06 +00:00
|
|
|
return $view->renderToResponse(
|
|
|
|
$response,
|
|
|
|
'admin/index/index',
|
|
|
|
[
|
|
|
|
'load' => sys_getloadavg(),
|
|
|
|
'space_percent' => Quota::getPercentage($spaceUsed, $spaceTotal),
|
|
|
|
'space_used' => Quota::getReadableSize($spaceUsed),
|
|
|
|
'space_total' => Quota::getReadableSize($spaceTotal),
|
|
|
|
'memory_percent' => Quota::getPercentage($memoryUsed, $memoryTotal),
|
|
|
|
'memory_used' => Quota::getReadableSize($memoryUsed),
|
|
|
|
'memory_total' => Quota::getReadableSize($memoryTotal),
|
|
|
|
]
|
|
|
|
);
|
2018-02-01 11:49:40 +00:00
|
|
|
}
|
2018-08-04 22:05:14 +00:00
|
|
|
}
|