62 lines
1.3 KiB
PHP
62 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\Message;
|
|
|
|
class UptimeService
|
|
{
|
|
/**
|
|
* @return array{system: string, fox: string}
|
|
*/
|
|
public function collect(): array
|
|
{
|
|
return [
|
|
'system' => $this->systemUptime(),
|
|
'fox' => $this->foxUptime(),
|
|
];
|
|
}
|
|
|
|
protected function systemUptime(): string
|
|
{
|
|
$data = (string) @file_get_contents('/proc/uptime');
|
|
$seconds = (int) round((float) explode(' ', trim($data))[0]);
|
|
|
|
return $this->humanize($seconds);
|
|
}
|
|
|
|
protected function foxUptime(): string
|
|
{
|
|
$first = Message::oldest()->first();
|
|
|
|
if (! $first) {
|
|
return 'soeben gestartet';
|
|
}
|
|
|
|
return $first->created_at->diffForHumans(null, true).' aktiv';
|
|
}
|
|
|
|
protected function humanize(int $seconds): string
|
|
{
|
|
if ($seconds < 60) {
|
|
return "{$seconds}s";
|
|
}
|
|
$d = intdiv($seconds, 86400);
|
|
$h = intdiv($seconds % 86400, 3600);
|
|
$m = intdiv($seconds % 3600, 60);
|
|
|
|
$parts = [];
|
|
if ($d) {
|
|
$parts[] = "{$d}d";
|
|
}
|
|
if ($h) {
|
|
$parts[] = "{$h}h";
|
|
}
|
|
if ($m && ! $d) {
|
|
$parts[] = "{$m}m";
|
|
}
|
|
|
|
return implode(' ', $parts) ?: '0m';
|
|
}
|
|
}
|