coolify/app/Livewire/Server/Charts.php

65 lines
1.5 KiB
PHP
Raw Permalink 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-10-30 13:54:27 +00:00
public function mount(string $server_uuid)
{
try {
$this->server = Server::ownedByCurrentTeam()->whereUuid($server_uuid)->firstOrFail();
} catch (\Throwable $e) {
return handleError($e, $this);
}
}
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);
$this->dispatch("refreshChartData-{$this->chartId}-cpu", [
'seriesData' => $cpuMetrics,
]);
$this->dispatch("refreshChartData-{$this->chartId}-memory", [
'seriesData' => $memoryMetrics,
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();
}
}