lasthourcloud/app/Livewire/Server/Charts.php

63 lines
1.5 KiB
PHP
Raw Normal View History

2024-06-18 14:42:42 +00:00
<?php
namespace App\Livewire\Server;
2024-06-18 14:42:42 +00:00
use App\Models\Server;
use Livewire\Component;
class Charts extends Component
2024-06-18 14:42:42 +00:00
{
public Server $server;
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 {
$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]];
});
$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-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();
}
}