homeos/app/Livewire/Host.php

65 lines
1.9 KiB
PHP

<?php
namespace App\Livewire;
use App\Models\Metric;
use App\Services\SystemHealth;
use Livewire\Attributes\Layout;
use Livewire\Component;
#[Layout('layouts.app')]
class Host extends Component
{
/** @var array<int, array{key:string,label:string,icon:string,state:string,detail:string}> */
public array $services = [];
public ?string $checkedAt = null;
public function mount(): void
{
$this->refreshHealth();
}
public function refreshHealth(): void
{
$this->services = app(SystemHealth::class)->services();
$this->checkedAt = now()->format('H:i:s');
}
public function worstState(): string
{
$states = array_column($this->services, 'state');
return in_array('offline', $states, true) ? 'offline'
: (in_array('warning', $states, true) ? 'warning' : 'online');
}
public function problemCount(): int
{
return count(array_filter($this->services, fn ($s) => $s['state'] !== 'online'));
}
public function chartConfig(): array
{
$metrics = Metric::orderByDesc('sampled_at')->limit(60)->get()->reverse()->values();
return [
'labels' => $metrics->map(fn ($m) => $m->sampled_at->format('H:i'))->all(),
'series' => [
['key' => 'mqtt', 'label' => __('host.chart_mqtt'), 'color' => '--color-accent', 'axis' => 'y', 'data' => $metrics->pluck('mqtt_messages')->all()],
['key' => 'cpu', 'label' => __('host.chart_cpu'), 'color' => '--color-online', 'axis' => 'y', 'data' => $metrics->pluck('cpu_load')->all()],
['key' => 'mem', 'label' => __('host.chart_mem'), 'color' => '--color-warning', 'axis' => 'y1', 'data' => $metrics->pluck('mem_used_pct')->all()],
],
];
}
public function render()
{
return view('livewire.host', [
'chart' => $this->chartConfig(),
'hasMetrics' => Metric::exists(),
]);
}
}