2024-06-18 14:42:42 +00:00
|
|
|
<?php
|
|
|
|
|
2024-06-21 12:49:13 +00:00
|
|
|
namespace App\Livewire\Server;
|
2024-06-18 14:42:42 +00:00
|
|
|
|
|
|
|
use App\Models\Server;
|
|
|
|
use Livewire\Component;
|
|
|
|
|
2024-06-21 12:49:13 +00:00
|
|
|
class Charts extends Component
|
2024-06-18 14:42:42 +00:00
|
|
|
{
|
|
|
|
public Server $server;
|
|
|
|
|
2024-06-21 12:49:13 +00:00
|
|
|
public $chartId = 'server';
|
2024-06-18 14:42:42 +00:00
|
|
|
|
|
|
|
public $data;
|
|
|
|
|
|
|
|
public $categories;
|
2024-06-18 14:43:18 +00:00
|
|
|
|
2024-06-19 07:30:56 +00:00
|
|
|
public int $interval = 5;
|
|
|
|
|
|
|
|
public bool $poll = true;
|
2024-06-18 14:42:42 +00:00
|
|
|
|
2024-06-19 07:30:56 +00:00
|
|
|
public function pollData()
|
|
|
|
{
|
|
|
|
if ($this->poll || $this->interval <= 10) {
|
|
|
|
$this->loadData();
|
|
|
|
if ($this->interval > 10) {
|
|
|
|
$this->poll = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-18 14:42:42 +00:00
|
|
|
public function loadData()
|
|
|
|
{
|
|
|
|
try {
|
2024-06-21 12:49:13 +00:00
|
|
|
$cpuMetrics = $this->server->getCpuMetrics($this->interval);
|
|
|
|
$memoryMetrics = $this->server->getMemoryMetrics($this->interval);
|
|
|
|
$cpuMetrics = collect($cpuMetrics)->map(function ($metric) {
|
2024-06-18 14:42:42 +00:00
|
|
|
return [$metric[0], $metric[1]];
|
|
|
|
});
|
2024-06-21 12:49:13 +00:00
|
|
|
$memoryMetrics = collect($memoryMetrics)->map(function ($metric) {
|
|
|
|
return [$metric[0], $metric[1]];
|
|
|
|
});
|
|
|
|
$this->dispatch("refreshChartData-{$this->chartId}-cpu", [
|
|
|
|
'seriesData' => $cpuMetrics,
|
|
|
|
]);
|
|
|
|
$this->dispatch("refreshChartData-{$this->chartId}-memory", [
|
|
|
|
'seriesData' => $memoryMetrics,
|
2024-06-18 14:42:42 +00:00
|
|
|
]);
|
2024-06-21 12:49:13 +00:00
|
|
|
|
2024-06-18 14:42:42 +00:00
|
|
|
} catch (\Throwable $e) {
|
|
|
|
return handleError($e, $this);
|
|
|
|
}
|
|
|
|
}
|
2024-06-18 14:43:18 +00:00
|
|
|
|
|
|
|
public function setInterval()
|
|
|
|
{
|
2024-06-19 07:30:56 +00:00
|
|
|
if ($this->interval <= 10) {
|
|
|
|
$this->poll = true;
|
|
|
|
}
|
2024-06-18 14:42:42 +00:00
|
|
|
$this->loadData();
|
|
|
|
}
|
|
|
|
}
|