2024-06-18 16:42:42 +02:00
|
|
|
<?php
|
|
|
|
|
|
2024-06-21 14:49:13 +02:00
|
|
|
namespace App\Livewire\Server;
|
2024-06-18 16:42:42 +02:00
|
|
|
|
2026-04-14 18:50:01 +05:30
|
|
|
use App\Actions\Server\StartSentinel;
|
2024-06-18 16:42:42 +02:00
|
|
|
use App\Models\Server;
|
2026-04-14 18:50:01 +05:30
|
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
2024-06-18 16:42:42 +02:00
|
|
|
use Livewire\Component;
|
|
|
|
|
|
2024-06-21 14:49:13 +02:00
|
|
|
class Charts extends Component
|
2024-06-18 16:42:42 +02:00
|
|
|
{
|
2026-04-14 18:50:01 +05:30
|
|
|
use AuthorizesRequests;
|
|
|
|
|
|
2024-06-18 16:42:42 +02:00
|
|
|
public Server $server;
|
|
|
|
|
|
2024-06-21 14:49:13 +02:00
|
|
|
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) {
|
2026-04-14 18:50:01 +05:30
|
|
|
return handleError($e, $this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-03 11:10:13 +02:00
|
|
|
public function toggleMetrics(): void
|
2026-04-14 18:50:01 +05:30
|
|
|
{
|
|
|
|
|
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);
|
2026-06-03 11:10:13 +02:00
|
|
|
$this->dispatch('success', 'Metrics enabled. Starting Sentinel.');
|
|
|
|
|
$this->dispatch('refreshServerShow');
|
2026-04-14 19:05:40 +05:30
|
|
|
$this->redirect(route('server.metrics', ['server_uuid' => $this->server->uuid]), navigate: true);
|
2026-04-14 18:50:01 +05:30
|
|
|
} else {
|
|
|
|
|
$this->server->restartSentinel();
|
|
|
|
|
$this->dispatch('success', 'Metrics disabled. Restarting Sentinel.');
|
2026-06-03 11:10:13 +02:00
|
|
|
$this->dispatch('refreshServerShow');
|
2026-04-14 18:50:01 +05:30
|
|
|
}
|
|
|
|
|
} catch (\Throwable $e) {
|
2026-06-03 11:10:13 +02:00
|
|
|
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 {
|
2024-06-21 14:49:13 +02:00
|
|
|
$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();
|
|
|
|
|
}
|
|
|
|
|
}
|