cleanup session logic and reduce number of node requests

This commit is contained in:
Boy132 2025-05-16 08:57:30 +02:00
parent 28f971b26e
commit 7a10f94221
2 changed files with 24 additions and 16 deletions

View File

@ -20,19 +20,24 @@ class NodeCpuChart extends ChartWidget
*/ */
protected array $cpuHistory = []; protected array $cpuHistory = [];
protected int $threads = 0;
protected function getData(): array protected function getData(): array
{ {
$data = $this->node->statistics(); $sessionKey = "node_stats.{$this->node->id}";
$threads = $this->node->systemInformation()['cpu_count'] ?? 0;
$this->cpuHistory = session()->get('cpuHistory', []); $data = $this->node->statistics();
$this->threads = session("{$sessionKey}.threads", $this->node->systemInformation()['cpu_count'] ?? 0);
$this->cpuHistory = session("{$sessionKey}.cpu_history", []);
$this->cpuHistory[] = [ $this->cpuHistory[] = [
'cpu' => round($data['cpu_percent'] * $threads, 2), 'cpu' => round($data['cpu_percent'] * $this->threads, 2),
'timestamp' => now(auth()->user()->timezone ?? 'UTC')->format('H:i:s'), 'timestamp' => now(auth()->user()->timezone ?? 'UTC')->format('H:i:s'),
]; ];
$this->cpuHistory = array_slice($this->cpuHistory, -60); $this->cpuHistory = array_slice($this->cpuHistory, -60);
session()->put('cpuHistory', $this->cpuHistory); session()->put("{$sessionKey}.cpu_history", $this->cpuHistory);
return [ return [
'datasets' => [ 'datasets' => [
@ -75,11 +80,10 @@ class NodeCpuChart extends ChartWidget
public function getHeading(): string public function getHeading(): string
{ {
$threads = $this->node->systemInformation()['cpu_count'] ?? 0;
$data = array_slice(end($this->cpuHistory), -60); $data = array_slice(end($this->cpuHistory), -60);
$cpu = Number::format($data['cpu'], maxPrecision: 2, locale: auth()->user()->language); $cpu = Number::format($data['cpu'], maxPrecision: 2, locale: auth()->user()->language);
$max = Number::format($threads * 100, locale: auth()->user()->language); $max = Number::format($this->threads * 100, locale: auth()->user()->language);
return trans('admin/node.cpu_chart', ['cpu' => $cpu, 'max' => $max]); return trans('admin/node.cpu_chart', ['cpu' => $cpu, 'max' => $max]);
} }

View File

@ -20,21 +20,26 @@ class NodeMemoryChart extends ChartWidget
*/ */
protected array $memoryHistory = []; protected array $memoryHistory = [];
protected int $totalMemory = 0;
protected function getData(): array protected function getData(): array
{ {
$data = $this->node->statistics(); $sessionKey = "node_stats.{$this->node->id}";
$value = $data['memory_used'];
$this->memoryHistory = session()->get('memoryHistory', []); $data = $this->node->statistics();
$this->totalMemory = session("{$sessionKey}.total_memory", $data['memory_total']);
$this->memoryHistory = session("{$sessionKey}.memory_history", []);
$this->memoryHistory[] = [ $this->memoryHistory[] = [
'memory' => round(config('panel.use_binary_prefix') 'memory' => round(config('panel.use_binary_prefix')
? $value / 1024 / 1024 / 1024 ? $data['memory_used'] / 1024 / 1024 / 1024
: $value / 1000 / 1000 / 1000, 2), : $data['memory_used'] / 1000 / 1000 / 1000, 2),
'timestamp' => now(auth()->user()->timezone ?? 'UTC')->format('H:i:s'), 'timestamp' => now(auth()->user()->timezone ?? 'UTC')->format('H:i:s'),
]; ];
$this->memoryHistory = array_slice($this->memoryHistory, -60); $this->memoryHistory = array_slice($this->memoryHistory, -60);
session()->put('memoryHistory', $this->memoryHistory); session()->put("{$sessionKey}.memory_history", $this->memoryHistory);
return [ return [
'datasets' => [ 'datasets' => [
@ -78,15 +83,14 @@ class NodeMemoryChart extends ChartWidget
public function getHeading(): string public function getHeading(): string
{ {
$latestMemoryUsed = array_slice(end($this->memoryHistory), -60); $latestMemoryUsed = array_slice(end($this->memoryHistory), -60);
$totalMemory = $this->node->statistics()['memory_total'];
$used = config('panel.use_binary_prefix') $used = config('panel.use_binary_prefix')
? Number::format($latestMemoryUsed['memory'], maxPrecision: 2, locale: auth()->user()->language) .' GiB' ? Number::format($latestMemoryUsed['memory'], maxPrecision: 2, locale: auth()->user()->language) .' GiB'
: Number::format($latestMemoryUsed['memory'], maxPrecision: 2, locale: auth()->user()->language) . ' GB'; : Number::format($latestMemoryUsed['memory'], maxPrecision: 2, locale: auth()->user()->language) . ' GB';
$total = config('panel.use_binary_prefix') $total = config('panel.use_binary_prefix')
? Number::format($totalMemory / 1024 / 1024 / 1024, maxPrecision: 2, locale: auth()->user()->language) .' GiB' ? Number::format($this->totalMemory / 1024 / 1024 / 1024, maxPrecision: 2, locale: auth()->user()->language) .' GiB'
: Number::format($totalMemory / 1000 / 1000 / 1000, maxPrecision: 2, locale: auth()->user()->language) . ' GB'; : Number::format($this->totalMemory / 1000 / 1000 / 1000, maxPrecision: 2, locale: auth()->user()->language) . ' GB';
return trans('admin/node.memory_chart', ['used' => $used, 'total' => $total]); return trans('admin/node.memory_chart', ['used' => $used, 'total' => $total]);
} }