diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 7545f177e..13113d2b5 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; @@ -41,8 +40,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 da2752415..c5ad7099e 100644 --- a/app/Filament/Admin/Resources/NodeResource/Widgets/NodeCpuChart.php +++ b/app/Filament/Admin/Resources/NodeResource/Widgets/NodeCpuChart.php @@ -3,35 +3,41 @@ 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; class NodeCpuChart extends ChartWidget { - protected static ?string $pollingInterval = '5s'; + protected static ?string $pollingInterval = '1s'; protected static ?string $maxHeight = '300px'; public Node $node; + /** + * @var array + */ + protected array $cpuHistory = []; + protected function getData(): array { - $threads = $this->node->systemInformation()['cpu_count'] ?? 0; + $data = $this->node->statistics(); + $threads = $this->node->systemInformation()['cpu_count']; - $cpu = collect(cache()->get("nodes.{$this->node->id}.cpu_percent")) - ->slice(-10) - ->map(fn ($value, $key) => [ - 'cpu' => Number::format($value * $threads, maxPrecision: 2), - 'timestamp' => Carbon::createFromTimestamp($key, auth()->user()->timezone ?? 'UTC')->format('H:i:s'), - ]) - ->all(); + $this->cpuHistory = session()->get('cpuHistory', []); + $this->cpuHistory[] = [ + 'cpu' => Number::format($data['cpu_percent'] * $threads, maxPrecision: 2), + 'timestamp' => now(auth()->user()->timezone ?? 'UTC')->format('H:i:s'), + ]; + + $this->cpuHistory = array_slice($this->cpuHistory, -60); + session()->put('cpuHistory', $this->cpuHistory); return [ 'datasets' => [ [ - 'data' => array_column($cpu, 'cpu'), + 'data' => array_column($this->cpuHistory, 'cpu'), 'backgroundColor' => [ 'rgba(96, 165, 250, 0.3)', ], @@ -39,7 +45,7 @@ class NodeCpuChart extends ChartWidget 'fill' => true, ], ], - 'labels' => array_column($cpu, 'timestamp'), + 'labels' => array_column($this->cpuHistory, 'timestamp'), 'locale' => auth()->user()->language ?? 'en', ]; } @@ -69,9 +75,10 @@ class NodeCpuChart extends ChartWidget public function getHeading(): string { - $threads = $this->node->systemInformation()['cpu_count'] ?? 0; + $threads = $this->node->systemInformation()['cpu_count']; + $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); + $cpu = Number::format($data['cpu'], maxPrecision: 2, locale: auth()->user()->language); $max = Number::format($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 4b18d846e..25672be57 100644 --- a/app/Filament/Admin/Resources/NodeResource/Widgets/NodeMemoryChart.php +++ b/app/Filament/Admin/Resources/NodeResource/Widgets/NodeMemoryChart.php @@ -3,32 +3,43 @@ 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; class NodeMemoryChart extends ChartWidget { - protected static ?string $pollingInterval = '5s'; + protected static ?string $pollingInterval = '1s'; protected static ?string $maxHeight = '300px'; public Node $node; + /** + * @var array + */ + protected array $memoryHistory = []; + protected function getData(): array { - $memUsed = collect(cache()->get("nodes.{$this->node->id}.memory_used"))->slice(-10) - ->map(fn ($value, $key) => [ - 'memory' => Number::format(config('panel.use_binary_prefix') ? $value / 1024 / 1024 / 1024 : $value / 1000 / 1000 / 1000, maxPrecision: 2), - 'timestamp' => Carbon::createFromTimestamp($key, auth()->user()->timezone ?? 'UTC')->format('H:i:s'), - ]) - ->all(); + $data = $this->node->statistics(); + $value = $data['memory_used']; + + $this->memoryHistory = session()->get('memoryHistory', []); + $this->memoryHistory[] = [ + 'memory' => Number::format(config('panel.use_binary_prefix') + ? $value / 1024 / 1024 / 1024 + : $value / 1000 / 1000 / 1000, maxPrecision: 2), + 'timestamp' => now(auth()->user()->timezone ?? 'UTC')->format('H:i:s'), + ]; + + $this->memoryHistory = array_slice($this->memoryHistory, -60); + session()->put('memoryHistory', $this->memoryHistory); return [ 'datasets' => [ [ - 'data' => array_column($memUsed, 'memory'), + 'data' => array_column($this->memoryHistory, 'memory'), 'backgroundColor' => [ 'rgba(96, 165, 250, 0.3)', ], @@ -36,7 +47,7 @@ class NodeMemoryChart extends ChartWidget 'fill' => true, ], ], - 'labels' => array_column($memUsed, 'timestamp'), + 'labels' => array_column($this->memoryHistory, 'timestamp'), 'locale' => auth()->user()->language ?? 'en', ]; } @@ -66,12 +77,12 @@ 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); + $totalMemory = $this->node->statistics()['memory_total']; $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' 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()); - } - } - } -}