diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 9e52a5b81..48fbe6b84 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -7,7 +7,6 @@ use App\Console\Commands\Maintenance\CleanServiceBackupFilesCommand; use App\Console\Commands\Maintenance\PruneImagesCommand; use App\Console\Commands\Maintenance\PruneOrphanedBackupsCommand; use App\Console\Commands\Schedule\ProcessRunnableCommand; -use App\Jobs\NodeStatistics; use App\Models\ActivityLog; use App\Models\Webhook; use Illuminate\Console\Scheduling\Schedule; @@ -44,8 +43,6 @@ class Kernel extends ConsoleKernel $schedule->command(PruneImagesCommand::class)->daily(); $schedule->command(CheckEggUpdatesCommand::class)->hourly(); - $schedule->job(new NodeStatistics())->everyFiveSeconds()->withoutOverlapping(); - if (config('backups.prune_age')) { // Every 30 minutes, run the backup pruning command so that any abandoned backups can be deleted. $schedule->command(PruneOrphanedBackupsCommand::class)->everyThirtyMinutes(); diff --git a/app/Filament/Admin/Resources/NodeResource/Widgets/NodeCpuChart.php b/app/Filament/Admin/Resources/NodeResource/Widgets/NodeCpuChart.php index 4cb23b787..b159a00b3 100644 --- a/app/Filament/Admin/Resources/NodeResource/Widgets/NodeCpuChart.php +++ b/app/Filament/Admin/Resources/NodeResource/Widgets/NodeCpuChart.php @@ -3,7 +3,6 @@ namespace App\Filament\Admin\Resources\NodeResource\Widgets; use App\Models\Node; -use Carbon\Carbon; use Filament\Support\RawJs; use Filament\Widgets\ChartWidget; use Illuminate\Support\Number; @@ -16,22 +15,34 @@ class NodeCpuChart extends ChartWidget public Node $node; + /** + * @var array + */ + protected array $cpuHistory = []; + + protected int $threads = 0; + protected function getData(): array { - $threads = $this->node->systemInformation()['cpu_count'] ?? 0; + $sessionKey = "node_stats.{$this->node->id}"; - $cpu = collect(cache()->get("nodes.{$this->node->id}.cpu_percent")) - ->slice(-10) - ->map(fn ($value, $key) => [ - 'cpu' => round($value * $threads, 2), - 'timestamp' => Carbon::createFromTimestamp($key, auth()->user()->timezone ?? 'UTC')->format('H:i:s'), - ]) - ->all(); + $data = $this->node->statistics(); + + $this->threads = session("{$sessionKey}.threads", $this->node->systemInformation()['cpu_count'] ?? 0); + + $this->cpuHistory = session("{$sessionKey}.cpu_history", []); + $this->cpuHistory[] = [ + 'cpu' => round($data['cpu_percent'] * $this->threads, 2), + 'timestamp' => now(auth()->user()->timezone ?? 'UTC')->format('H:i:s'), + ]; + + $this->cpuHistory = array_slice($this->cpuHistory, -60); + session()->put("{$sessionKey}.cpu_history", $this->cpuHistory); return [ 'datasets' => [ [ - 'data' => array_column($cpu, 'cpu'), + 'data' => array_column($this->cpuHistory, 'cpu'), 'backgroundColor' => [ 'rgba(96, 165, 250, 0.3)', ], @@ -39,7 +50,7 @@ class NodeCpuChart extends ChartWidget 'fill' => true, ], ], - 'labels' => array_column($cpu, 'timestamp'), + 'labels' => array_column($this->cpuHistory, 'timestamp'), 'locale' => auth()->user()->language ?? 'en', ]; } @@ -69,10 +80,10 @@ class NodeCpuChart extends ChartWidget public function getHeading(): string { - $threads = $this->node->systemInformation()['cpu_count'] ?? 0; + $data = array_slice(end($this->cpuHistory), -60); - $cpu = Number::format(collect(cache()->get("nodes.{$this->node->id}.cpu_percent"))->last() * $threads, maxPrecision: 2, locale: auth()->user()->language); - $max = Number::format($threads * 100, locale: auth()->user()->language); + $cpu = Number::format($data['cpu'], maxPrecision: 2, locale: auth()->user()->language); + $max = Number::format($this->threads * 100, locale: auth()->user()->language); return trans('admin/node.cpu_chart', ['cpu' => $cpu, 'max' => $max]); } diff --git a/app/Filament/Admin/Resources/NodeResource/Widgets/NodeMemoryChart.php b/app/Filament/Admin/Resources/NodeResource/Widgets/NodeMemoryChart.php index 23147f9cc..256c37e46 100644 --- a/app/Filament/Admin/Resources/NodeResource/Widgets/NodeMemoryChart.php +++ b/app/Filament/Admin/Resources/NodeResource/Widgets/NodeMemoryChart.php @@ -3,7 +3,6 @@ namespace App\Filament\Admin\Resources\NodeResource\Widgets; use App\Models\Node; -use Carbon\Carbon; use Filament\Support\RawJs; use Filament\Widgets\ChartWidget; use Illuminate\Support\Number; @@ -16,19 +15,36 @@ class NodeMemoryChart extends ChartWidget public Node $node; + /** + * @var array + */ + protected array $memoryHistory = []; + + protected int $totalMemory = 0; + protected function getData(): array { - $memUsed = collect(cache()->get("nodes.{$this->node->id}.memory_used"))->slice(-10) - ->map(fn ($value, $key) => [ - 'memory' => round(config('panel.use_binary_prefix') ? $value / 1024 / 1024 / 1024 : $value / 1000 / 1000 / 1000, 2), - 'timestamp' => Carbon::createFromTimestamp($key, auth()->user()->timezone ?? 'UTC')->format('H:i:s'), - ]) - ->all(); + $sessionKey = "node_stats.{$this->node->id}"; + + $data = $this->node->statistics(); + + $this->totalMemory = session("{$sessionKey}.total_memory", $data['memory_total']); + + $this->memoryHistory = session("{$sessionKey}.memory_history", []); + $this->memoryHistory[] = [ + 'memory' => round(config('panel.use_binary_prefix') + ? $data['memory_used'] / 1024 / 1024 / 1024 + : $data['memory_used'] / 1000 / 1000 / 1000, 2), + 'timestamp' => now(auth()->user()->timezone ?? 'UTC')->format('H:i:s'), + ]; + + $this->memoryHistory = array_slice($this->memoryHistory, -60); + session()->put("{$sessionKey}.memory_history", $this->memoryHistory); return [ 'datasets' => [ [ - 'data' => array_column($memUsed, 'memory'), + 'data' => array_column($this->memoryHistory, 'memory'), 'backgroundColor' => [ 'rgba(96, 165, 250, 0.3)', ], @@ -36,7 +52,7 @@ class NodeMemoryChart extends ChartWidget 'fill' => true, ], ], - 'labels' => array_column($memUsed, 'timestamp'), + 'labels' => array_column($this->memoryHistory, 'timestamp'), 'locale' => auth()->user()->language ?? 'en', ]; } @@ -66,16 +82,15 @@ class NodeMemoryChart extends ChartWidget public function getHeading(): string { - $latestMemoryUsed = collect(cache()->get("nodes.{$this->node->id}.memory_used"))->last(); - $totalMemory = collect(cache()->get("nodes.{$this->node->id}.memory_total"))->last(); + $latestMemoryUsed = array_slice(end($this->memoryHistory), -60); $used = config('panel.use_binary_prefix') - ? Number::format($latestMemoryUsed / 1024 / 1024 / 1024, maxPrecision: 2, locale: auth()->user()->language) .' GiB' - : Number::format($latestMemoryUsed / 1000 / 1000 / 1000, maxPrecision: 2, locale: auth()->user()->language) . ' GB'; + ? Number::format($latestMemoryUsed['memory'], maxPrecision: 2, locale: auth()->user()->language) .' GiB' + : Number::format($latestMemoryUsed['memory'], maxPrecision: 2, locale: auth()->user()->language) . ' GB'; $total = config('panel.use_binary_prefix') - ? Number::format($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 / 1024 / 1024 / 1024, maxPrecision: 2, locale: auth()->user()->language) .' GiB' + : Number::format($this->totalMemory / 1000 / 1000 / 1000, maxPrecision: 2, locale: auth()->user()->language) . ' GB'; return trans('admin/node.memory_chart', ['used' => $used, 'total' => $total]); } diff --git a/app/Jobs/NodeStatistics.php b/app/Jobs/NodeStatistics.php deleted file mode 100644 index d6815ce6c..000000000 --- a/app/Jobs/NodeStatistics.php +++ /dev/null @@ -1,35 +0,0 @@ -statistics(); - $timestamp = now()->getTimestamp(); - - foreach ($stats as $key => $value) { - $cacheKey = "nodes.{$node->id}.$key"; - $data = cache()->get($cacheKey, []); - - // Add current timestamp and value to the data array - $data[$timestamp] = $value; - - // Update the cache with the new data, expires in 1 minute - cache()->put($cacheKey, $data, now()->addMinute()); - } - } - } -}