Files
coolify/app/Livewire/Server/Charts.php
T

92 lines
2.5 KiB
PHP
Raw Normal View History

2024-06-18 16:42:42 +02:00
<?php
namespace App\Livewire\Server;
2024-06-18 16:42:42 +02:00
use App\Actions\Server\StartSentinel;
2024-06-18 16:42:42 +02:00
use App\Models\Server;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
2024-06-18 16:42:42 +02:00
use Livewire\Component;
class Charts extends Component
2024-06-18 16:42:42 +02:00
{
use AuthorizesRequests;
2024-06-18 16:42:42 +02:00
public Server $server;
public $chartId = 'server';
2024-06-18 16:42:42 +02:00
public $data;
public $categories;
2024-06-18 14:43:18 +00:00
2024-06-19 09:30:56 +02:00
public int $interval = 5;
public bool $poll = true;
2024-06-18 16:42:42 +02:00
2024-10-30 14:54:27 +01:00
public function mount(string $server_uuid)
{
try {
$this->server = Server::ownedByCurrentTeam()->whereUuid($server_uuid)->firstOrFail();
} catch (\Throwable $e) {
return handleError($e, $this);
}
}
public function toggleMetrics(): void
{
try {
$this->authorize('update', $this->server);
$this->server->settings->is_metrics_enabled = ! $this->server->settings->is_metrics_enabled;
$this->server->settings->save();
$this->server->refresh();
if ($this->server->isMetricsEnabled()) {
StartSentinel::run($this->server, true);
$this->dispatch('success', 'Metrics enabled. Starting Sentinel.');
$this->dispatch('refreshServerShow');
$this->redirect(route('server.metrics', ['server_uuid' => $this->server->uuid]), navigate: true);
} else {
$this->server->restartSentinel();
$this->dispatch('success', 'Metrics disabled. Restarting Sentinel.');
$this->dispatch('refreshServerShow');
}
} catch (\Throwable $e) {
handleError($e, $this);
2024-10-30 14:54:27 +01:00
}
}
2024-06-19 09:30:56 +02:00
public function pollData()
{
if ($this->poll || $this->interval <= 10) {
$this->loadData();
if ($this->interval > 10) {
$this->poll = false;
}
}
}
2024-06-18 16:42:42 +02: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 16:42:42 +02:00
]);
} catch (\Throwable $e) {
return handleError($e, $this);
}
}
2024-06-18 14:43:18 +00:00
public function setInterval()
{
2024-06-19 09:30:56 +02:00
if ($this->interval <= 10) {
$this->poll = true;
}
2024-06-18 16:42:42 +02:00
$this->loadData();
}
}