fix: make server charts one livewire component with one interval selector

This commit is contained in:
Andras Bacsai
2024-06-21 14:49:13 +02:00
parent 28522418ff
commit 453b28baf7
6 changed files with 248 additions and 312 deletions

View File

@@ -1,59 +0,0 @@
<?php
namespace App\Livewire\Charts;
use App\Models\Server as ModelsServer;
use Livewire\Component;
class ServerCpu extends Component
{
public ModelsServer $server;
public $chartId = 'server-cpu';
public $data;
public $categories;
public int $interval = 5;
public bool $poll = true;
public function render()
{
return view('livewire.charts.server-cpu');
}
public function pollData()
{
if ($this->poll || $this->interval <= 10) {
$this->loadData();
if ($this->interval > 10) {
$this->poll = false;
}
}
}
public function loadData()
{
try {
$metrics = $this->server->getCpuMetrics($this->interval);
$metrics = collect($metrics)->map(function ($metric) {
return [$metric[0], $metric[1]];
});
$this->dispatch("refreshChartData-{$this->chartId}", [
'seriesData' => $metrics,
]);
} catch (\Throwable $e) {
return handleError($e, $this);
}
}
public function setInterval()
{
if ($this->interval <= 10) {
$this->poll = true;
}
$this->loadData();
}
}

View File

@@ -1,15 +1,15 @@
<?php
namespace App\Livewire\Charts;
namespace App\Livewire\Server;
use App\Models\Server;
use Livewire\Component;
class ServerMemory extends Component
class Charts extends Component
{
public Server $server;
public $chartId = 'server-memory';
public $chartId = 'server';
public $data;
@@ -19,11 +19,6 @@ class ServerMemory extends Component
public bool $poll = true;
public function render()
{
return view('livewire.charts.server-memory');
}
public function pollData()
{
if ($this->poll || $this->interval <= 10) {
@@ -37,13 +32,21 @@ class ServerMemory extends Component
public function loadData()
{
try {
$metrics = $this->server->getMemoryMetrics($this->interval);
$metrics = collect($metrics)->map(function ($metric) {
$cpuMetrics = $this->server->getCpuMetrics($this->interval);
$memoryMetrics = $this->server->getMemoryMetrics($this->interval);
$cpuMetrics = collect($cpuMetrics)->map(function ($metric) {
return [$metric[0], $metric[1]];
});
$this->dispatch("refreshChartData-{$this->chartId}", [
'seriesData' => $metrics,
$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,
]);
} catch (\Throwable $e) {
return handleError($e, $this);
}